Skip to content

refactor: move wasip2 implementation to wasmtime_wasi::p2 #10073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/vendor-wit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ make_vendor "wasi-io" "
[email protected]
"

make_vendor "wasi" "
make_vendor "wasi/src/p2" "
[email protected]
[email protected]
[email protected]
Expand Down
18 changes: 12 additions & 6 deletions crates/c-api/src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::ffi::{c_char, CStr};
use std::fs::File;
use std::path::Path;
use std::slice;
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
use wasmtime_wasi::p2::WasiCtxBuilder;
use wasmtime_wasi::preview1::WasiP1Ctx;

unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> {
CStr::from_ptr(path).to_str().map(Path::new).ok()
Expand Down Expand Up @@ -105,8 +106,9 @@ pub unsafe extern "C" fn wasi_config_set_stdin_file(
};

let file = tokio::fs::File::from_std(file);
let stdin_stream =
wasmtime_wasi::AsyncStdinStream::new(wasmtime_wasi::pipe::AsyncReadStream::new(file));
let stdin_stream = wasmtime_wasi::p2::AsyncStdinStream::new(
wasmtime_wasi::p2::pipe::AsyncReadStream::new(file),
);
config.builder.stdin(stdin_stream);

true
Expand All @@ -118,7 +120,7 @@ pub unsafe extern "C" fn wasi_config_set_stdin_bytes(
binary: &mut wasm_byte_vec_t,
) {
let binary = binary.take();
let binary = wasmtime_wasi::pipe::MemoryInputPipe::new(binary);
let binary = wasmtime_wasi::p2::pipe::MemoryInputPipe::new(binary);
config.builder.stdin(binary);
}

Expand All @@ -137,7 +139,9 @@ pub unsafe extern "C" fn wasi_config_set_stdout_file(
None => return false,
};

config.builder.stdout(wasmtime_wasi::OutputFile::new(file));
config
.builder
.stdout(wasmtime_wasi::p2::OutputFile::new(file));

true
}
Expand All @@ -157,7 +161,9 @@ pub unsafe extern "C" fn wasi_config_set_stderr_file(
None => return false,
};

config.builder.stderr(wasmtime_wasi::OutputFile::new(file));
config
.builder
.stderr(wasmtime_wasi::p2::OutputFile::new(file));

true
}
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/src/bin/api_reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::{Mutex, MutexGuard};

wit_bindgen::generate!({
world: "test-reactor",
path: "../wasi/wit",
path: "../wasi/src/p2/wit",
generate_all,
});

Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/src/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl WasiCtxBuilder {
}

