Skip to content

Commit

Permalink
add wasm32-wasip2 support
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dicej committed Oct 9, 2024
1 parent 679d765 commit 19bca4c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pin-project-lite = "0.2.11"

# Everything else is optional...
bytes = { version = "1.0.0", optional = true }
mio = { version = "1.0.1", optional = true, default-features = false }
# TODO dicej: switch to upstream once WASIp2 support is merged:
mio = { git = "https://github.com/dicej/mio", branch = "wasip2", optional = true, default-features = false }
parking_lot = { version = "0.12.0", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]

//! A runtime for writing reliable network applications without compromising speed.
//!
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,15 @@ macro_rules! cfg_not_wasi {
}
}

macro_rules! cfg_not_wasip1 {
($($item:item)*) => {
$(
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
$item
)*
}
}

macro_rules! cfg_is_wasm_not_wasi {
($($item:item)*) => {
$(
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! [`AsyncFd`]: crate::io::unix::AsyncFd

mod addr;
cfg_not_wasi! {
cfg_not_wasip1! {
#[cfg(feature = "net")]
pub(crate) use addr::to_socket_addrs;
}
Expand Down
7 changes: 5 additions & 2 deletions tokio/src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cfg_not_wasi! {
use std::time::Duration;
}

cfg_not_wasip1! {
use crate::net::{to_socket_addrs, ToSocketAddrs};
use std::future::poll_fn;
use std::time::Duration;
}

use crate::io::{AsyncRead, AsyncWrite, Interest, PollEvented, ReadBuf, Ready};
Expand Down Expand Up @@ -72,7 +75,7 @@ cfg_net! {
}

impl TcpStream {
cfg_not_wasi! {
cfg_not_wasip1! {
/// Opens a TCP connection to a remote host.
///
/// `addr` is an address of the remote host. Anything which implements the
Expand Down

0 comments on commit 19bca4c

Please sign in to comment.