Skip to content

Commit 630f6d0

Browse files
committed
add wasm32-wasip2 support
This adds support for the new `wasm32-wasip2` target platform, which includes more extensive support for sockets than `wasm32-wasip1` (formerly known as `wasm32-wasi`). The bulk of the changes are in tokio-rs/mio#1836. This patch just tweaks a few `cfg` directives to indicate `wasm32-wasip2`'s additional capabilities. In the future, we could consider adding support for `ToSocketAddrs`. WASIp2 natively supports asynchronous DNS lookups and is single threaded, whereas Tokio currently assumes DNS lookups are blocking and require multithreading to emulate async lookups. A WASIp2-specific implementation could do the lookup directly without multithreading. I've tested this end-to-end using https://github.com/dicej/wasi-sockets-tests, which includes smoke tests for `mio`, `tokio`, `tokio-postgres`, etc. I'd also be happy to add tests to this repo if appropriate; it would require adding a dev-dependency on e.g. `wasmtime` to actually run the test cases. Signed-off-by: Joel Dice <[email protected]>
1 parent 99a03a5 commit 630f6d0

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

tokio/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ pin-project-lite = "0.2.11"
9292

9393
# Everything else is optional...
9494
bytes = { version = "1.2.1", optional = true }
95-
mio = { version = "1.0.1", optional = true, default-features = false }
95+
# TODO dicej: switch to upstream once WASIp2 support is merged:
96+
mio = { git = "https://github.com/dicej/mio", branch = "wasip2-draft", optional = true, default-features = false }
9697
parking_lot = { version = "0.12.0", optional = true }
9798

9899
[target.'cfg(not(target_family = "wasm"))'.dependencies]
@@ -106,7 +107,7 @@ tracing = { version = "0.1.29", default-features = false, features = ["std"], op
106107
[target.'cfg(all(tokio_uring, target_os = "linux"))'.dependencies]
107108
io-uring = { version = "0.7.6", default-features = false }
108109
libc = { version = "0.2.168" }
109-
mio = { version = "1.0.1", default-features = false, features = ["os-poll", "os-ext"] }
110+
mio = { git = "https://github.com/dicej/mio", branch = "wasip2-draft", optional = true, default-features = false }
110111
slab = "0.4.9"
111112

112113
# Currently unstable. The API exposed by these features may be broken at any time.

tokio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![cfg_attr(docsrs, allow(unused_attributes))]
2020
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
2121
#![cfg_attr(windows, allow(rustdoc::broken_intra_doc_links))]
22+
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]
2223

2324
//! A runtime for writing reliable network applications without compromising speed.
2425
//!

tokio/src/macros/cfg.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,15 @@ macro_rules! cfg_not_wasi {
647647
}
648648
}
649649

650+
macro_rules! cfg_not_wasip1 {
651+
($($item:item)*) => {
652+
$(
653+
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
654+
$item
655+
)*
656+
}
657+
}
658+
650659
macro_rules! cfg_is_wasm_not_wasi {
651660
($($item:item)*) => {
652661
$(

tokio/src/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! [`AsyncFd`]: crate::io::unix::AsyncFd
3030
3131
mod addr;
32-
cfg_not_wasi! {
32+
cfg_not_wasip1! {
3333
#[cfg(feature = "net")]
3434
pub(crate) use addr::to_socket_addrs;
3535
}

tokio/src/net/tcp/stream.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
cfg_not_wasi! {
2+
use std::time::Duration;
3+
}
4+
5+
cfg_not_wasip1! {
26
use crate::net::{to_socket_addrs, ToSocketAddrs};
37
use std::future::poll_fn;
4-
use std::time::Duration;
58
}
69

710
use crate::io::{AsyncRead, AsyncWrite, Interest, PollEvented, ReadBuf, Ready};
@@ -73,7 +76,7 @@ cfg_net! {
7376
}
7477

7578
impl TcpStream {
76-
cfg_not_wasi! {
79+
cfg_not_wasip1! {
7780
/// Opens a TCP connection to a remote host.
7881
///
7982
/// `addr` is an address of the remote host. Anything which implements the

0 commit comments

Comments
 (0)