Skip to content

Commit

Permalink
Add support for WasmEdge.
Browse files Browse the repository at this point in the history
Signed-off-by: Tricster <[email protected]>
  • Loading branch information
MediosZ committed Sep 5, 2022
1 parent 1fd7f82 commit 4f2e423
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 69 deletions.
7 changes: 6 additions & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# [build]
# rustflags = ["--cfg", "tokio_unstable"]
# rustflags = ["--cfg", "tokio_unstable"]
[build]
target="wasm32-wasi"

[target.wasm32-wasi]
runner = "wasmedge"
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
# If you copy one of the examples into a new project, you should be using
# [dependencies] instead, and delete the **path**.
[dev-dependencies]
tokio = { version = "1.0.0", path = "../tokio", features = ["full", "tracing"] }
tokio = { version = "1.0.0", path = "../tokio", features = ["sync", "macros", "io-util", "rt", "time", "net", "tracing"] }
tokio-util = { version = "0.7.0", path = "../tokio-util", features = ["full"] }
tokio-stream = { version = "0.1", path = "../tokio-stream" }

Expand Down
2 changes: 1 addition & 1 deletion examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::io;
use std::net::SocketAddr;
use std::sync::Arc;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
// Configure a `tracing` subscriber that logs traces emitted by the chat
Expand Down
2 changes: 1 addition & 1 deletion examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tokio::net::TcpListener;
use std::env;
use std::error::Error;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
// Allow passing an address to listen on as the first argument of this
// program, but otherwise we'll just set up our TCP listener on
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::net::TcpStream;

use std::error::Error;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
pub async fn main() -> Result<(), Box<dyn Error>> {
// Open a TCP stream to the socket address.
//
Expand Down
2 changes: 1 addition & 1 deletion examples/print_each_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use tokio_util::codec::{BytesCodec, Decoder};

use std::env;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Allow passing an address to listen on as the first argument of this
// program, but otherwise we'll just set up our TCP listener on
Expand Down
2 changes: 1 addition & 1 deletion examples/tinydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ enum Response {
},
}

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
// Parse the address we're going to run this server on
// and set up our TCP listener to accept connections.
Expand Down
2 changes: 1 addition & 1 deletion examples/tinyhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::net::{TcpListener, TcpStream};
use tokio_stream::StreamExt;
use tokio_util::codec::{Decoder, Encoder, Framed};

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
// Parse the arguments, bind the TCP socket we'll be listening to, spin up
// our worker threads, and start shipping sockets to those worker threads.
Expand Down
4 changes: 3 additions & 1 deletion tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pin-project-lite = "0.2.0"
bytes = { version = "1.0.0", optional = true }
once_cell = { version = "1.5.2", optional = true }
memchr = { version = "2.2", optional = true }
mio = { version = "0.8.4", optional = true }
mio = {git="https://github.com/WasmEdge/mio.git", optional = true, features = ["wasmedge", "os-poll", "net"] }
num_cpus = { version = "1.8.0", optional = true }
parking_lot = { version = "0.12.0", optional = true }

Expand Down Expand Up @@ -159,6 +159,8 @@ rand = "0.8.0"

[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), not(target_os = "wasi")))'.dev-dependencies]
wasm-bindgen-test = "0.3.0"
[target.'cfg(target_os = "wasi")'.dependencies]
wasmedge_wasi_socket = {git="https://github.com/second-state/wasmedge_wasi_socket.git"}

[target.'cfg(target_os = "freebsd")'.dev-dependencies]
mio-aio = { version = "0.6.0", features = ["tokio"] }
Expand Down
2 changes: 1 addition & 1 deletion tokio/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn main() {
if target.starts_with("wasm") {
autocfg::emit("tokio_wasm");
if target.contains("wasi") {
autocfg::emit("tokio_wasi");
autocfg::emit("tokio_wasi_wasmedge");
} else {
autocfg::emit("tokio_wasm_not_wasi");
}
Expand Down
55 changes: 29 additions & 26 deletions tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,32 +434,32 @@ compile_error! {
//
// Each condition is written all(a, not(b)). This should be read as
// "if a, then we must also have b".
#[cfg(any(
all(target_arch = "wasm32", not(tokio_wasm)),
all(target_arch = "wasm64", not(tokio_wasm)),
all(target_family = "wasm", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasi)),
all(target_os = "wasi", tokio_wasm_not_wasi),
all(tokio_wasm, not(any(target_arch = "wasm32", target_arch = "wasm64"))),
all(tokio_wasm_not_wasi, not(tokio_wasm)),
all(tokio_wasi, not(tokio_wasm))
))]
compile_error!("Tokio's build script has incorrectly detected wasm.");

