Skip to content

Commit 19bca4c

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 <joel.dice@fermyon.com>
1 parent 679d765 commit 19bca4c

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

tokio/Cargo.toml

Lines changed: 2 additions & 1 deletion
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.0.0", 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", optional = true, default-features = false }
9697
parking_lot = { version = "0.12.0", optional = true }
9798

9899
[target.'cfg(not(target_family = "wasm"))'.dependencies]

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, feature(doc_cfg))]
2020
#![cfg_attr(docsrs, allow(unused_attributes))]
2121
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
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
@@ -594,6 +594,15 @@ macro_rules! cfg_not_wasi {
594594
}
595595
}
596596

597+
macro_rules! cfg_not_wasip1 {
598+
($($item:item)*) => {
599+
$(
600+
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
601+
$item
602+
)*
603+
}
604+
}
605+
597606
macro_rules! cfg_is_wasm_not_wasi {
598607
($($item:item)*) => {
599608
$(

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};
@@ -72,7 +75,7 @@ cfg_net! {
7275
}
7376

7477
impl TcpStream {
75-
cfg_not_wasi! {
78+
cfg_not_wasip1! {
7679
/// Opens a TCP connection to a remote host.
7780
///
7881
/// `addr` is an address of the remote host. Anything which implements the

0 commit comments

Comments
 (0)