// Much of this mod is implemented in terms of `async` methods from the
// wasmtime_wasi::sync module. These methods may be async in signature, however,
// wasmtime_wasi::p2::sync module. These methods may be async in signature, however,
// they are synchronous in implementation (always Poll::Ready on first poll)
// and perform blocking syscalls.
//
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! component::{Linker, ResourceTable},
//! Config, Engine, Result, Store,
//! };
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
//!
//! #[tokio::main]
Expand All @@ -36,7 +36,7 @@
//! });
//!
//! let mut linker = Linker::<Ctx>::new(&engine);
//! wasmtime_wasi::add_to_linker_async(&mut linker)?;
//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
//! // add `wasi-config` world's interfaces to the linker
//! wasmtime_wasi_config::add_to_linker(&mut linker, |h: &mut Ctx| {
//! WasiConfig::from(&h.wasi_config_vars)
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-config/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use wasmtime::{
component::{Component, Linker, ResourceTable},
Store,
};
use wasmtime_wasi::{
use wasmtime_wasi::p2::{
add_to_linker_async, bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView,
};
use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod generated {
require_store_data_send: true,
with: {
// Upstream package dependencies
"wasi:io": wasmtime_wasi::bindings::io,
"wasi:io": wasmtime_wasi::p2::bindings::io,

// Configure all WIT http resources to be defined types in this
// crate to use the `ResourceTable` helper methods.
Expand Down Expand Up @@ -63,7 +63,7 @@ pub mod sync {
"wasi:http": crate::bindings::http,
// sync requires the wrapper in the wasmtime_wasi crate, in
// order to have in_tokio
"wasi:io": wasmtime_wasi::bindings::sync::io,
"wasi:io": wasmtime_wasi::p2::bindings::sync::io,
},
require_store_data_send: true,
});
Expand Down
6 changes: 2 additions & 4 deletions crates/wasi-http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ use std::mem;
use std::task::{Context, Poll};
use std::{pin::Pin, sync::Arc, time::Duration};
use tokio::sync::{mpsc, oneshot};
use wasmtime_wasi::{
runtime::{poll_noop, AbortOnDropJoinHandle},
InputStream, OutputStream, Pollable, StreamError,
};
use wasmtime_wasi::p2::{InputStream, OutputStream, Pollable, StreamError};
use wasmtime_wasi::runtime::{poll_noop, AbortOnDropJoinHandle};

/// Common type for incoming bodies.
pub type HyperIncomingBody = BoxBody<Bytes, types::ErrorCode>;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bytes::Bytes;
use http_body_util::{BodyExt, Empty};
use hyper::Method;
use wasmtime::component::Resource;
use wasmtime_wasi::IoView;
use wasmtime_wasi::p2::IoView;

impl<T> outgoing_handler::Host for WasiHttpImpl<T>
where
Expand Down
72 changes: 37 additions & 35 deletions crates/wasi-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//!
//! All `bindgen!`-generated `Host` traits are implemented in terms of a
//! [`WasiHttpView`] trait which provides basic access to [`WasiHttpCtx`],
//! configuration for WASI HTTP, and a [`wasmtime_wasi::ResourceTable`], the
//! configuration for WASI HTTP, and a [`wasmtime_wasi::p2::ResourceTable`], the
//! state for all host-defined component model resources.
//!
//! The [`WasiHttpView`] trait additionally offers a few other configuration
Expand All @@ -53,7 +53,7 @@
//! `wasi:http/proxy` together
//! * Use [`add_only_http_to_linker_async`] to add only HTTP interfaces but
//! no others. This is useful when working with
//! [`wasmtime_wasi::add_to_linker_async`] for example.
//! [`wasmtime_wasi::p2::add_to_linker_async`] for example.
//! * Add individual interfaces such as with the
//! [`bindings::http::outgoing_handler::add_to_linker_get_host`] function.
//! 3. Use [`ProxyPre`](bindings::ProxyPre) to pre-instantiate a component
Expand All @@ -71,7 +71,7 @@
//! use tokio::net::TcpListener;
//! use wasmtime::component::{Component, Linker, ResourceTable};
//! use wasmtime::{Config, Engine, Result, Store};
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi_http::bindings::ProxyPre;
//! use wasmtime_wasi_http::bindings::http::types::Scheme;
//! use wasmtime_wasi_http::body::HyperOutgoingBody;
Expand Down Expand Up @@ -237,7 +237,7 @@ pub use crate::types::{
WasiHttpCtx, WasiHttpImpl, WasiHttpView, DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,
DEFAULT_OUTGOING_BODY_CHUNK_SIZE,
};
use wasmtime_wasi::IoImpl;
use wasmtime_wasi::p2::IoImpl;
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
///
/// This function will add the `async` variant of all interfaces into the
Expand All @@ -252,7 +252,7 @@ use wasmtime_wasi::IoImpl;
/// ```
/// use wasmtime::{Engine, Result, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// fn main() -> Result<()> {
Expand Down Expand Up @@ -285,20 +285,21 @@ use wasmtime_wasi::IoImpl;
/// ```
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView,
T: WasiHttpView + wasmtime_wasi::p2::WasiView,
{
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
wasmtime_wasi::bindings::io::poll::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::bindings::io::streams::add_to_linker_get_host(l, io_closure)?;
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::p2::IoImpl(t));
wasmtime_wasi::p2::bindings::io::poll::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::p2::bindings::io::error::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::p2::bindings::io::streams::add_to_linker_get_host(l, io_closure)?;

let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;
let closure =
type_annotate_wasi::<T, _>(|t| wasmtime_wasi::p2::WasiImpl(wasmtime_wasi::p2::IoImpl(t)));
wasmtime_wasi::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::random::random::add_to_linker_get_host(l, closure)?;

add_only_http_to_linker_async(l)
}
Expand All @@ -313,21 +314,21 @@ where
}
fn type_annotate_wasi<T, F>(val: F) -> F
where
F: Fn(&mut T) -> wasmtime_wasi::WasiImpl<&mut T>,
F: Fn(&mut T) -> wasmtime_wasi::p2::WasiImpl<&mut T>,
{
val
}
fn type_annotate_io<T, F>(val: F) -> F
where
F: Fn(&mut T) -> wasmtime_wasi::IoImpl<&mut T>,
F: Fn(&mut T) -> wasmtime_wasi::p2::IoImpl<&mut T>,
{
val
}