#[cfg(all(
not(tokio_unstable),
tokio_wasm,
any(
feature = "fs",
feature = "io-std",
feature = "net",
feature = "process",
feature = "rt-multi-thread",
feature = "signal"
)
))]
compile_error!("Only features sync,macros,io-util,rt,time are supported on wasm.");
// #[cfg(any(
// all(target_arch = "wasm32", not(tokio_wasm)),
// all(target_arch = "wasm64", not(tokio_wasm)),
// all(target_family = "wasm", not(tokio_wasm)),
// all(target_os = "wasi", not(tokio_wasm)),
// all(target_os = "wasi", not(tokio_wasi)),
// all(target_os = "wasi", tokio_wasm_not_wasi),
// all(tokio_wasm, not(any(target_arch = "wasm32", target_arch = "wasm64"))),
// all(tokio_wasm_not_wasi, not(tokio_wasm)),
// all(tokio_wasi, not(tokio_wasm))
// ))]
// compile_error!("Tokio's build script has incorrectly detected wasm.");

// #[cfg(all(
// not(tokio_unstable),
// tokio_wasm,
// any(
// feature = "fs",
// feature = "io-std",
// feature = "net",
// feature = "process",
// feature = "rt-multi-thread",
// feature = "signal"
// )
// ))]
// compile_error!("Only features sync,macros,io-util,rt,time are supported on wasm.");

// Includes re-exports used by macros.
//
Expand All @@ -481,6 +481,7 @@ pub mod net;
mod loom;
mod park;

#[cfg(not(target_os = "wasi"))]
cfg_process! {
pub mod process;
}
Expand All @@ -504,10 +505,12 @@ cfg_not_rt! {

pub(crate) mod coop;

#[cfg(not(target_os = "wasi"))]
cfg_signal! {
pub mod signal;
}

#[cfg(not(target_os = "wasi"))]
cfg_signal_internal! {
#[cfg(not(feature = "signal"))]
#[allow(dead_code)]
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ cfg_net! {
cfg_not_wasi! {
pub use tcp::socket::TcpSocket;

mod udp;
pub use udp::UdpSocket;
// mod udp;
// pub use udp::UdpSocket;
}
}

Expand Down
20 changes: 19 additions & 1 deletion tokio/src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,22 @@ impl TcpListener {
/// from a future driven by a tokio runtime, otherwise runtime can be set
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
#[track_caller]
#[cfg(not(target_os = "wasi"))]
pub fn from_std(listener: net::TcpListener) -> io::Result<TcpListener> {
let io = mio::net::TcpListener::from_std(listener);
let io = PollEvented::new(io)?;
Ok(TcpListener { io })
}

/// Creates new `TcpListener` from a `std::net::TcpListener`.
///
#[cfg(target_os = "wasi")]
pub fn from_std(listener: wasmedge_wasi_socket::TcpListener) -> io::Result<TcpListener> {
let io = mio::net::TcpListener::from_std(listener);
let io = PollEvented::new(io)?;
Ok(TcpListener { io })
}

/// Turns a [`tokio::net::TcpListener`] into a [`std::net::TcpListener`].
///
/// The returned [`std::net::TcpListener`] will have nonblocking mode set as
Expand All @@ -256,6 +266,7 @@ impl TcpListener {
/// [`tokio::net::TcpListener`]: TcpListener
/// [`std::net::TcpListener`]: std::net::TcpListener
/// [`set_nonblocking`]: fn@std::net::TcpListener::set_nonblocking
#[cfg(not(target_os = "wasi"))]
pub fn into_std(self) -> io::Result<std::net::TcpListener> {
#[cfg(unix)]
{
Expand Down Expand Up @@ -315,6 +326,13 @@ impl TcpListener {
/// Ok(())
/// }
/// ```
#[cfg(not(target_os = "wasi"))]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.io.local_addr()
}

/// Returns the local address that this listener is bound to.
#[cfg(target_os = "wasi")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.io.local_addr()
}
Expand Down Expand Up @@ -371,7 +389,7 @@ impl TcpListener {
self.io.set_ttl(ttl)
}
}

#[cfg(not(target_os = "wasi"))]
impl TryFrom<net::TcpListener> for TcpListener {
type Error = io::Error;

Expand Down
Loading

0 comments on commit 4f2e423

Please sign in to comment.