/// A slimmed down version of [`add_to_linker_async`] which only adds
/// `wasi:http` interfaces to the linker.
///
/// This is useful when using [`wasmtime_wasi::add_to_linker_async`] for
/// This is useful when using [`wasmtime_wasi::p2::add_to_linker_async`] for
/// example to avoid re-adding the same interfaces twice.
pub fn add_only_http_to_linker_async<T>(
l: &mut wasmtime::component::Linker<T>,
Expand All @@ -353,7 +354,7 @@ where
/// ```
/// use wasmtime::{Engine, Result, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// fn main() -> Result<()> {
Expand Down Expand Up @@ -384,24 +385,25 @@ where
/// ```
pub fn add_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView,
T: WasiHttpView + wasmtime_wasi::p2::WasiView,
{
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::p2::IoImpl(t));
// For the sync linker, use the definitions of poll and streams from the
// wasmtime_wasi::bindings::sync space because those are defined using in_tokio.
wasmtime_wasi::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?;
// wasmtime_wasi::p2::bindings::sync space because those are defined using in_tokio.
wasmtime_wasi::p2::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::p2::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?;
// The error interface in the wasmtime_wasi is synchronous
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, io_closure)?;
wasmtime_wasi::p2::bindings::io::error::add_to_linker_get_host(l, io_closure)?;

let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));
let closure =
type_annotate_wasi::<T, _>(|t| wasmtime_wasi::p2::WasiImpl(wasmtime_wasi::p2::IoImpl(t)));

wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::p2::bindings::random::random::add_to_linker_get_host(l, closure)?;

add_only_http_to_linker_sync(l)?;

Expand All @@ -411,7 +413,7 @@ where
/// A slimmed down version of [`add_to_linker_sync`] which only adds
/// `wasi:http` interfaces to the linker.
///
/// This is useful when using [`wasmtime_wasi::add_to_linker_sync`] for
/// This is useful when using [`wasmtime_wasi::p2::add_to_linker_sync`] for
/// example to avoid re-adding the same interfaces twice.
pub fn add_only_http_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
Expand Down
5 changes: 3 additions & 2 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use std::time::Duration;
use tokio::net::TcpStream;
use tokio::time::timeout;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, IoImpl, IoView, Pollable};
use wasmtime_wasi::p2::{IoImpl, IoView, Pollable};
use wasmtime_wasi::runtime::AbortOnDropJoinHandle;

/// Capture the state necessary for use in the wasi-http API implementation.
#[derive(Debug)]
Expand All @@ -39,7 +40,7 @@ impl WasiHttpCtx {
///
/// ```
/// use wasmtime::component::ResourceTable;
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder};
/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// struct MyState {
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::Context;
use std::any::Any;
use std::str::FromStr;
use wasmtime::component::{Resource, ResourceTable, ResourceTableError};
use wasmtime_wasi::{DynInputStream, DynOutputStream, DynPollable, IoView};
use wasmtime_wasi::p2::{DynInputStream, DynOutputStream, DynPollable, IoView};

impl<T> crate::bindings::http::types::Host for WasiHttpImpl<T>
where
Expand Down Expand Up @@ -660,7 +660,7 @@ where
&mut self,
index: Resource<HostFutureTrailers>,
) -> wasmtime::Result<Resource<DynPollable>> {
wasmtime_wasi::subscribe(self.table(), index)
wasmtime_wasi::p2::subscribe(self.table(), index)
}

fn get(
Expand Down Expand Up @@ -881,7 +881,7 @@ where
&mut self,
id: Resource<HostFutureIncomingResponse>,
) -> wasmtime::Result<Resource<DynPollable>> {
wasmtime_wasi::subscribe(self.table(), id)
wasmtime_wasi::p2::subscribe(self.table(), id)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/tests/all/async_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::bindings::Command;
use wasmtime_wasi::p2::bindings::Command;

foreach_http!(assert_test_exists);

Expand All @@ -12,7 +12,7 @@ async fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker_async(&mut linker)?;
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker)?;
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
let result = command.wasi_cli_run().call_run(&mut store).await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use wasmtime::{
component::{Component, Linker, ResourceTable},
Config, Engine, Store,
};
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, IoView, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::p2::{pipe::MemoryOutputPipe, IoView, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{
bindings::http::types::{ErrorCode, Scheme},
body::HyperOutgoingBody,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/tests/all/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::bindings::sync::Command;
use wasmtime_wasi::p2::bindings::sync::Command;

foreach_http!(assert_test_exists);

Expand All @@ -11,7 +11,7 @@ fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker)?;
let command = Command::instantiate(&mut store, &component, &linker)?;
let result = command.wasi_cli_run().call_run(&mut store)?;
Expand Down
Loading