diff --git a/ci/vendor-wit.sh b/ci/vendor-wit.sh index 33c14b8730f4..c46ba257e746 100755 --- a/ci/vendor-wit.sh +++ b/ci/vendor-wit.sh @@ -40,7 +40,7 @@ make_vendor "wasi-io" " io@v0.2.3 " -make_vendor "wasi" " +make_vendor "wasi/src/p2" " cli@v0.2.3 clocks@v0.2.3 filesystem@v0.2.3 diff --git a/crates/c-api/src/wasi.rs b/crates/c-api/src/wasi.rs index baa612c6a0fc..824c0f7be68c 100644 --- a/crates/c-api/src/wasi.rs +++ b/crates/c-api/src/wasi.rs @@ -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() @@ -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 @@ -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); } @@ -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 } @@ -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 } diff --git a/crates/test-programs/src/bin/api_reactor.rs b/crates/test-programs/src/bin/api_reactor.rs index 360c96fab61b..3f8b420b6e6b 100644 --- a/crates/test-programs/src/bin/api_reactor.rs +++ b/crates/test-programs/src/bin/api_reactor.rs @@ -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, }); diff --git a/crates/wasi-common/src/tokio/mod.rs b/crates/wasi-common/src/tokio/mod.rs index ff80197f28f2..0551cccf8d36 100644 --- a/crates/wasi-common/src/tokio/mod.rs +++ b/crates/wasi-common/src/tokio/mod.rs @@ -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. // diff --git a/crates/wasi-config/src/lib.rs b/crates/wasi-config/src/lib.rs index ec6f3bd595b0..47994a3254e5 100644 --- a/crates/wasi-config/src/lib.rs +++ b/crates/wasi-config/src/lib.rs @@ -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] @@ -36,7 +36,7 @@ //! }); //! //! let mut linker = Linker::::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) diff --git a/crates/wasi-config/tests/main.rs b/crates/wasi-config/tests/main.rs index 08619d30ec21..24819f559e21 100644 --- a/crates/wasi-config/tests/main.rs +++ b/crates/wasi-config/tests/main.rs @@ -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}; diff --git a/crates/wasi-http/src/bindings.rs b/crates/wasi-http/src/bindings.rs index f88fd1216f7f..34bf5efbb76e 100644 --- a/crates/wasi-http/src/bindings.rs +++ b/crates/wasi-http/src/bindings.rs @@ -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. @@ -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, }); diff --git a/crates/wasi-http/src/body.rs b/crates/wasi-http/src/body.rs index 8adab1352a22..cb82701bc77f 100644 --- a/crates/wasi-http/src/body.rs +++ b/crates/wasi-http/src/body.rs @@ -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; diff --git a/crates/wasi-http/src/http_impl.rs b/crates/wasi-http/src/http_impl.rs index 8850b63f29c6..ace2629b5f43 100644 --- a/crates/wasi-http/src/http_impl.rs +++ b/crates/wasi-http/src/http_impl.rs @@ -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 outgoing_handler::Host for WasiHttpImpl where diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rs index c9bd946c2e55..e046c64de825 100644 --- a/crates/wasi-http/src/lib.rs +++ b/crates/wasi-http/src/lib.rs @@ -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 @@ -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 @@ -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; @@ -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 @@ -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<()> { @@ -285,20 +285,21 @@ use wasmtime_wasi::IoImpl; /// ``` pub fn add_to_linker_async(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> where - T: WasiHttpView + wasmtime_wasi::WasiView, + T: WasiHttpView + wasmtime_wasi::p2::WasiView, { - let io_closure = type_annotate_io::(|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| 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| 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| 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) } @@ -313,13 +314,13 @@ where } fn type_annotate_wasi(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(val: F) -> F where - F: Fn(&mut T) -> wasmtime_wasi::IoImpl<&mut T>, + F: Fn(&mut T) -> wasmtime_wasi::p2::IoImpl<&mut T>, { val } @@ -327,7 +328,7 @@ where /// 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( l: &mut wasmtime::component::Linker, @@ -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<()> { @@ -384,24 +385,25 @@ where /// ``` pub fn add_to_linker_sync(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> where - T: WasiHttpView + wasmtime_wasi::WasiView, + T: WasiHttpView + wasmtime_wasi::p2::WasiView, { - let io_closure = type_annotate_io::(|t| wasmtime_wasi::IoImpl(t)); + let io_closure = type_annotate_io::(|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| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t))); + let closure = + type_annotate_wasi::(|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)?; @@ -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(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> where diff --git a/crates/wasi-http/src/types.rs b/crates/wasi-http/src/types.rs index f510a065c291..5f75066488de 100644 --- a/crates/wasi-http/src/types.rs +++ b/crates/wasi-http/src/types.rs @@ -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)] @@ -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 { diff --git a/crates/wasi-http/src/types_impl.rs b/crates/wasi-http/src/types_impl.rs index 8f27143467a2..0e04f076234b 100644 --- a/crates/wasi-http/src/types_impl.rs +++ b/crates/wasi-http/src/types_impl.rs @@ -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 crate::bindings::http::types::Host for WasiHttpImpl where @@ -660,7 +660,7 @@ where &mut self, index: Resource, ) -> wasmtime::Result> { - wasmtime_wasi::subscribe(self.table(), index) + wasmtime_wasi::p2::subscribe(self.table(), index) } fn get( @@ -881,7 +881,7 @@ where &mut self, id: Resource, ) -> wasmtime::Result> { - wasmtime_wasi::subscribe(self.table(), id) + wasmtime_wasi::p2::subscribe(self.table(), id) } } diff --git a/crates/wasi-http/tests/all/async_.rs b/crates/wasi-http/tests/all/async_.rs index 4ea9c7c0782d..ef77e7352ca0 100644 --- a/crates/wasi-http/tests/all/async_.rs +++ b/crates/wasi-http/tests/all/async_.rs @@ -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); @@ -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?; diff --git a/crates/wasi-http/tests/all/main.rs b/crates/wasi-http/tests/all/main.rs index 395d67ab73ee..aae5c4cdcc51 100644 --- a/crates/wasi-http/tests/all/main.rs +++ b/crates/wasi-http/tests/all/main.rs @@ -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, diff --git a/crates/wasi-http/tests/all/sync.rs b/crates/wasi-http/tests/all/sync.rs index 64147a5a0e30..77e4c2940c3c 100644 --- a/crates/wasi-http/tests/all/sync.rs +++ b/crates/wasi-http/tests/all/sync.rs @@ -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); @@ -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)?; diff --git a/crates/wasi-keyvalue/src/lib.rs b/crates/wasi-keyvalue/src/lib.rs index ce8d5b276984..d4d69cad51bd 100644 --- a/crates/wasi-keyvalue/src/lib.rs +++ b/crates/wasi-keyvalue/src/lib.rs @@ -20,7 +20,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_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder}; //! //! #[tokio::main] @@ -36,7 +36,7 @@ //! }); //! //! let mut linker = Linker::::new(&engine); -//! wasmtime_wasi::add_to_linker_async(&mut linker)?; +//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; //! // add `wasi-keyvalue` world's interfaces to the linker //! wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| { //! WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table) diff --git a/crates/wasi-keyvalue/tests/main.rs b/crates/wasi-keyvalue/tests/main.rs index 5d0b6bf4a07e..17817e90487e 100644 --- a/crates/wasi-keyvalue/tests/main.rs +++ b/crates/wasi-keyvalue/tests/main.rs @@ -4,7 +4,7 @@ use wasmtime::{ component::{Component, Linker, ResourceTable}, Store, }; -use wasmtime_wasi::{bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::p2::{bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView}; use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder}; struct Ctx { @@ -32,7 +32,7 @@ async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> { let component = Component::from_file(&engine, path)?; 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_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| { WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table) })?; diff --git a/crates/wasi-nn/tests/exec/wit.rs b/crates/wasi-nn/tests/exec/wit.rs index 21a756c4ddf8..9a057a3ab6b5 100644 --- a/crates/wasi-nn/tests/exec/wit.rs +++ b/crates/wasi-nn/tests/exec/wit.rs @@ -4,8 +4,9 @@ use anyhow::{anyhow, Result}; use std::path::Path; use wasmtime::component::{Component, Linker, ResourceTable}; use wasmtime::{Config, Engine, Store}; -use wasmtime_wasi::bindings::sync::Command; -use wasmtime_wasi::{DirPerms, FilePerms, WasiCtx, WasiCtxBuilder}; +use wasmtime_wasi::p2::bindings::sync::Command; +use wasmtime_wasi::p2::{WasiCtx, WasiCtxBuilder}; +use wasmtime_wasi::{DirPerms, FilePerms}; use wasmtime_wasi_nn::wit::WasiNnView; use wasmtime_wasi_nn::{wit::WasiNnCtx, Backend, InMemoryRegistry}; @@ -19,7 +20,7 @@ pub fn run(path: &str, backend: Backend, preload_model: bool) -> Result<()> { wasmtime_wasi_nn::wit::add_to_linker(&mut linker, |c: &mut Ctx| { WasiNnView::new(&mut c.table, &mut c.wasi_nn) })?; - wasmtime_wasi::add_to_linker_sync(&mut linker)?; + wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; let module = Component::from_file(&engine, path)?; let mut store = Store::new(&engine, Ctx::new(&artifacts_dir(), preload_model, backend)?); let command = Command::instantiate(&mut store, &module, &linker)?; @@ -62,12 +63,12 @@ impl Ctx { } } -impl wasmtime_wasi::IoView for Ctx { +impl wasmtime_wasi::p2::IoView for Ctx { fn table(&mut self) -> &mut ResourceTable { &mut self.table } } -impl wasmtime_wasi::WasiView for Ctx { +impl wasmtime_wasi::p2::WasiView for Ctx { fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi } diff --git a/crates/wasi-nn/tests/exec/witx.rs b/crates/wasi-nn/tests/exec/witx.rs index 21feea1dd641..803a62ea9e1d 100644 --- a/crates/wasi-nn/tests/exec/witx.rs +++ b/crates/wasi-nn/tests/exec/witx.rs @@ -3,7 +3,9 @@ use crate::check::artifacts_dir; use anyhow::Result; use std::path::Path; use wasmtime::{Config, Engine, Linker, Module, Store}; -use wasmtime_wasi::{preview1::WasiP1Ctx, DirPerms, FilePerms, WasiCtxBuilder}; +use wasmtime_wasi::p2::WasiCtxBuilder; +use wasmtime_wasi::preview1::WasiP1Ctx; +use wasmtime_wasi::{DirPerms, FilePerms}; use wasmtime_wasi_nn::{witx::WasiNnCtx, Backend, InMemoryRegistry}; /// Run a wasi-nn test program. This is modeled after diff --git a/crates/wasi-preview1-component-adapter/src/lib.rs b/crates/wasi-preview1-component-adapter/src/lib.rs index c8267982d342..8aeca7cb4573 100644 --- a/crates/wasi-preview1-component-adapter/src/lib.rs +++ b/crates/wasi-preview1-component-adapter/src/lib.rs @@ -52,7 +52,7 @@ use crate::descriptors::{Descriptor, Descriptors, StreamType, Streams}; pub mod bindings { #[cfg(feature = "command")] wit_bindgen_rust_macro::generate!({ - path: "../wasi/wit", + path: "../wasi/src/p2/wit", world: "wasi:cli/command", raw_strings, runtime_path: "crate::bindings::wit_bindgen_rt_shim", @@ -69,7 +69,7 @@ pub mod bindings { #[cfg(feature = "reactor")] wit_bindgen_rust_macro::generate!({ - path: "../wasi/wit", + path: "../wasi/src/p2/wit", world: "wasi:cli/imports", raw_strings, runtime_path: "crate::bindings::wit_bindgen_rt_shim", diff --git a/crates/wasi-tls/src/lib.rs b/crates/wasi-tls/src/lib.rs index 583d37c2e8e4..36fdb535d7ec 100644 --- a/crates/wasi-tls/src/lib.rs +++ b/crates/wasi-tls/src/lib.rs @@ -8,7 +8,7 @@ //! # An example of how to configure [wasi-tls] is the following: //! //! ```rust -//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; +//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; //! use wasmtime::{ //! component::{Linker, ResourceTable}, //! Store, Engine, Result, Config @@ -50,7 +50,7 @@ //! // Set up wasi-cli //! let mut store = Store::new(&engine, ctx); //! let mut linker = Linker::new(&engine); -//! wasmtime_wasi::add_to_linker_async(&mut linker)?; +//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; //! //! // Add wasi-tls types and turn on the feature in linker //! let mut opts = LinkOptions::default(); @@ -82,25 +82,22 @@ use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; use tokio::sync::Mutex; use tokio_rustls::client::TlsStream; use wasmtime::component::{Resource, ResourceTable}; -use wasmtime_wasi::pipe::AsyncReadStream; -use wasmtime_wasi::runtime::AbortOnDropJoinHandle; -use wasmtime_wasi::OutputStream; -use wasmtime_wasi::{ - async_trait, - bindings::io::{ - error::Error as HostIoError, - poll::Pollable as HostPollable, - streams::{InputStream as BoxInputStream, OutputStream as BoxOutputStream}, - }, - Pollable, StreamError, +use wasmtime_wasi::async_trait; +use wasmtime_wasi::p2::bindings::io::{ + error::Error as HostIoError, + poll::Pollable as HostPollable, + streams::{InputStream as BoxInputStream, OutputStream as BoxOutputStream}, }; +use wasmtime_wasi::p2::pipe::AsyncReadStream; +use wasmtime_wasi::p2::{OutputStream, Pollable, StreamError}; +use wasmtime_wasi::runtime::AbortOnDropJoinHandle; mod gen_ { wasmtime::component::bindgen!({ path: "wit", world: "wasi:tls/imports", with: { - "wasi:io": wasmtime_wasi::bindings::io, + "wasi:io": wasmtime_wasi::p2::bindings::io, "wasi:tls/types/client-connection": super::ClientConnection, "wasi:tls/types/client-handshake": super::ClientHandShake, "wasi:tls/types/future-client-streams": super::FutureClientStreams, @@ -158,7 +155,7 @@ enum TlsError { /// A failure indicated by the underlying transport stream as /// [`StreamError::LastOperationFailed`]. - Io(wasmtime_wasi::IoError), + Io(wasmtime_wasi::p2::IoError), /// A TLS protocol error occurred. Tls(rustls::Error), @@ -277,7 +274,7 @@ impl<'a> generated::types::HostFutureClientStreams for WasiTlsCtx<'a> { &mut self, this: wasmtime::component::Resource, ) -> wasmtime::Result> { - wasmtime_wasi::subscribe(self.table, this) + wasmtime_wasi::p2::subscribe(self.table, this) } fn get( @@ -317,7 +314,7 @@ impl<'a> generated::types::HostFutureClientStreams for WasiTlsCtx<'a> { return Ok(Some(Ok(Err(error)))); } Err(TlsError::Tls(e)) => { - let error = self.table.push(wasmtime_wasi::IoError::new(e))?; + let error = self.table.push(wasmtime_wasi::p2::IoError::new(e))?; return Ok(Some(Ok(Err(error)))); } }; diff --git a/crates/wasi-tls/tests/main.rs b/crates/wasi-tls/tests/main.rs index 6919a73ead4e..6463fac4ead4 100644 --- a/crates/wasi-tls/tests/main.rs +++ b/crates/wasi-tls/tests/main.rs @@ -4,7 +4,7 @@ use wasmtime::{ component::{Component, Linker, ResourceTable}, Store, }; -use wasmtime_wasi::{bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::p2::{bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView}; use wasmtime_wasi_tls::{LinkOptions, WasiTlsCtx}; struct Ctx { @@ -31,7 +31,7 @@ async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> { let component = Component::from_file(&engine, path)?; let mut linker = Linker::new(&engine); - wasmtime_wasi::add_to_linker_async(&mut linker)?; + wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; let mut opts = LinkOptions::default(); opts.tls(true); wasmtime_wasi_tls::add_to_linker(&mut linker, &mut opts, |h: &mut Ctx| { diff --git a/crates/wasi/src/fs.rs b/crates/wasi/src/fs.rs new file mode 100644 index 000000000000..c363a49ebe26 --- /dev/null +++ b/crates/wasi/src/fs.rs @@ -0,0 +1,32 @@ +bitflags::bitflags! { + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + pub struct FilePerms: usize { + const READ = 0b1; + const WRITE = 0b10; + } +} + +bitflags::bitflags! { + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + pub struct OpenMode: usize { + const READ = 0b1; + const WRITE = 0b10; + } +} + +bitflags::bitflags! { + /// Permission bits for operating on a directory. + /// + /// Directories can be limited to being readonly. This will restrict what + /// can be done with them, for example preventing creation of new files. + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + pub struct DirPerms: usize { + /// This directory can be read, for example its entries can be iterated + /// over and files can be opened. + const READ = 0b1; + + /// This directory can be mutated, for example by creating new files + /// within it. + const MUTATE = 0b10; + } +} diff --git a/crates/wasi/src/lib.rs b/crates/wasi/src/lib.rs index 6c77a27b90f5..876447058585 100644 --- a/crates/wasi/src/lib.rs +++ b/crates/wasi/src/lib.rs @@ -1,272 +1,33 @@ +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![expect(clippy::allow_attributes_without_reason, reason = "crate not migrated")] + //! # Wasmtime's WASI Implementation //! -//! -//! This crate provides a Wasmtime host implementation of WASI 0.2 (aka WASIp2 -//! aka Preview 2) and WASI 0.1 (aka WASIp1 aka Preview 1). WASI is implemented -//! with the Rust crates [`tokio`] and [`cap-std`] primarily, meaning that +//! This crate provides a Wasmtime host implementations of different versions of WASI. +//! WASI is implemented with the Rust crates [`tokio`] and [`cap-std`](cap_std) primarily, meaning that //! operations are implemented in terms of their native platform equivalents by //! default. //! -//! For components and WASIp2, continue reading below. For WASIp1 and core -//! modules, see the [`preview1`] module documentation. -//! -//! # WASIp2 interfaces -//! -//! This crate contains implementations of the following interfaces: -//! -//! * [`wasi:cli/environment`] -//! * [`wasi:cli/exit`] -//! * [`wasi:cli/stderr`] -//! * [`wasi:cli/stdin`] -//! * [`wasi:cli/stdout`] -//! * [`wasi:cli/terminal-input`] -//! * [`wasi:cli/terminal-output`] -//! * [`wasi:cli/terminal-stderr`] -//! * [`wasi:cli/terminal-stdin`] -//! * [`wasi:cli/terminal-stdout`] -//! * [`wasi:clocks/monotonic-clock`] -//! * [`wasi:clocks/wall-clock`] -//! * [`wasi:filesystem/preopens`] -//! * [`wasi:filesystem/types`] -//! * [`wasi:random/insecure-seed`] -//! * [`wasi:random/insecure`] -//! * [`wasi:random/random`] -//! * [`wasi:sockets/instance-network`] -//! * [`wasi:sockets/ip-name-lookup`] -//! * [`wasi:sockets/network`] -//! * [`wasi:sockets/tcp-create-socket`] -//! * [`wasi:sockets/tcp`] -//! * [`wasi:sockets/udp-create-socket`] -//! * [`wasi:sockets/udp`] -//! -//! All traits are implemented in terms of a [`WasiView`] trait which provides -//! access to [`WasiCtx`], which defines the configuration for WASI. -//! The [`WasiView`] trait imples [`IoView`], which provides access to a common -//! [`ResourceTable`], which owns all host-defined component model resources. -//! -//! The [`wasmtime-wasi-io`] crate contains implementations of the -//! following interfaces, and this crate reuses those implementations: -//! -//! * [`wasi:io/error`] -//! * [`wasi:io/poll`] -//! * [`wasi:io/streams`] -//! -//! These traits are implemented in terms of a [`IoView`] trait, which only -//! provides access to a common [`ResourceTable`]. All aspects of -//! `wasmtime-wasi-io` that are used by this crate are re-exported. Unless you -//! are implementing other host functionality that needs to interact with the -//! WASI scheduler and don't want to use other functionality provided by -//! `wasmtime-wasi`, you don't need to take a direct dependency on -//! `wasmtime-wasi-io`. -//! -//! # Generated Bindings -//! -//! This crate uses [`wasmtime::component::bindgen!`] to generate bindings for -//! all WASI interfaces. Raw bindings are available in the [`bindings`] module -//! of this crate. Downstream users can either implement these traits themselves -//! or you can use the built-in implementations in this crate for -//! `WasiImpl`. -//! -//! # The `WasiView` trait -//! -//! This crate's implementation of WASI is done in terms of an implementation of -//! [`WasiView`]. This trait provides a "view" into WASI-related state that is -//! contained within a [`Store`](wasmtime::Store). [`WasiView`] implies the -//! [`IoView`] trait, which provides access to common [`ResourceTable`] which -//! owns all host-implemented component model resources. -//! -//! For all of the generated bindings in this crate (Host traits), -//! implementations are provided looking like: -//! -//! ``` -//! # use wasmtime_wasi::WasiImpl; -//! # trait WasiView {} -//! # mod bindings { pub mod wasi { pub trait Host {} } } -//! impl bindings::wasi::Host for WasiImpl { -//! // ... -//! } -//! ``` -//! -//! The [`add_to_linker_sync`] and [`add_to_linker_async`] function then require -//! that `T: WasiView` with [`Linker`](wasmtime::component::Linker). -//! -//! To implement the [`WasiView`] and [`IoView`] trait you will first select a -//! `T` to put in `Store` (typically, by defining your own struct). -//! Somewhere within `T` you'll store: -//! -//! * [`ResourceTable`] - created through default constructors. -//! * [`WasiCtx`] - created through [`WasiCtxBuilder`]. -//! -//! You'll then write implementations of the [`IoView`] and [`WasiView`] -//! traits to access those items in your `T`. For example: -//! ``` -//! use wasmtime::component::ResourceTable; -//! use wasmtime_wasi::{WasiCtx, IoView, WasiView}; -//! struct MyCtx { -//! table: ResourceTable, -//! wasi: WasiCtx, -//! } -//! impl IoView for MyCtx { -//! fn table(&mut self) -> &mut ResourceTable { -//! &mut self.table -//! } -//! } -//! impl WasiView for MyCtx { -//! fn ctx(&mut self) -> &mut WasiCtx { -//! &mut self.wasi -//! } -//! } -//! -//! ``` -//! -//! # Async and Sync -//! -//! As of WASI0.2, WASI functions are not blocking from WebAssembly's point of -//! view: a WebAssembly call into these functions returns when they are -//! complete. -//! -//! This crate provides an implementation of those functions in the host, -//! where for some functions, it is appropriate to implement them using -//! async Rust and the Tokio executor, so that the host implementation can be -//! nonblocking when Wasmtime's [`Config::async_support`][async] is set. -//! Synchronous wrappers are provided for all async implementations, which -//! creates a private Tokio executor. -//! -//! Users can choose between these modes of implementation using variants -//! of the add_to_linker functions: -//! -//! * For non-async users (the default of `Config`), use [`add_to_linker_sync`]. -//! * For async users, use [`add_to_linker_async`]. -//! -//! Note that bindings are generated once for async and once for sync. Most -//! interfaces do not change, however, so only interfaces with blocking -//! functions have bindings generated twice. Bindings are organized as: -//! -//! * [`bindings`] - default location of all bindings, blocking functions are -//! `async` -//! * [`bindings::sync`] - blocking interfaces have synchronous versions here. -//! -//! # Crate-specific traits -//! -//! This crate's default implementation of WASI bindings to native primitives -//! for the platform that it is compiled for. For example opening a TCP socket -//! uses the native platform to open a TCP socket (so long as [`WasiCtxBuilder`] -//! allows it). There are a few important traits, however, that are specific to -//! this crate. -//! -//! * [`InputStream`] and [`OutputStream`] - these are the host traits -//! behind the WASI `input-stream` and `output-stream` types in the -//! `wasi:io/streams` interface. These enable embedders to build their own -//! custom stream and insert them into a [`ResourceTable`] (as a boxed trait -//! object, see [`DynInputStream`] and [`DynOutputStream`]) to be used from -//! wasm. -//! -//! * [`Pollable`] - this trait enables building arbitrary logic to get hooked -//! into a `pollable` resource from `wasi:io/poll`. A pollable resource is -//! created through the [`subscribe`] function. -//! -//! * [`HostWallClock`] and [`HostMonotonicClock`] are used in conjunction with -//! [`WasiCtxBuilder::wall_clock`] and [`WasiCtxBuilder::monotonic_clock`] if -//! the defaults host's clock should not be used. -//! -//! * [`StdinStream`] and [`StdoutStream`] are used to provide custom -//! stdin/stdout streams if they're not inherited (or null, which is the -//! default). -//! -//! These traits enable embedders to customize small portions of WASI interfaces -//! provided while still providing all other interfaces. -//! -//! # Examples -//! -//! Usage of this crate is done through a few steps to get everything hooked up: -//! -//! 1. First implement [`IoView`] and [`WasiView`] for your type which is the -//! `T` in `Store`. -//! 2. Add WASI interfaces to a `wasmtime::component::Linker`. This is either -//! done through top-level functions like [`add_to_linker_sync`] or through -//! individual `add_to_linker` functions in generated bindings throughout -//! this crate. -//! 3. Create a [`WasiCtx`] for each `Store` through [`WasiCtxBuilder`]. Each -//! WASI context is "null" or "empty" by default, so items must be explicitly -//! added to get accessed by wasm (such as env vars or program arguments). -//! 4. Use the previous `Linker` to instantiate a `Component` within a -//! `Store`. -//! -//! For examples see each of [`WasiView`], [`WasiCtx`], [`WasiCtxBuilder`], -//! [`add_to_linker_sync`], and [`bindings::Command`]. -//! -//! [`wasmtime::component::bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html -//! [`tokio`]: https://crates.io/crates/tokio -//! [`cap-std`]: https://crates.io/crates/cap-std -//! [`wasmtime-wasi-io`]: https://crates.io/crates/wasmtime-wasi-io -//! [`wasi:cli/environment`]: bindings::cli::environment::Host -//! [`wasi:cli/exit`]: bindings::cli::exit::Host -//! [`wasi:cli/stderr`]: bindings::cli::stderr::Host -//! [`wasi:cli/stdin`]: bindings::cli::stdin::Host -//! [`wasi:cli/stdout`]: bindings::cli::stdout::Host -//! [`wasi:cli/terminal-input`]: bindings::cli::terminal_input::Host -//! [`wasi:cli/terminal-output`]: bindings::cli::terminal_output::Host -//! [`wasi:cli/terminal-stdin`]: bindings::cli::terminal_stdin::Host -//! [`wasi:cli/terminal-stdout`]: bindings::cli::terminal_stdout::Host -//! [`wasi:cli/terminal-stderr`]: bindings::cli::terminal_stderr::Host -//! [`wasi:clocks/monotonic-clock`]: bindings::clocks::monotonic_clock::Host -//! [`wasi:clocks/wall-clock`]: bindings::clocks::wall_clock::Host -//! [`wasi:filesystem/preopens`]: bindings::filesystem::preopens::Host -//! [`wasi:filesystem/types`]: bindings::filesystem::types::Host -//! [`wasi:io/error`]: wasmtime_wasi_io::bindings::wasi::io::error::Host -//! [`wasi:io/poll`]: wasmtime_wasi_io::bindings::wasi::io::poll::Host -//! [`wasi:io/streams`]: wasmtime_wasi_io::bindings::wasi::io::streams::Host -//! [`wasi:random/insecure-seed`]: bindings::random::insecure_seed::Host -//! [`wasi:random/insecure`]: bindings::random::insecure::Host -//! [`wasi:random/random`]: bindings::random::random::Host -//! [`wasi:sockets/instance-network`]: bindings::sockets::instance_network::Host -//! [`wasi:sockets/ip-name-lookup`]: bindings::sockets::ip_name_lookup::Host -//! [`wasi:sockets/network`]: bindings::sockets::network::Host -//! [`wasi:sockets/tcp-create-socket`]: bindings::sockets::tcp_create_socket::Host -//! [`wasi:sockets/tcp`]: bindings::sockets::tcp::Host -//! [`wasi:sockets/udp-create-socket`]: bindings::sockets::udp_create_socket::Host -//! [`wasi:sockets/udp`]: bindings::sockets::udp::Host -//! [async]: https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.async_support -//! [`ResourceTable`]: wasmtime::component::ResourceTable +//! For components and WASIp2, see [`p2`]. +//! For WASIp1 and core modules, see the [`preview1`] module documentation. -#![cfg_attr(docsrs, feature(doc_auto_cfg))] -#![expect(clippy::allow_attributes_without_reason, reason = "crate not migrated")] - -use wasmtime::component::Linker; - -pub mod bindings; mod clocks; -mod ctx; mod error; -mod filesystem; -mod host; -mod ip_name_lookup; -mod network; -pub mod pipe; -mod poll; +mod fs; +mod net; +pub mod p2; #[cfg(feature = "preview1")] pub mod preview0; #[cfg(feature = "preview1")] pub mod preview1; mod random; pub mod runtime; -mod stdio; -mod tcp; -mod udp; -mod view; -mod write_stream; pub use self::clocks::{HostMonotonicClock, HostWallClock}; -pub use self::ctx::{WasiCtx, WasiCtxBuilder}; pub use self::error::{I32Exit, TrappableError}; -pub use self::filesystem::{DirPerms, FileInputStream, FilePerms, FsError, FsResult}; -pub use self::network::{Network, SocketAddrUse, SocketError, SocketResult}; +pub use self::fs::{DirPerms, FilePerms, OpenMode}; +pub use self::net::{Network, SocketAddrUse}; pub use self::random::{thread_rng, Deterministic}; -pub use self::stdio::{ - stderr, stdin, stdout, AsyncStdinStream, AsyncStdoutStream, IsATTY, OutputFile, Stderr, Stdin, - StdinStream, Stdout, StdoutStream, -}; -pub use self::view::{WasiImpl, WasiView}; #[doc(no_inline)] pub use async_trait::async_trait; #[doc(no_inline)] @@ -275,230 +36,3 @@ pub use cap_fs_ext::SystemTimeSpec; pub use cap_rand::RngCore; #[doc(no_inline)] pub use wasmtime::component::{ResourceTable, ResourceTableError}; -// These contents of wasmtime-wasi-io are re-exported by this crate for compatibility: -// they were originally defined in this crate before being factored out, and many -// users of this crate depend on them at these names. -pub use wasmtime_wasi_io::poll::{subscribe, DynFuture, DynPollable, MakeFuture, Pollable}; -pub use wasmtime_wasi_io::streams::{ - DynInputStream, DynOutputStream, Error as IoError, InputStream, OutputStream, StreamError, - StreamResult, -}; -pub use wasmtime_wasi_io::{IoImpl, IoView}; - -/// Add all WASI interfaces from this crate into the `linker` provided. -/// -/// This function will add the `async` variant of all interfaces into the -/// [`Linker`] provided. By `async` this means that this function is only -/// compatible with [`Config::async_support(true)`][async]. For embeddings with -/// async support disabled see [`add_to_linker_sync`] instead. -/// -/// This function will add all interfaces implemented by this crate to the -/// [`Linker`], which corresponds to the `wasi:cli/imports` world supported by -/// this crate. -/// -/// [async]: wasmtime::Config::async_support -/// -/// # Example -/// -/// ``` -/// use wasmtime::{Engine, Result, Store, Config}; -/// use wasmtime::component::{ResourceTable, Linker}; -/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; -/// -/// fn main() -> Result<()> { -/// let mut config = Config::new(); -/// config.async_support(true); -/// let engine = Engine::new(&config)?; -/// -/// let mut linker = Linker::::new(&engine); -/// wasmtime_wasi::add_to_linker_async(&mut linker)?; -/// // ... add any further functionality to `linker` if desired ... -/// -/// let mut builder = WasiCtxBuilder::new(); -/// -/// // ... configure `builder` more to add env vars, args, etc ... -/// -/// let mut store = Store::new( -/// &engine, -/// MyState { -/// ctx: builder.build(), -/// table: ResourceTable::new(), -/// }, -/// ); -/// -/// // ... use `linker` to instantiate within `store` ... -/// -/// Ok(()) -/// } -/// -/// struct MyState { -/// ctx: WasiCtx, -/// table: ResourceTable, -/// } -/// -/// impl IoView for MyState { -/// fn table(&mut self) -> &mut ResourceTable { &mut self.table } -/// } -/// impl WasiView for MyState { -/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } -/// } -/// ``` -pub fn add_to_linker_async(linker: &mut Linker) -> anyhow::Result<()> { - let options = crate::bindings::LinkOptions::default(); - add_to_linker_with_options_async(linker, &options) -} - -/// Similar to [`add_to_linker_async`], but with the ability to enable unstable features. -pub fn add_to_linker_with_options_async( - linker: &mut Linker, - options: &crate::bindings::LinkOptions, -) -> anyhow::Result<()> { - let l = linker; - wasmtime_wasi_io::add_to_linker_async(l)?; - - let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); - - crate::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - crate::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - crate::bindings::filesystem::types::add_to_linker_get_host(l, closure)?; - crate::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; - crate::bindings::random::random::add_to_linker_get_host(l, closure)?; - crate::bindings::random::insecure::add_to_linker_get_host(l, closure)?; - crate::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; - crate::bindings::cli::environment::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::tcp::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::udp::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; - crate::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; - Ok(()) -} - -/// Add all WASI interfaces from this crate into the `linker` provided. -/// -/// This function will add the synchronous variant of all interfaces into the -/// [`Linker`] provided. By synchronous this means that this function is only -/// compatible with [`Config::async_support(false)`][async]. For embeddings -/// with async support enabled see [`add_to_linker_async`] instead. -/// -/// This function will add all interfaces implemented by this crate to the -/// [`Linker`], which corresponds to the `wasi:cli/imports` world supported by -/// this crate. -/// -/// [async]: wasmtime::Config::async_support -/// -/// # Example -/// -/// ``` -/// use wasmtime::{Engine, Result, Store, Config}; -/// use wasmtime::component::{ResourceTable, Linker}; -/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; -/// -/// fn main() -> Result<()> { -/// let engine = Engine::default(); -/// -/// let mut linker = Linker::::new(&engine); -/// wasmtime_wasi::add_to_linker_sync(&mut linker)?; -/// // ... add any further functionality to `linker` if desired ... -/// -/// let mut builder = WasiCtxBuilder::new(); -/// -/// // ... configure `builder` more to add env vars, args, etc ... -/// -/// let mut store = Store::new( -/// &engine, -/// MyState { -/// ctx: builder.build(), -/// table: ResourceTable::new(), -/// }, -/// ); -/// -/// // ... use `linker` to instantiate within `store` ... -/// -/// Ok(()) -/// } -/// -/// struct MyState { -/// ctx: WasiCtx, -/// table: ResourceTable, -/// } -/// impl IoView for MyState { -/// fn table(&mut self) -> &mut ResourceTable { &mut self.table } -/// } -/// impl WasiView for MyState { -/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } -/// } -/// ``` -pub fn add_to_linker_sync( - linker: &mut wasmtime::component::Linker, -) -> anyhow::Result<()> { - let options = crate::bindings::sync::LinkOptions::default(); - add_to_linker_with_options_sync(linker, &options) -} - -/// Similar to [`add_to_linker_sync`], but with the ability to enable unstable features. -pub fn add_to_linker_with_options_sync( - linker: &mut wasmtime::component::Linker, - options: &crate::bindings::sync::LinkOptions, -) -> anyhow::Result<()> { - let l = linker; - let io_closure = io_type_annotate::(|t| IoImpl(t)); - wasmtime_wasi_io::bindings::wasi::io::error::add_to_linker_get_host(l, io_closure)?; - - crate::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?; - crate::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?; - - let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); - - crate::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - crate::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - crate::bindings::sync::filesystem::types::add_to_linker_get_host(l, closure)?; - crate::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; - crate::bindings::random::random::add_to_linker_get_host(l, closure)?; - crate::bindings::random::insecure::add_to_linker_get_host(l, closure)?; - crate::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; - crate::bindings::cli::environment::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; - crate::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; - crate::bindings::sync::sockets::tcp::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; - crate::bindings::sync::sockets::udp::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; - crate::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; - crate::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; - Ok(()) -} - -// NB: workaround some rustc inference - a future refactoring may make this -// obsolete. -fn io_type_annotate(val: F) -> F -where - F: Fn(&mut T) -> IoImpl<&mut T>, -{ - val -} -fn type_annotate(val: F) -> F -where - F: Fn(&mut T) -> WasiImpl<&mut T>, -{ - val -} diff --git a/crates/wasi/src/network.rs b/crates/wasi/src/net.rs similarity index 56% rename from crates/wasi/src/network.rs rename to crates/wasi/src/net.rs index c435accf213f..d0d4aa374cb9 100644 --- a/crates/wasi/src/network.rs +++ b/crates/wasi/src/net.rs @@ -1,10 +1,11 @@ -use crate::bindings::sockets::network::{ErrorCode, Ipv4Address, Ipv6Address}; -use crate::TrappableError; use std::future::Future; use std::net::SocketAddr; use std::pin::Pin; use std::sync::Arc; +/// Value taken from rust std library. +pub const DEFAULT_TCP_BACKLOG: u32 = 128; + pub struct Network { pub socket_addr_check: SocketAddrCheck, pub allow_ip_name_lookup: bool, @@ -64,50 +65,8 @@ pub enum SocketAddrUse { UdpOutgoingDatagram, } -pub type SocketResult = Result; - -pub type SocketError = TrappableError; - -impl From for SocketError { - fn from(error: wasmtime::component::ResourceTableError) -> Self { - Self::trap(error) - } -} - -impl From for SocketError { - fn from(error: std::io::Error) -> Self { - ErrorCode::from(error).into() - } -} - -impl From for SocketError { - fn from(error: rustix::io::Errno) -> Self { - ErrorCode::from(error).into() - } -} - #[derive(Copy, Clone)] pub enum SocketAddressFamily { Ipv4, Ipv6, } - -pub(crate) fn to_ipv4_addr(addr: Ipv4Address) -> std::net::Ipv4Addr { - let (x0, x1, x2, x3) = addr; - std::net::Ipv4Addr::new(x0, x1, x2, x3) -} - -pub(crate) fn from_ipv4_addr(addr: std::net::Ipv4Addr) -> Ipv4Address { - let [x0, x1, x2, x3] = addr.octets(); - (x0, x1, x2, x3) -} - -pub(crate) fn to_ipv6_addr(addr: Ipv6Address) -> std::net::Ipv6Addr { - let (x0, x1, x2, x3, x4, x5, x6, x7) = addr; - std::net::Ipv6Addr::new(x0, x1, x2, x3, x4, x5, x6, x7) -} - -pub(crate) fn from_ipv6_addr(addr: std::net::Ipv6Addr) -> Ipv6Address { - let [x0, x1, x2, x3, x4, x5, x6, x7] = addr.segments(); - (x0, x1, x2, x3, x4, x5, x6, x7) -} diff --git a/crates/wasi/src/bindings.rs b/crates/wasi/src/p2/bindings.rs similarity index 87% rename from crates/wasi/src/bindings.rs rename to crates/wasi/src/p2/bindings.rs index 5f117cddf3e2..f3d24e73057e 100644 --- a/crates/wasi/src/bindings.rs +++ b/crates/wasi/src/p2/bindings.rs @@ -11,13 +11,13 @@ //! # Examples //! //! If you have a WIT world which refers to WASI interfaces you probably want to -//! use this crate's bindings rather than generate fresh bindings. That can be +//! use this modules's bindings rather than generate fresh bindings. That can be //! done using the `with` option to [`bindgen!`]: //! //! ```rust -//! use wasmtime_wasi::{IoView, WasiCtx, ResourceTable, WasiView}; +//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView}; //! use wasmtime::{Result, Engine, Config}; -//! use wasmtime::component::Linker; +//! use wasmtime::component::{Linker, ResourceTable}; //! //! wasmtime::component::bindgen!({ //! inline: " @@ -35,9 +35,9 @@ //! my-custom-function: func(); //! } //! ", -//! path: "wit", +//! path: "src/p2/wit", //! with: { -//! "wasi": wasmtime_wasi::bindings, +//! "wasi": wasmtime_wasi::p2::bindings, //! }, //! async: true, //! }); @@ -65,7 +65,7 @@ //! config.async_support(true); //! let engine = Engine::new(&config)?; //! let mut linker: Linker = Linker::new(&engine); -//! wasmtime_wasi::add_to_linker_async(&mut linker)?; +//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; //! example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?; //! //! // .. use `Linker` to instantiate component ... @@ -76,19 +76,19 @@ /// Synchronous-generated bindings for WASI interfaces. /// -/// This is the same as the top-level [`bindings`](crate::bindings) module of -/// this crate except that it's for synchronous calls. +/// This is the same as the top-level [`bindings`](crate::p2::bindings) submodule of +/// this module except that it's for synchronous calls. /// /// # Examples /// /// If you have a WIT world which refers to WASI interfaces you probably want to -/// use this crate's bindings rather than generate fresh bindings. That can be +/// use this modules's bindings rather than generate fresh bindings. That can be /// done using the `with` option to `bindgen!`: /// /// ```rust -/// use wasmtime_wasi::{IoView, WasiCtx, ResourceTable, WasiView}; +/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView}; /// use wasmtime::{Result, Engine}; -/// use wasmtime::component::Linker; +/// use wasmtime::component::{Linker, ResourceTable}; /// /// wasmtime::component::bindgen!({ /// inline: " @@ -106,9 +106,9 @@ /// my-custom-function: func(); /// } /// ", -/// path: "wit", +/// path: "src/p2/wit", /// with: { -/// "wasi": wasmtime_wasi::bindings::sync, +/// "wasi": wasmtime_wasi::p2::bindings::sync, /// }, /// // This is required for bindings using `wasmtime-wasi` and it otherwise /// // isn't the default for non-async bindings. @@ -136,7 +136,7 @@ /// fn main() -> Result<()> { /// let engine = Engine::default(); /// let mut linker: Linker = Linker::new(&engine); -/// wasmtime_wasi::add_to_linker_sync(&mut linker)?; +/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; /// example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?; /// /// // .. use `Linker` to instantiate component ... @@ -146,11 +146,11 @@ /// ``` pub mod sync { mod generated { - use crate::{FsError, SocketError}; + use crate::p2::{FsError, SocketError}; use wasmtime_wasi_io::streams::StreamError; wasmtime::component::bindgen!({ - path: "wit", + path: "src/p2/wit", world: "wasi:cli/command", tracing: true, trappable_error_type: { @@ -162,11 +162,11 @@ pub mod sync { with: { // These interfaces contain only synchronous methods, so they // can be aliased directly - "wasi:clocks": crate::bindings::clocks, - "wasi:random": crate::bindings::random, - "wasi:cli": crate::bindings::cli, - "wasi:filesystem/preopens": crate::bindings::filesystem::preopens, - "wasi:sockets/network": crate::bindings::sockets::network, + "wasi:clocks": crate::p2::bindings::clocks, + "wasi:random": crate::p2::bindings::random, + "wasi:cli": crate::p2::bindings::cli, + "wasi:filesystem/preopens": crate::p2::bindings::filesystem::preopens, + "wasi:sockets/network": crate::p2::bindings::sockets::network, // Configure the resource types of the bound interfaces here // to be the same as the async versions of the resources, that @@ -204,15 +204,15 @@ pub mod sync { /// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run). /// /// [async]: wasmtime::Config::async_support - /// [`wasmtime_wasi::add_to_linker_sync`]: crate::add_to_linker_sync + /// [`wasmtime_wasi::p2::add_to_linker_sync`]: crate::p2::add_to_linker_sync /// /// # Examples /// /// ```no_run /// use wasmtime::{Engine, Result, Store, Config}; /// use wasmtime::component::{ResourceTable, Linker, Component}; - /// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; - /// use wasmtime_wasi::bindings::sync::Command; + /// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; + /// use wasmtime_wasi::p2::bindings::sync::Command; /// /// // This example is an example shim of executing a component based on the /// // command line arguments provided to this program. @@ -225,7 +225,7 @@ pub mod sync { /// // Configure a `Linker` with WASI, compile a component based on /// // command line arguments. /// let mut linker = Linker::::new(&engine); - /// wasmtime_wasi::add_to_linker_sync(&mut linker)?; + /// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; /// let component = Component::from_file(&engine, &args[0])?; /// /// @@ -276,8 +276,8 @@ pub mod sync { /// ```no_run /// use wasmtime::{Engine, Result, Store, Config}; /// use wasmtime::component::{ResourceTable, Linker, Component}; - /// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; - /// use wasmtime_wasi::bindings::sync::CommandPre; + /// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; + /// use wasmtime_wasi::p2::bindings::sync::CommandPre; /// /// // This example is an example shim of executing a component based on the /// // command line arguments provided to this program. @@ -290,7 +290,7 @@ pub mod sync { /// // Configure a `Linker` with WASI, compile a component based on /// // command line arguments, and then pre-instantiate it. /// let mut linker = Linker::::new(&engine); - /// wasmtime_wasi::add_to_linker_sync(&mut linker)?; + /// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; /// let component = Component::from_file(&engine, &args[0])?; /// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?; /// @@ -339,7 +339,7 @@ pub mod sync { mod async_io { wasmtime::component::bindgen!({ - path: "wit", + path: "src/p2/wit", world: "wasi:cli/command", tracing: true, trappable_imports: true, @@ -406,8 +406,8 @@ mod async_io { }, trappable_error_type: { "wasi:io/streams/stream-error" => wasmtime_wasi_io::streams::StreamError, - "wasi:filesystem/types/error-code" => crate::FsError, - "wasi:sockets/network/error-code" => crate::SocketError, + "wasi:filesystem/types/error-code" => crate::p2::FsError, + "wasi:sockets/network/error-code" => crate::p2::SocketError, }, with: { // All interfaces in the wasi:io package should be aliased to @@ -419,16 +419,16 @@ mod async_io { // Configure all other resources to be concrete types defined in // this crate - "wasi:sockets/network/network": crate::network::Network, - "wasi:sockets/tcp/tcp-socket": crate::tcp::TcpSocket, - "wasi:sockets/udp/udp-socket": crate::udp::UdpSocket, - "wasi:sockets/udp/incoming-datagram-stream": crate::udp::IncomingDatagramStream, - "wasi:sockets/udp/outgoing-datagram-stream": crate::udp::OutgoingDatagramStream, - "wasi:sockets/ip-name-lookup/resolve-address-stream": crate::ip_name_lookup::ResolveAddressStream, - "wasi:filesystem/types/directory-entry-stream": crate::filesystem::ReaddirIterator, - "wasi:filesystem/types/descriptor": crate::filesystem::Descriptor, - "wasi:cli/terminal-input/terminal-input": crate::stdio::TerminalInput, - "wasi:cli/terminal-output/terminal-output": crate::stdio::TerminalOutput, + "wasi:sockets/network/network": crate::net::Network, + "wasi:sockets/tcp/tcp-socket": crate::p2::tcp::TcpSocket, + "wasi:sockets/udp/udp-socket": crate::p2::udp::UdpSocket, + "wasi:sockets/udp/incoming-datagram-stream": crate::p2::udp::IncomingDatagramStream, + "wasi:sockets/udp/outgoing-datagram-stream": crate::p2::udp::OutgoingDatagramStream, + "wasi:sockets/ip-name-lookup/resolve-address-stream": crate::p2::ip_name_lookup::ResolveAddressStream, + "wasi:filesystem/types/directory-entry-stream": crate::p2::filesystem::ReaddirIterator, + "wasi:filesystem/types/descriptor": crate::p2::filesystem::Descriptor, + "wasi:cli/terminal-input/terminal-input": crate::p2::stdio::TerminalInput, + "wasi:cli/terminal-output/terminal-output": crate::p2::stdio::TerminalOutput, }, }); } @@ -448,15 +448,15 @@ pub use self::async_io::LinkOptions; /// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run). /// /// [async]: wasmtime::Config::async_support -/// [`wasmtime_wasi::add_to_linker_async`]: crate::add_to_linker_async +/// [`wasmtime_wasi::p2::add_to_linker_async`]: crate::p2::add_to_linker_async /// /// # Examples /// /// ```no_run /// use wasmtime::{Engine, Result, Store, Config}; /// use wasmtime::component::{ResourceTable, Linker, Component}; -/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; -/// use wasmtime_wasi::bindings::Command; +/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; +/// use wasmtime_wasi::p2::bindings::Command; /// /// // This example is an example shim of executing a component based on the /// // command line arguments provided to this program. @@ -472,7 +472,7 @@ pub use self::async_io::LinkOptions; /// // Configure a `Linker` with WASI, compile a component based on /// // command line arguments, and then pre-instantiate it. /// let mut linker = Linker::::new(&engine); -/// wasmtime_wasi::add_to_linker_async(&mut linker)?; +/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; /// let component = Component::from_file(&engine, &args[0])?; /// /// @@ -523,8 +523,8 @@ pub use self::async_io::Command; /// ```no_run /// use wasmtime::{Engine, Result, Store, Config}; /// use wasmtime::component::{ResourceTable, Linker, Component}; -/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; -/// use wasmtime_wasi::bindings::CommandPre; +/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; +/// use wasmtime_wasi::p2::bindings::CommandPre; /// /// // This example is an example shim of executing a component based on the /// // command line arguments provided to this program. @@ -540,7 +540,7 @@ pub use self::async_io::Command; /// // Configure a `Linker` with WASI, compile a component based on /// // command line arguments, and then pre-instantiate it. /// let mut linker = Linker::::new(&engine); -/// wasmtime_wasi::add_to_linker_async(&mut linker)?; +/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; /// let component = Component::from_file(&engine, &args[0])?; /// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?; /// diff --git a/crates/wasi/src/ctx.rs b/crates/wasi/src/p2/ctx.rs similarity index 96% rename from crates/wasi/src/ctx.rs rename to crates/wasi/src/p2/ctx.rs index a64edd1dcf50..6f3004dae5b5 100644 --- a/crates/wasi/src/ctx.rs +++ b/crates/wasi/src/p2/ctx.rs @@ -1,14 +1,14 @@ -use crate::{ - clocks::{ - host::{monotonic_clock, wall_clock}, - HostMonotonicClock, HostWallClock, - }, - filesystem::{Dir, OpenMode}, - network::{SocketAddrCheck, SocketAddrUse}, - pipe, random, stdio, +use crate::clocks::{ + host::{monotonic_clock, wall_clock}, + HostMonotonicClock, HostWallClock, +}; +use crate::net::{SocketAddrCheck, SocketAddrUse}; +use crate::p2::{ + filesystem::Dir, + pipe, stdio, stdio::{StdinStream, StdoutStream}, - DirPerms, FilePerms, }; +use crate::{random, DirPerms, FilePerms, OpenMode}; use anyhow::Result; use cap_rand::{Rng, RngCore, SeedableRng}; use cap_std::ambient_authority; @@ -26,7 +26,7 @@ use std::{mem, net::SocketAddr}; /// # Examples /// /// ``` -/// use wasmtime_wasi::{WasiCtxBuilder, WasiCtx}; +/// use wasmtime_wasi::p2::{WasiCtxBuilder, WasiCtx}; /// /// let mut wasi = WasiCtxBuilder::new(); /// wasi.arg("./foo.wasm"); @@ -111,7 +111,7 @@ impl WasiCtxBuilder { /// stdin looks like: /// /// ``` - /// use wasmtime_wasi::{stdin, WasiCtxBuilder}; + /// use wasmtime_wasi::p2::{stdin, WasiCtxBuilder}; /// /// let mut wasi = WasiCtxBuilder::new(); /// wasi.stdin(stdin()); @@ -216,7 +216,7 @@ impl WasiCtxBuilder { /// # Examples /// /// ``` - /// use wasmtime_wasi::{stdin, WasiCtxBuilder}; + /// use wasmtime_wasi::p2::{stdin, WasiCtxBuilder}; /// /// let mut wasi = WasiCtxBuilder::new(); /// wasi.envs(&[ @@ -240,7 +240,7 @@ impl WasiCtxBuilder { /// # Examples /// /// ``` - /// use wasmtime_wasi::{stdin, WasiCtxBuilder}; + /// use wasmtime_wasi::p2::{stdin, WasiCtxBuilder}; /// /// let mut wasi = WasiCtxBuilder::new(); /// wasi.env("FOO", "bar"); @@ -309,7 +309,8 @@ impl WasiCtxBuilder { /// # Examples /// /// ``` - /// use wasmtime_wasi::{WasiCtxBuilder, DirPerms, FilePerms}; + /// use wasmtime_wasi::p2::WasiCtxBuilder; + /// use wasmtime_wasi::{DirPerms, FilePerms}; /// /// # fn main() {} /// # fn foo() -> wasmtime::Result<()> { @@ -529,7 +530,7 @@ impl WasiCtxBuilder { /// /// This structure is created through [`WasiCtxBuilder`] and is stored within /// the `T` of [`Store`][`Store`]. Access to the structure is provided -/// through the [`WasiView`](crate::WasiView) trait as an implementation on `T`. +/// through the [`WasiView`](crate::p2::WasiView) trait as an implementation on `T`. /// /// Note that this structure itself does not have any accessors, it's here for /// internal use within the `wasmtime-wasi` crate's implementation of @@ -540,7 +541,8 @@ impl WasiCtxBuilder { /// # Example /// /// ``` -/// use wasmtime_wasi::{WasiCtx, ResourceTable, WasiView, IoView, WasiCtxBuilder}; +/// use wasmtime_wasi::ResourceTable; +/// use wasmtime_wasi::p2::{WasiCtx, WasiView, IoView, WasiCtxBuilder}; /// /// struct MyState { /// ctx: WasiCtx, diff --git a/crates/wasi/src/filesystem.rs b/crates/wasi/src/p2/filesystem.rs similarity index 93% rename from crates/wasi/src/filesystem.rs rename to crates/wasi/src/p2/filesystem.rs index 14f70892068a..fb8bf7d26d8d 100644 --- a/crates/wasi/src/filesystem.rs +++ b/crates/wasi/src/p2/filesystem.rs @@ -1,6 +1,7 @@ -use crate::bindings::filesystem::types; +use crate::p2::bindings::filesystem::types; +use crate::p2::{InputStream, OutputStream, Pollable, StreamError, StreamResult}; use crate::runtime::{spawn_blocking, AbortOnDropJoinHandle}; -use crate::{InputStream, OutputStream, Pollable, StreamError, StreamResult, TrappableError}; +use crate::{DirPerms, FilePerms, OpenMode, TrappableError}; use anyhow::anyhow; use bytes::{Bytes, BytesMut}; use std::io; @@ -58,22 +59,6 @@ impl Descriptor { } } -bitflags::bitflags! { - #[derive(Copy, Clone, Debug, PartialEq, Eq)] - pub struct FilePerms: usize { - const READ = 0b1; - const WRITE = 0b10; - } -} - -bitflags::bitflags! { - #[derive(Copy, Clone, Debug, PartialEq, Eq)] - pub struct OpenMode: usize { - const READ = 0b1; - const WRITE = 0b10; - } -} - #[derive(Clone)] pub struct File { /// The operating system File this struct is mediating access to. @@ -85,7 +70,7 @@ pub struct File { /// [`spawn_blocking`]: Self::spawn_blocking pub file: Arc, /// Permissions to enforce on access to the file. These permissions are - /// specified by a user of the `crate::WasiCtxBuilder`, and are + /// specified by a user of the `crate::p2::WasiCtxBuilder`, and are /// enforced prior to any enforced by the underlying operating system. pub perms: FilePerms, /// The mode the file was opened under: bits for reading, and writing. @@ -123,7 +108,7 @@ impl File { /// /// Intentionally blocking the executor thread might seem unorthodox, but is /// not actually a problem for specific workloads. See: - /// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`] + /// - [`crate::p2::WasiCtxBuilder::allow_blocking_current_thread`] /// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973) /// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190) pub(crate) async fn run_blocking(&self, body: F) -> R @@ -158,23 +143,6 @@ impl File { } } -bitflags::bitflags! { - /// Permission bits for operating on a directory. - /// - /// Directories can be limited to being readonly. This will restrict what - /// can be done with them, for example preventing creation of new files. - #[derive(Copy, Clone, Debug, PartialEq, Eq)] - pub struct DirPerms: usize { - /// This directory can be read, for example its entries can be iterated - /// over and files can be opened. - const READ = 0b1; - - /// This directory can be mutated, for example by creating new files - /// within it. - const MUTATE = 0b10; - } -} - #[derive(Clone)] pub struct Dir { /// The operating system file descriptor this struct is mediating access @@ -185,7 +153,7 @@ pub struct Dir { /// [`spawn_blocking`]: Self::spawn_blocking pub dir: Arc, /// Permissions to enforce on access to this directory. These permissions - /// are specified by a user of the `crate::WasiCtxBuilder`, and + /// are specified by a user of the `crate::p2::WasiCtxBuilder`, and /// are enforced prior to any enforced by the underlying operating system. /// /// These permissions are also enforced on any directories opened under @@ -230,7 +198,7 @@ impl Dir { /// /// Intentionally blocking the executor thread might seem unorthodox, but is /// not actually a problem for specific workloads. See: - /// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`] + /// - [`crate::p2::WasiCtxBuilder::allow_blocking_current_thread`] /// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973) /// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190) pub(crate) async fn run_blocking(&self, body: F) -> R diff --git a/crates/wasi/src/host/clocks.rs b/crates/wasi/src/p2/host/clocks.rs similarity index 97% rename from crates/wasi/src/host/clocks.rs rename to crates/wasi/src/p2/host/clocks.rs index fec0a6061c49..948591e7e8f9 100644 --- a/crates/wasi/src/host/clocks.rs +++ b/crates/wasi/src/p2/host/clocks.rs @@ -1,10 +1,10 @@ #![allow(unused_variables)] -use crate::bindings::{ +use crate::p2::bindings::{ clocks::monotonic_clock::{self, Duration as WasiDuration, Instant}, clocks::wall_clock::{self, Datetime}, }; -use crate::{DynPollable, IoView, WasiImpl, WasiView}; +use crate::p2::{DynPollable, IoView, WasiImpl, WasiView}; use cap_std::time::SystemTime; use std::time::Duration; use wasmtime::component::Resource; diff --git a/crates/wasi/src/host/env.rs b/crates/wasi/src/p2/host/env.rs similarity index 84% rename from crates/wasi/src/host/env.rs rename to crates/wasi/src/p2/host/env.rs index db259d8caa14..02652e32744c 100644 --- a/crates/wasi/src/host/env.rs +++ b/crates/wasi/src/p2/host/env.rs @@ -1,5 +1,5 @@ -use crate::bindings::cli::environment; -use crate::{WasiImpl, WasiView}; +use crate::p2::bindings::cli::environment; +use crate::p2::{WasiImpl, WasiView}; impl environment::Host for WasiImpl where diff --git a/crates/wasi/src/host/exit.rs b/crates/wasi/src/p2/host/exit.rs similarity index 84% rename from crates/wasi/src/host/exit.rs rename to crates/wasi/src/p2/host/exit.rs index b955aa2244ab..54b07279a933 100644 --- a/crates/wasi/src/host/exit.rs +++ b/crates/wasi/src/p2/host/exit.rs @@ -1,4 +1,5 @@ -use crate::{bindings::cli::exit, I32Exit, WasiImpl, WasiView}; +use crate::p2::{bindings::cli::exit, WasiImpl, WasiView}; +use crate::I32Exit; impl exit::Host for WasiImpl where diff --git a/crates/wasi/src/host/filesystem.rs b/crates/wasi/src/p2/host/filesystem.rs similarity index 99% rename from crates/wasi/src/host/filesystem.rs rename to crates/wasi/src/p2/host/filesystem.rs index 0f7d096758fe..3ddb5b54a453 100644 --- a/crates/wasi/src/host/filesystem.rs +++ b/crates/wasi/src/p2/host/filesystem.rs @@ -1,12 +1,13 @@ -use crate::bindings::clocks::wall_clock; -use crate::bindings::filesystem::preopens; -use crate::bindings::filesystem::types::{ +use crate::p2::bindings::clocks::wall_clock; +use crate::p2::bindings::filesystem::preopens; +use crate::p2::bindings::filesystem::types::{ self, ErrorCode, HostDescriptor, HostDirectoryEntryStream, }; -use crate::filesystem::{ - Descriptor, Dir, File, FileInputStream, FileOutputStream, OpenMode, ReaddirIterator, +use crate::p2::filesystem::{ + Descriptor, Dir, File, FileInputStream, FileOutputStream, ReaddirIterator, }; -use crate::{DirPerms, FilePerms, FsError, FsResult, IoView, WasiImpl, WasiView}; +use crate::p2::{FsError, FsResult, IoView, WasiImpl, WasiView}; +use crate::{DirPerms, FilePerms, OpenMode}; use anyhow::Context; use wasmtime::component::Resource; use wasmtime_wasi_io::streams::{DynInputStream, DynOutputStream}; diff --git a/crates/wasi/src/host/filesystem/sync.rs b/crates/wasi/src/p2/host/filesystem/sync.rs similarity index 98% rename from crates/wasi/src/host/filesystem/sync.rs rename to crates/wasi/src/p2/host/filesystem/sync.rs index 5de6b051921d..4294f43f0d9b 100644 --- a/crates/wasi/src/host/filesystem/sync.rs +++ b/crates/wasi/src/p2/host/filesystem/sync.rs @@ -1,8 +1,8 @@ -use crate::bindings::filesystem::types as async_filesystem; -use crate::bindings::sync::filesystem::types as sync_filesystem; -use crate::bindings::sync::io::streams; +use crate::p2::bindings::filesystem::types as async_filesystem; +use crate::p2::bindings::sync::filesystem::types as sync_filesystem; +use crate::p2::bindings::sync::io::streams; +use crate::p2::{FsError, FsResult, WasiImpl, WasiView}; use crate::runtime::in_tokio; -use crate::{FsError, FsResult, WasiImpl, WasiView}; use wasmtime::component::Resource; impl sync_filesystem::Host for WasiImpl diff --git a/crates/wasi/src/host/instance_network.rs b/crates/wasi/src/p2/host/instance_network.rs similarity index 79% rename from crates/wasi/src/host/instance_network.rs rename to crates/wasi/src/p2/host/instance_network.rs index d6b06718a8d0..40af0d34541f 100644 --- a/crates/wasi/src/host/instance_network.rs +++ b/crates/wasi/src/p2/host/instance_network.rs @@ -1,6 +1,6 @@ -use crate::bindings::sockets::instance_network; -use crate::network::Network; -use crate::{IoView, WasiImpl, WasiView}; +use crate::net::Network; +use crate::p2::bindings::sockets::instance_network; +use crate::p2::{IoView, WasiImpl, WasiView}; use wasmtime::component::Resource; impl instance_network::Host for WasiImpl diff --git a/crates/wasi/src/host/io.rs b/crates/wasi/src/p2/host/io.rs similarity index 98% rename from crates/wasi/src/host/io.rs rename to crates/wasi/src/p2/host/io.rs index 75d41865102e..ad3864f8bac5 100644 --- a/crates/wasi/src/host/io.rs +++ b/crates/wasi/src/p2/host/io.rs @@ -1,9 +1,9 @@ -use crate::{ +use crate::p2::{ bindings::sync::io::poll::Pollable, bindings::sync::io::streams::{self, InputStream, OutputStream}, - runtime::in_tokio, IoImpl, IoView, StreamError, StreamResult, }; +use crate::runtime::in_tokio; use wasmtime::component::Resource; use wasmtime_wasi_io::bindings::wasi::io::streams::{ self as async_streams, Host as AsyncHost, HostInputStream as AsyncHostInputStream, diff --git a/crates/wasi/src/host/mod.rs b/crates/wasi/src/p2/host/mod.rs similarity index 100% rename from crates/wasi/src/host/mod.rs rename to crates/wasi/src/p2/host/mod.rs diff --git a/crates/wasi/src/host/network.rs b/crates/wasi/src/p2/host/network.rs similarity index 98% rename from crates/wasi/src/host/network.rs rename to crates/wasi/src/p2/host/network.rs index 5d04ebf1d7de..fa07d3f42c63 100644 --- a/crates/wasi/src/host/network.rs +++ b/crates/wasi/src/p2/host/network.rs @@ -1,9 +1,9 @@ -use crate::bindings::sockets::network::{ +use crate::p2::bindings::sockets::network::{ self, ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Ipv4SocketAddress, Ipv6SocketAddress, }; -use crate::network::{from_ipv4_addr, from_ipv6_addr, to_ipv4_addr, to_ipv6_addr}; -use crate::{IoView, SocketError, WasiImpl, WasiView}; +use crate::p2::network::{from_ipv4_addr, from_ipv6_addr, to_ipv4_addr, to_ipv6_addr}; +use crate::p2::{IoView, SocketError, WasiImpl, WasiView}; use anyhow::Error; use rustix::io::Errno; use std::io; @@ -28,7 +28,7 @@ where } } -impl crate::bindings::sockets::network::HostNetwork for WasiImpl +impl crate::p2::bindings::sockets::network::HostNetwork for WasiImpl where T: WasiView, { @@ -245,7 +245,7 @@ pub(crate) mod util { use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use std::time::Duration; - use crate::network::SocketAddressFamily; + use crate::net::SocketAddressFamily; use cap_net_ext::{AddressFamily, Blocking, UdpSocketExt}; use rustix::fd::{AsFd, OwnedFd}; use rustix::io::Errno; diff --git a/crates/wasi/src/host/random.rs b/crates/wasi/src/p2/host/random.rs similarity index 91% rename from crates/wasi/src/host/random.rs rename to crates/wasi/src/p2/host/random.rs index a3fe59487d3f..9c0c540d2a3d 100644 --- a/crates/wasi/src/host/random.rs +++ b/crates/wasi/src/p2/host/random.rs @@ -1,5 +1,5 @@ -use crate::bindings::random::{insecure, insecure_seed, random}; -use crate::{WasiImpl, WasiView}; +use crate::p2::bindings::random::{insecure, insecure_seed, random}; +use crate::p2::{WasiImpl, WasiView}; use cap_rand::{distributions::Standard, Rng}; impl random::Host for WasiImpl diff --git a/crates/wasi/src/host/tcp.rs b/crates/wasi/src/p2/host/tcp.rs similarity index 97% rename from crates/wasi/src/host/tcp.rs rename to crates/wasi/src/p2/host/tcp.rs index 0b8ad5ae5b9b..9445b73384eb 100644 --- a/crates/wasi/src/host/tcp.rs +++ b/crates/wasi/src/p2/host/tcp.rs @@ -1,12 +1,9 @@ -use crate::network::SocketAddrUse; -use crate::{ - bindings::{ - sockets::network::{IpAddressFamily, IpSocketAddress, Network}, - sockets::tcp::{self, ShutdownType}, - }, - network::SocketAddressFamily, +use crate::net::{SocketAddrUse, SocketAddressFamily}; +use crate::p2::bindings::{ + sockets::network::{IpAddressFamily, IpSocketAddress, Network}, + sockets::tcp::{self, ShutdownType}, }; -use crate::{SocketResult, WasiImpl, WasiView}; +use crate::p2::{SocketResult, WasiImpl, WasiView}; use std::net::SocketAddr; use std::time::Duration; use wasmtime::component::Resource; @@ -18,7 +15,7 @@ use wasmtime_wasi_io::{ impl tcp::Host for WasiImpl where T: WasiView {} -impl crate::host::tcp::tcp::HostTcpSocket for WasiImpl +impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiImpl where T: WasiView, { @@ -322,7 +319,7 @@ where pub mod sync { use wasmtime::component::Resource; - use crate::{ + use crate::p2::{ bindings::{ sockets::{ network::Network, @@ -333,9 +330,9 @@ pub mod sync { OutputStream, Pollable, ShutdownType, TcpSocket, }, }, - runtime::in_tokio, SocketError, WasiImpl, WasiView, }; + use crate::runtime::in_tokio; impl tcp::Host for WasiImpl where T: WasiView {} diff --git a/crates/wasi/src/host/tcp_create_socket.rs b/crates/wasi/src/p2/host/tcp_create_socket.rs similarity index 67% rename from crates/wasi/src/host/tcp_create_socket.rs rename to crates/wasi/src/p2/host/tcp_create_socket.rs index 4c846295f552..09dd5c07ea1a 100644 --- a/crates/wasi/src/host/tcp_create_socket.rs +++ b/crates/wasi/src/p2/host/tcp_create_socket.rs @@ -1,6 +1,6 @@ -use crate::bindings::{sockets::network::IpAddressFamily, sockets::tcp_create_socket}; -use crate::tcp::TcpSocket; -use crate::{IoView, SocketResult, WasiImpl, WasiView}; +use crate::p2::bindings::{sockets::network::IpAddressFamily, sockets::tcp_create_socket}; +use crate::p2::tcp::TcpSocket; +use crate::p2::{IoView, SocketResult, WasiImpl, WasiView}; use wasmtime::component::Resource; impl tcp_create_socket::Host for WasiImpl diff --git a/crates/wasi/src/host/udp.rs b/crates/wasi/src/p2/host/udp.rs similarity index 98% rename from crates/wasi/src/host/udp.rs rename to crates/wasi/src/p2/host/udp.rs index ce67e270f859..9d63755318b2 100644 --- a/crates/wasi/src/host/udp.rs +++ b/crates/wasi/src/p2/host/udp.rs @@ -1,14 +1,9 @@ -use crate::host::network::util; -use crate::network::{SocketAddrUse, SocketAddressFamily}; -use crate::{ - bindings::{ - sockets::network::{ErrorCode, IpAddressFamily, IpSocketAddress, Network}, - sockets::udp, - }, - udp::{IncomingDatagramStream, OutgoingDatagramStream, SendState, UdpState}, - Pollable, -}; -use crate::{IoView, SocketError, SocketResult, WasiImpl, WasiView}; +use crate::net::{SocketAddrUse, SocketAddressFamily}; +use crate::p2::bindings::sockets::network::{ErrorCode, IpAddressFamily, IpSocketAddress, Network}; +use crate::p2::bindings::sockets::udp; +use crate::p2::host::network::util; +use crate::p2::udp::{IncomingDatagramStream, OutgoingDatagramStream, SendState, UdpState}; +use crate::p2::{IoView, Pollable, SocketError, SocketResult, WasiImpl, WasiView}; use anyhow::anyhow; use async_trait::async_trait; use io_lifetimes::AsSocketlike; @@ -549,7 +544,7 @@ impl Pollable for OutgoingDatagramStream { pub mod sync { use wasmtime::component::Resource; - use crate::{ + use crate::p2::{ bindings::{ sockets::{ network::Network, @@ -567,9 +562,9 @@ pub mod sync { UdpSocket, }, }, - runtime::in_tokio, SocketError, WasiImpl, WasiView, }; + use crate::runtime::in_tokio; impl udp::Host for WasiImpl where T: WasiView {} diff --git a/crates/wasi/src/host/udp_create_socket.rs b/crates/wasi/src/p2/host/udp_create_socket.rs similarity index 67% rename from crates/wasi/src/host/udp_create_socket.rs rename to crates/wasi/src/p2/host/udp_create_socket.rs index 533b4d7d5c92..bcacee25455d 100644 --- a/crates/wasi/src/host/udp_create_socket.rs +++ b/crates/wasi/src/p2/host/udp_create_socket.rs @@ -1,6 +1,6 @@ -use crate::bindings::{sockets::network::IpAddressFamily, sockets::udp_create_socket}; -use crate::udp::UdpSocket; -use crate::{IoView, SocketResult, WasiImpl, WasiView}; +use crate::p2::bindings::{sockets::network::IpAddressFamily, sockets::udp_create_socket}; +use crate::p2::udp::UdpSocket; +use crate::p2::{IoView, SocketResult, WasiImpl, WasiView}; use wasmtime::component::Resource; impl udp_create_socket::Host for WasiImpl diff --git a/crates/wasi/src/ip_name_lookup.rs b/crates/wasi/src/p2/ip_name_lookup.rs similarity index 94% rename from crates/wasi/src/ip_name_lookup.rs rename to crates/wasi/src/p2/ip_name_lookup.rs index 90824aa8a57d..4849aca9b255 100644 --- a/crates/wasi/src/ip_name_lookup.rs +++ b/crates/wasi/src/p2/ip_name_lookup.rs @@ -1,8 +1,8 @@ -use crate::bindings::sockets::ip_name_lookup::{Host, HostResolveAddressStream}; -use crate::bindings::sockets::network::{ErrorCode, IpAddress, Network}; -use crate::host::network::util; +use crate::p2::bindings::sockets::ip_name_lookup::{Host, HostResolveAddressStream}; +use crate::p2::bindings::sockets::network::{ErrorCode, IpAddress, Network}; +use crate::p2::host::network::util; +use crate::p2::{IoView, SocketError, WasiImpl, WasiView}; use crate::runtime::{spawn_blocking, AbortOnDropJoinHandle}; -use crate::{IoView, SocketError, WasiImpl, WasiView}; use anyhow::Result; use std::mem; use std::net::{Ipv6Addr, ToSocketAddrs}; diff --git a/crates/wasi/src/p2/mod.rs b/crates/wasi/src/p2/mod.rs new file mode 100644 index 000000000000..150350bd2da6 --- /dev/null +++ b/crates/wasi/src/p2/mod.rs @@ -0,0 +1,479 @@ +//! # Wasmtime's WASIp2 Implementation +//! +//! +//! This module provides a Wasmtime host implementation of WASI 0.2 (aka WASIp2 +//! aka Preview 2) and WASI 0.1 (aka WASIp1 aka Preview 1). WASI is implemented +//! with the Rust crates [`tokio`] and [`cap-std`] primarily, meaning that +//! operations are implemented in terms of their native platform equivalents by +//! default. +//! +//! # WASIp2 interfaces +//! +//! This module contains implementations of the following interfaces: +//! +//! * [`wasi:cli/environment`] +//! * [`wasi:cli/exit`] +//! * [`wasi:cli/stderr`] +//! * [`wasi:cli/stdin`] +//! * [`wasi:cli/stdout`] +//! * [`wasi:cli/terminal-input`] +//! * [`wasi:cli/terminal-output`] +//! * [`wasi:cli/terminal-stderr`] +//! * [`wasi:cli/terminal-stdin`] +//! * [`wasi:cli/terminal-stdout`] +//! * [`wasi:clocks/monotonic-clock`] +//! * [`wasi:clocks/wall-clock`] +//! * [`wasi:filesystem/preopens`] +//! * [`wasi:filesystem/types`] +//! * [`wasi:random/insecure-seed`] +//! * [`wasi:random/insecure`] +//! * [`wasi:random/random`] +//! * [`wasi:sockets/instance-network`] +//! * [`wasi:sockets/ip-name-lookup`] +//! * [`wasi:sockets/network`] +//! * [`wasi:sockets/tcp-create-socket`] +//! * [`wasi:sockets/tcp`] +//! * [`wasi:sockets/udp-create-socket`] +//! * [`wasi:sockets/udp`] +//! +//! All traits are implemented in terms of a [`WasiView`] trait which provides +//! access to [`WasiCtx`], which defines the configuration for WASI. +//! The [`WasiView`] trait imples [`IoView`], which provides access to a common +//! [`ResourceTable`], which owns all host-defined component model resources. +//! +//! The [`wasmtime-wasi-io`] crate contains implementations of the +//! following interfaces, and this module reuses those implementations: +//! +//! * [`wasi:io/error`] +//! * [`wasi:io/poll`] +//! * [`wasi:io/streams`] +//! +//! These traits are implemented in terms of a [`IoView`] trait, which only +//! provides access to a common [`ResourceTable`]. All aspects of +//! `wasmtime-wasi-io` that are used by this module are re-exported. Unless you +//! are implementing other host functionality that needs to interact with the +//! WASI scheduler and don't want to use other functionality provided by +//! `wasmtime-wasi`, you don't need to take a direct dependency on +//! `wasmtime-wasi-io`. +//! +//! # Generated Bindings +//! +//! This module uses [`wasmtime::component::bindgen!`] to generate bindings for +//! all WASI interfaces. Raw bindings are available in the [`bindings`] submodule +//! of this module. Downstream users can either implement these traits themselves +//! or you can use the built-in implementations in this module for +//! `WasiImpl`. +//! +//! # The `WasiView` trait +//! +//! This module's implementation of WASI is done in terms of an implementation of +//! [`WasiView`]. This trait provides a "view" into WASI-related state that is +//! contained within a [`Store`](wasmtime::Store). [`WasiView`] implies the +//! [`IoView`] trait, which provides access to common [`ResourceTable`] which +//! owns all host-implemented component model resources. +//! +//! For all of the generated bindings in this module (Host traits), +//! implementations are provided looking like: +//! +//! ``` +//! # use wasmtime_wasi::p2::WasiImpl; +//! # trait WasiView {} +//! # mod bindings { pub mod wasi { pub trait Host {} } } +//! impl bindings::wasi::Host for WasiImpl { +//! // ... +//! } +//! ``` +//! +//! The [`add_to_linker_sync`] and [`add_to_linker_async`] function then require +//! that `T: WasiView` with [`Linker`](wasmtime::component::Linker). +//! +//! To implement the [`WasiView`] and [`IoView`] trait you will first select a +//! `T` to put in `Store` (typically, by defining your own struct). +//! Somewhere within `T` you'll store: +//! +//! * [`ResourceTable`] - created through default constructors. +//! * [`WasiCtx`] - created through [`WasiCtxBuilder`]. +//! +//! You'll then write implementations of the [`IoView`] and [`WasiView`] +//! traits to access those items in your `T`. For example: +//! ``` +//! use wasmtime::component::ResourceTable; +//! use wasmtime_wasi::p2::{WasiCtx, IoView, WasiView}; +//! struct MyCtx { +//! table: ResourceTable, +//! wasi: WasiCtx, +//! } +//! impl IoView for MyCtx { +//! fn table(&mut self) -> &mut ResourceTable { +//! &mut self.table +//! } +//! } +//! impl WasiView for MyCtx { +//! fn ctx(&mut self) -> &mut WasiCtx { +//! &mut self.wasi +//! } +//! } +//! +//! ``` +//! +//! # Async and Sync +//! +//! As of WASI0.2, WASI functions are not blocking from WebAssembly's point of +//! view: a WebAssembly call into these functions returns when they are +//! complete. +//! +//! This module provides an implementation of those functions in the host, +//! where for some functions, it is appropriate to implement them using +//! async Rust and the Tokio executor, so that the host implementation can be +//! nonblocking when Wasmtime's [`Config::async_support`][async] is set. +//! Synchronous wrappers are provided for all async implementations, which +//! creates a private Tokio executor. +//! +//! Users can choose between these modes of implementation using variants +//! of the add_to_linker functions: +//! +//! * For non-async users (the default of `Config`), use [`add_to_linker_sync`]. +//! * For async users, use [`add_to_linker_async`]. +//! +//! Note that bindings are generated once for async and once for sync. Most +//! interfaces do not change, however, so only interfaces with blocking +//! functions have bindings generated twice. Bindings are organized as: +//! +//! * [`bindings`] - default location of all bindings, blocking functions are +//! `async` +//! * [`bindings::sync`] - blocking interfaces have synchronous versions here. +//! +//! # Module-specific traits +//! +//! This module's default implementation of WASI bindings to native primitives +//! for the platform that it is compiled for. For example opening a TCP socket +//! uses the native platform to open a TCP socket (so long as [`WasiCtxBuilder`] +//! allows it). There are a few important traits, however, that are specific to +//! this module. +//! +//! * [`InputStream`] and [`OutputStream`] - these are the host traits +//! behind the WASI `input-stream` and `output-stream` types in the +//! `wasi:io/streams` interface. These enable embedders to build their own +//! custom stream and insert them into a [`ResourceTable`] (as a boxed trait +//! object, see [`DynInputStream`] and [`DynOutputStream`]) to be used from +//! wasm. +//! +//! * [`Pollable`] - this trait enables building arbitrary logic to get hooked +//! into a `pollable` resource from `wasi:io/poll`. A pollable resource is +//! created through the [`subscribe`] function. +//! +//! * [`HostWallClock`](crate::HostWallClock) and [`HostMonotonicClock`](crate::HostMonotonicClock) are used in conjunction with +//! [`WasiCtxBuilder::wall_clock`] and [`WasiCtxBuilder::monotonic_clock`] if +//! the defaults host's clock should not be used. +//! +//! * [`StdinStream`] and [`StdoutStream`] are used to provide custom +//! stdin/stdout streams if they're not inherited (or null, which is the +//! default). +//! +//! These traits enable embedders to customize small portions of WASI interfaces +//! provided while still providing all other interfaces. +//! +//! # Examples +//! +//! Usage of this module is done through a few steps to get everything hooked up: +//! +//! 1. First implement [`IoView`] and [`WasiView`] for your type which is the +//! `T` in `Store`. +//! 2. Add WASI interfaces to a `wasmtime::component::Linker`. This is either +//! done through top-level functions like [`add_to_linker_sync`] or through +//! individual `add_to_linker` functions in generated bindings throughout +//! this module. +//! 3. Create a [`WasiCtx`] for each `Store` through [`WasiCtxBuilder`]. Each +//! WASI context is "null" or "empty" by default, so items must be explicitly +//! added to get accessed by wasm (such as env vars or program arguments). +//! 4. Use the previous `Linker` to instantiate a `Component` within a +//! `Store`. +//! +//! For examples see each of [`WasiView`], [`WasiCtx`], [`WasiCtxBuilder`], +//! [`add_to_linker_sync`], and [`bindings::Command`]. +//! +//! [`wasmtime::component::bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html +//! [`tokio`]: https://crates.io/crates/tokio +//! [`cap-std`]: https://crates.io/crates/cap-std +//! [`wasmtime-wasi-io`]: https://crates.io/crates/wasmtime-wasi-io +//! [`wasi:cli/environment`]: bindings::cli::environment::Host +//! [`wasi:cli/exit`]: bindings::cli::exit::Host +//! [`wasi:cli/stderr`]: bindings::cli::stderr::Host +//! [`wasi:cli/stdin`]: bindings::cli::stdin::Host +//! [`wasi:cli/stdout`]: bindings::cli::stdout::Host +//! [`wasi:cli/terminal-input`]: bindings::cli::terminal_input::Host +//! [`wasi:cli/terminal-output`]: bindings::cli::terminal_output::Host +//! [`wasi:cli/terminal-stdin`]: bindings::cli::terminal_stdin::Host +//! [`wasi:cli/terminal-stdout`]: bindings::cli::terminal_stdout::Host +//! [`wasi:cli/terminal-stderr`]: bindings::cli::terminal_stderr::Host +//! [`wasi:clocks/monotonic-clock`]: bindings::clocks::monotonic_clock::Host +//! [`wasi:clocks/wall-clock`]: bindings::clocks::wall_clock::Host +//! [`wasi:filesystem/preopens`]: bindings::filesystem::preopens::Host +//! [`wasi:filesystem/types`]: bindings::filesystem::types::Host +//! [`wasi:io/error`]: wasmtime_wasi_io::bindings::wasi::io::error::Host +//! [`wasi:io/poll`]: wasmtime_wasi_io::bindings::wasi::io::poll::Host +//! [`wasi:io/streams`]: wasmtime_wasi_io::bindings::wasi::io::streams::Host +//! [`wasi:random/insecure-seed`]: bindings::random::insecure_seed::Host +//! [`wasi:random/insecure`]: bindings::random::insecure::Host +//! [`wasi:random/random`]: bindings::random::random::Host +//! [`wasi:sockets/instance-network`]: bindings::sockets::instance_network::Host +//! [`wasi:sockets/ip-name-lookup`]: bindings::sockets::ip_name_lookup::Host +//! [`wasi:sockets/network`]: bindings::sockets::network::Host +//! [`wasi:sockets/tcp-create-socket`]: bindings::sockets::tcp_create_socket::Host +//! [`wasi:sockets/tcp`]: bindings::sockets::tcp::Host +//! [`wasi:sockets/udp-create-socket`]: bindings::sockets::udp_create_socket::Host +//! [`wasi:sockets/udp`]: bindings::sockets::udp::Host +//! [async]: https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.async_support +//! [`ResourceTable`]: wasmtime::component::ResourceTable + +use wasmtime::component::Linker; + +pub mod bindings; +mod ctx; +pub(crate) mod filesystem; +mod host; +mod ip_name_lookup; +mod network; +pub mod pipe; +mod poll; +mod stdio; +mod tcp; +mod udp; +mod view; +mod write_stream; + +pub use self::ctx::{WasiCtx, WasiCtxBuilder}; +pub use self::filesystem::{FileInputStream, FsError, FsResult}; +pub use self::network::{SocketError, SocketResult}; +pub use self::stdio::{ + stderr, stdin, stdout, AsyncStdinStream, AsyncStdoutStream, IsATTY, OutputFile, Stderr, Stdin, + StdinStream, Stdout, StdoutStream, +}; +pub use self::view::{WasiImpl, WasiView}; +// These contents of wasmtime-wasi-io are re-exported by this module for compatibility: +// they were originally defined in this module before being factored out, and many +// users of this module depend on them at these names. +pub use wasmtime_wasi_io::poll::{subscribe, DynFuture, DynPollable, MakeFuture, Pollable}; +pub use wasmtime_wasi_io::streams::{ + DynInputStream, DynOutputStream, Error as IoError, InputStream, OutputStream, StreamError, + StreamResult, +}; +pub use wasmtime_wasi_io::{IoImpl, IoView}; + +/// Add all WASI interfaces from this crate into the `linker` provided. +/// +/// This function will add the `async` variant of all interfaces into the +/// [`Linker`] provided. By `async` this means that this function is only +/// compatible with [`Config::async_support(true)`][async]. For embeddings with +/// async support disabled see [`add_to_linker_sync`] instead. +/// +/// This function will add all interfaces implemented by this crate to the +/// [`Linker`], which corresponds to the `wasi:cli/imports` world supported by +/// this crate. +/// +/// [async]: wasmtime::Config::async_support +/// +/// # Example +/// +/// ``` +/// use wasmtime::{Engine, Result, Store, Config}; +/// use wasmtime::component::{ResourceTable, Linker}; +/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; +/// +/// fn main() -> Result<()> { +/// let mut config = Config::new(); +/// config.async_support(true); +/// let engine = Engine::new(&config)?; +/// +/// let mut linker = Linker::::new(&engine); +/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; +/// // ... add any further functionality to `linker` if desired ... +/// +/// let mut builder = WasiCtxBuilder::new(); +/// +/// // ... configure `builder` more to add env vars, args, etc ... +/// +/// let mut store = Store::new( +/// &engine, +/// MyState { +/// ctx: builder.build(), +/// table: ResourceTable::new(), +/// }, +/// ); +/// +/// // ... use `linker` to instantiate within `store` ... +/// +/// Ok(()) +/// } +/// +/// struct MyState { +/// ctx: WasiCtx, +/// table: ResourceTable, +/// } +/// +/// impl IoView for MyState { +/// fn table(&mut self) -> &mut ResourceTable { &mut self.table } +/// } +/// impl WasiView for MyState { +/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } +/// } +/// ``` +pub fn add_to_linker_async(linker: &mut Linker) -> anyhow::Result<()> { + let options = crate::p2::bindings::LinkOptions::default(); + add_to_linker_with_options_async(linker, &options) +} + +/// Similar to [`add_to_linker_async`], but with the ability to enable unstable features. +pub fn add_to_linker_with_options_async( + linker: &mut Linker, + options: &crate::p2::bindings::LinkOptions, +) -> anyhow::Result<()> { + let l = linker; + wasmtime_wasi_io::add_to_linker_async(l)?; + + let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); + + crate::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::filesystem::types::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::insecure::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; + crate::p2::bindings::cli::environment::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::tcp::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::udp::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; + crate::p2::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; + Ok(()) +} + +/// Add all WASI interfaces from this crate into the `linker` provided. +/// +/// This function will add the synchronous variant of all interfaces into the +/// [`Linker`] provided. By synchronous this means that this function is only +/// compatible with [`Config::async_support(false)`][async]. For embeddings +/// with async support enabled see [`add_to_linker_async`] instead. +/// +/// This function will add all interfaces implemented by this crate to the +/// [`Linker`], which corresponds to the `wasi:cli/imports` world supported by +/// this crate. +/// +/// [async]: wasmtime::Config::async_support +/// +/// # Example +/// +/// ``` +/// use wasmtime::{Engine, Result, Store, Config}; +/// use wasmtime::component::{ResourceTable, Linker}; +/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView, WasiCtxBuilder}; +/// +/// fn main() -> Result<()> { +/// let engine = Engine::default(); +/// +/// let mut linker = Linker::::new(&engine); +/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; +/// // ... add any further functionality to `linker` if desired ... +/// +/// let mut builder = WasiCtxBuilder::new(); +/// +/// // ... configure `builder` more to add env vars, args, etc ... +/// +/// let mut store = Store::new( +/// &engine, +/// MyState { +/// ctx: builder.build(), +/// table: ResourceTable::new(), +/// }, +/// ); +/// +/// // ... use `linker` to instantiate within `store` ... +/// +/// Ok(()) +/// } +/// +/// struct MyState { +/// ctx: WasiCtx, +/// table: ResourceTable, +/// } +/// impl IoView for MyState { +/// fn table(&mut self) -> &mut ResourceTable { &mut self.table } +/// } +/// impl WasiView for MyState { +/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } +/// } +/// ``` +pub fn add_to_linker_sync( + linker: &mut wasmtime::component::Linker, +) -> anyhow::Result<()> { + let options = crate::p2::bindings::sync::LinkOptions::default(); + add_to_linker_with_options_sync(linker, &options) +} + +/// Similar to [`add_to_linker_sync`], but with the ability to enable unstable features. +pub fn add_to_linker_with_options_sync( + linker: &mut wasmtime::component::Linker, + options: &crate::p2::bindings::sync::LinkOptions, +) -> anyhow::Result<()> { + let l = linker; + let io_closure = io_type_annotate::(|t| IoImpl(t)); + wasmtime_wasi_io::bindings::wasi::io::error::add_to_linker_get_host(l, io_closure)?; + + crate::p2::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?; + crate::p2::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?; + + let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); + + crate::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sync::filesystem::types::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::insecure::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; + crate::p2::bindings::cli::environment::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sync::sockets::tcp::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sync::sockets::udp::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; + crate::p2::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; + crate::p2::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; + Ok(()) +} + +// NB: workaround some rustc inference - a future refactoring may make this +// obsolete. +fn io_type_annotate(val: F) -> F +where + F: Fn(&mut T) -> IoImpl<&mut T>, +{ + val +} +fn type_annotate(val: F) -> F +where + F: Fn(&mut T) -> WasiImpl<&mut T>, +{ + val +} diff --git a/crates/wasi/src/p2/network.rs b/crates/wasi/src/p2/network.rs new file mode 100644 index 000000000000..5f53986f33b3 --- /dev/null +++ b/crates/wasi/src/p2/network.rs @@ -0,0 +1,44 @@ +use crate::p2::bindings::sockets::network::{ErrorCode, Ipv4Address, Ipv6Address}; +use crate::TrappableError; + +pub type SocketResult = Result; + +pub type SocketError = TrappableError; + +impl From for SocketError { + fn from(error: wasmtime::component::ResourceTableError) -> Self { + Self::trap(error) + } +} + +impl From for SocketError { + fn from(error: std::io::Error) -> Self { + ErrorCode::from(error).into() + } +} + +impl From for SocketError { + fn from(error: rustix::io::Errno) -> Self { + ErrorCode::from(error).into() + } +} + +pub(crate) fn to_ipv4_addr(addr: Ipv4Address) -> std::net::Ipv4Addr { + let (x0, x1, x2, x3) = addr; + std::net::Ipv4Addr::new(x0, x1, x2, x3) +} + +pub(crate) fn from_ipv4_addr(addr: std::net::Ipv4Addr) -> Ipv4Address { + let [x0, x1, x2, x3] = addr.octets(); + (x0, x1, x2, x3) +} + +pub(crate) fn to_ipv6_addr(addr: Ipv6Address) -> std::net::Ipv6Addr { + let (x0, x1, x2, x3, x4, x5, x6, x7) = addr; + std::net::Ipv6Addr::new(x0, x1, x2, x3, x4, x5, x6, x7) +} + +pub(crate) fn from_ipv6_addr(addr: std::net::Ipv6Addr) -> Ipv6Address { + let [x0, x1, x2, x3, x4, x5, x6, x7] = addr.segments(); + (x0, x1, x2, x3, x4, x5, x6, x7) +} diff --git a/crates/wasi/src/pipe.rs b/crates/wasi/src/p2/pipe.rs similarity index 99% rename from crates/wasi/src/pipe.rs rename to crates/wasi/src/p2/pipe.rs index afd627defc2c..0b74a8f39058 100644 --- a/crates/wasi/src/pipe.rs +++ b/crates/wasi/src/p2/pipe.rs @@ -16,7 +16,7 @@ use wasmtime_wasi_io::{ streams::{InputStream, OutputStream, StreamError}, }; -pub use crate::write_stream::AsyncWriteStream; +pub use crate::p2::write_stream::AsyncWriteStream; #[derive(Debug, Clone)] pub struct MemoryInputPipe { diff --git a/crates/wasi/src/poll.rs b/crates/wasi/src/p2/poll.rs similarity index 86% rename from crates/wasi/src/poll.rs rename to crates/wasi/src/p2/poll.rs index ee5fd9c44e5c..ed9f03676c19 100644 --- a/crates/wasi/src/poll.rs +++ b/crates/wasi/src/p2/poll.rs @@ -4,7 +4,7 @@ use wasmtime_wasi_io::{bindings::wasi::io::poll as async_poll, poll::DynPollable use anyhow::Result; use wasmtime::component::Resource; -impl crate::bindings::sync::io::poll::Host for IoImpl +impl crate::p2::bindings::sync::io::poll::Host for IoImpl where T: IoView, { @@ -13,7 +13,7 @@ where } } -impl crate::bindings::sync::io::poll::HostPollable for IoImpl +impl crate::p2::bindings::sync::io::poll::HostPollable for IoImpl where T: IoView, { diff --git a/crates/wasi/src/stdio.rs b/crates/wasi/src/p2/stdio.rs similarity index 95% rename from crates/wasi/src/stdio.rs rename to crates/wasi/src/p2/stdio.rs index 05c7abafadeb..8816e1543594 100644 --- a/crates/wasi/src/stdio.rs +++ b/crates/wasi/src/p2/stdio.rs @@ -1,9 +1,9 @@ -use crate::bindings::cli::{ +use crate::p2::bindings::cli::{ stderr, stdin, stdout, terminal_input, terminal_output, terminal_stderr, terminal_stdin, terminal_stdout, }; -use crate::pipe; -use crate::{ +use crate::p2::pipe; +use crate::p2::{ InputStream, IoView, OutputStream, Pollable, StreamError, StreamResult, WasiImpl, WasiView, }; use bytes::Bytes; @@ -58,7 +58,7 @@ impl StdinStream for pipe::ClosedInputStream { } } -/// An impl of [`StdinStream`] built on top of [`crate::pipe::AsyncReadStream`]. +/// An impl of [`StdinStream`] built on top of [`crate::p2::pipe::AsyncReadStream`]. // // Note the usage of `tokio::sync::Mutex` here as opposed to a // `std::sync::Mutex`. This is intentionally done to implement the `Pollable` @@ -86,10 +86,10 @@ impl StdinStream for pipe::ClosedInputStream { // between the two at this time. This may all change in the future with WASI // 0.3, but perhaps we'll have a better story for stdio at that time (see the // doc block on the `OutputStream` impl below) -pub struct AsyncStdinStream(Arc>); +pub struct AsyncStdinStream(Arc>); impl AsyncStdinStream { - pub fn new(s: crate::pipe::AsyncReadStream) -> Self { + pub fn new(s: crate::p2::pipe::AsyncReadStream) -> Self { Self(Arc::new(Mutex::new(s))) } } @@ -253,7 +253,7 @@ pub struct Stdout; /// Returns a stream that represents the host's standard out. /// /// Suitable for passing to -/// [`WasiCtxBuilder::stdout`](crate::WasiCtxBuilder::stdout). +/// [`WasiCtxBuilder::stdout`](crate::p2::WasiCtxBuilder::stdout). pub fn stdout() -> Stdout { Stdout } @@ -277,7 +277,7 @@ pub struct Stderr; /// Returns a stream that represents the host's standard err. /// /// Suitable for passing to -/// [`WasiCtxBuilder::stderr`](crate::WasiCtxBuilder::stderr). +/// [`WasiCtxBuilder::stderr`](crate::p2::WasiCtxBuilder::stderr). pub fn stderr() -> Stderr { Stderr } @@ -326,17 +326,17 @@ impl Pollable for StdioOutputStream { async fn ready(&mut self) {} } -/// A wrapper of [`crate::pipe::AsyncWriteStream`] that implements +/// A wrapper of [`crate::p2::pipe::AsyncWriteStream`] that implements /// [`StdoutStream`]. Note that the [`OutputStream`] impl for this is not /// correct when used for interleaved async IO. // // Note that the use of `tokio::sync::Mutex` here is intentional, in addition to // the `try_lock()` calls below in the implementation of `OutputStream`. For // more information see the documentation on `AsyncStdinStream`. -pub struct AsyncStdoutStream(Arc>); +pub struct AsyncStdoutStream(Arc>); impl AsyncStdoutStream { - pub fn new(s: crate::pipe::AsyncWriteStream) -> Self { + pub fn new(s: crate::p2::pipe::AsyncWriteStream) -> Self { Self(Arc::new(Mutex::new(s))) } } @@ -504,9 +504,9 @@ where #[cfg(test)] mod test { - use crate::stdio::StdoutStream; - use crate::write_stream::AsyncWriteStream; - use crate::{AsyncStdoutStream, OutputStream}; + use crate::p2::stdio::StdoutStream; + use crate::p2::write_stream::AsyncWriteStream; + use crate::p2::{AsyncStdoutStream, OutputStream}; use anyhow::Result; use bytes::Bytes; use tokio::io::AsyncReadExt; @@ -560,7 +560,8 @@ mod test { let file = tokio::fs::File::open(&path) .await .expect("open created file"); - let stdin_stream = super::AsyncStdinStream::new(crate::pipe::AsyncReadStream::new(file)); + let stdin_stream = + super::AsyncStdinStream::new(crate::p2::pipe::AsyncReadStream::new(file)); use super::StdinStream; diff --git a/crates/wasi/src/stdio/worker_thread_stdin.rs b/crates/wasi/src/p2/stdio/worker_thread_stdin.rs similarity index 98% rename from crates/wasi/src/stdio/worker_thread_stdin.rs rename to crates/wasi/src/p2/stdio/worker_thread_stdin.rs index 190069e18962..bf7bef781b1e 100644 --- a/crates/wasi/src/stdio/worker_thread_stdin.rs +++ b/crates/wasi/src/p2/stdio/worker_thread_stdin.rs @@ -23,7 +23,7 @@ //! This module is one that's likely to change over time though as new systems //! are encountered along with preexisting bugs. -use crate::stdio::StdinStream; +use crate::p2::stdio::StdinStream; use bytes::{Bytes, BytesMut}; use std::io::{IsTerminal, Read}; use std::mem; @@ -105,7 +105,7 @@ pub struct Stdin; /// Returns a stream that represents the host's standard input. /// /// Suitable for passing to -/// [`WasiCtxBuilder::stdin`](crate::WasiCtxBuilder::stdin). +/// [`WasiCtxBuilder::stdin`](crate::p2::WasiCtxBuilder::stdin). pub fn stdin() -> Stdin { Stdin } diff --git a/crates/wasi/src/tcp.rs b/crates/wasi/src/p2/tcp.rs similarity index 99% rename from crates/wasi/src/tcp.rs rename to crates/wasi/src/p2/tcp.rs index 359a5fe57757..8662ded7b52b 100644 --- a/crates/wasi/src/tcp.rs +++ b/crates/wasi/src/p2/tcp.rs @@ -1,11 +1,11 @@ -use crate::bindings::sockets::tcp::ErrorCode; -use crate::host::network; -use crate::network::SocketAddressFamily; -use crate::runtime::{with_ambient_tokio_runtime, AbortOnDropJoinHandle}; -use crate::{ +use crate::net::{SocketAddressFamily, DEFAULT_TCP_BACKLOG}; +use crate::p2::bindings::sockets::tcp::ErrorCode; +use crate::p2::host::network; +use crate::p2::{ DynInputStream, DynOutputStream, InputStream, OutputStream, Pollable, SocketError, SocketResult, StreamError, }; +use crate::runtime::{with_ambient_tokio_runtime, AbortOnDropJoinHandle}; use anyhow::Result; use cap_net_ext::AddressFamily; use futures::Future; @@ -21,9 +21,6 @@ use std::sync::Arc; use std::task::Poll; use tokio::sync::Mutex; -/// Value taken from rust std library. -const DEFAULT_BACKLOG: u32 = 128; - /// The state of a TCP socket. /// /// This represents the various states a socket can be in during the @@ -132,7 +129,7 @@ impl TcpSocket { fn from_state(state: TcpState, family: SocketAddressFamily) -> io::Result { Ok(Self { tcp_state: state, - listen_backlog_size: DEFAULT_BACKLOG, + listen_backlog_size: DEFAULT_TCP_BACKLOG, family, #[cfg(target_os = "macos")] receive_buffer_size: None, @@ -146,7 +143,7 @@ impl TcpSocket { } fn as_std_view(&self) -> SocketResult> { - use crate::bindings::sockets::network::ErrorCode; + use crate::p2::bindings::sockets::network::ErrorCode; match &self.tcp_state { TcpState::Default(socket) | TcpState::Bound(socket) => { diff --git a/crates/wasi/src/udp.rs b/crates/wasi/src/p2/udp.rs similarity index 97% rename from crates/wasi/src/udp.rs rename to crates/wasi/src/p2/udp.rs index 04e0981b95ee..3d98e56ac7a2 100644 --- a/crates/wasi/src/udp.rs +++ b/crates/wasi/src/p2/udp.rs @@ -1,4 +1,5 @@ -use crate::host::network::util; +use crate::net::{SocketAddrCheck, SocketAddressFamily}; +use crate::p2::host::network::util; use crate::runtime::with_ambient_tokio_runtime; use async_trait::async_trait; use cap_net_ext::{AddressFamily, Blocking}; @@ -8,8 +9,6 @@ use std::net::SocketAddr; use std::sync::Arc; use wasmtime_wasi_io::poll::Pollable; -use super::network::{SocketAddrCheck, SocketAddressFamily}; - /// The state of a UDP socket. /// /// This represents the various states a socket can be in during the diff --git a/crates/wasi/src/view.rs b/crates/wasi/src/p2/view.rs similarity index 82% rename from crates/wasi/src/view.rs rename to crates/wasi/src/p2/view.rs index 5ec710958b13..cf9b1acd96e3 100644 --- a/crates/wasi/src/view.rs +++ b/crates/wasi/src/p2/view.rs @@ -1,4 +1,4 @@ -use crate::ctx::WasiCtx; +use crate::p2::ctx::WasiCtx; use wasmtime::component::ResourceTable; pub use wasmtime_wasi_io::{IoImpl, IoView}; @@ -9,8 +9,8 @@ pub use wasmtime_wasi_io::{IoImpl, IoView}; /// [`WasiCtx`]. The `T` type [`Store`][`Store`] is defined in each /// embedding of Wasmtime. These implementations are connected to the /// [`Linker`][`Linker`] by the -/// [`add_to_linker_sync`](crate::add_to_linker_sync) and -/// [`add_to_linker_async`](crate::add_to_linker_async) functions. +/// [`add_to_linker_sync`](crate::p2::add_to_linker_sync) and +/// [`add_to_linker_async`](crate::p2::add_to_linker_async) functions. /// /// The [`WasiView`] trait implies the [`IoView`] trait, so each `T` must /// also contain a [`ResourceTable`] and impl `IoView`. @@ -18,7 +18,8 @@ pub use wasmtime_wasi_io::{IoImpl, IoView}; /// # Example /// /// ``` -/// use wasmtime_wasi::{WasiCtx, ResourceTable, WasiView, IoView, WasiCtxBuilder}; +/// use wasmtime_wasi::ResourceTable; +/// use wasmtime_wasi::p2::{WasiCtx, WasiView, IoView, WasiCtxBuilder}; /// /// struct MyState { /// ctx: WasiCtx, @@ -59,12 +60,12 @@ impl WasiView for Box { /// /// This type is used as the basis for the implementation of all `Host` traits /// generated by `bindgen!` for WASI interfaces. This is used automatically with -/// [`add_to_linker_sync`](crate::add_to_linker_sync) and -/// [`add_to_linker_async`](crate::add_to_linker_async). +/// [`add_to_linker_sync`](crate::p2::add_to_linker_sync) and +/// [`add_to_linker_async`](crate::p2::add_to_linker_async). /// /// This type is otherwise provided if you're calling the `add_to_linker` /// functions generated by `bindgen!` from the [`bindings` -/// module](crate::bindings). In this situation you'll want to create a value of +/// module](crate::p2::bindings). In this situation you'll want to create a value of /// this type in the closures added to a `Linker`. #[repr(transparent)] pub struct WasiImpl(pub IoImpl); diff --git a/crates/wasi/wit/deps/cli/command.wit b/crates/wasi/src/p2/wit/deps/cli/command.wit similarity index 100% rename from crates/wasi/wit/deps/cli/command.wit rename to crates/wasi/src/p2/wit/deps/cli/command.wit diff --git a/crates/wasi/wit/deps/cli/environment.wit b/crates/wasi/src/p2/wit/deps/cli/environment.wit similarity index 100% rename from crates/wasi/wit/deps/cli/environment.wit rename to crates/wasi/src/p2/wit/deps/cli/environment.wit diff --git a/crates/wasi/wit/deps/cli/exit.wit b/crates/wasi/src/p2/wit/deps/cli/exit.wit similarity index 100% rename from crates/wasi/wit/deps/cli/exit.wit rename to crates/wasi/src/p2/wit/deps/cli/exit.wit diff --git a/crates/wasi/wit/deps/cli/imports.wit b/crates/wasi/src/p2/wit/deps/cli/imports.wit similarity index 100% rename from crates/wasi/wit/deps/cli/imports.wit rename to crates/wasi/src/p2/wit/deps/cli/imports.wit diff --git a/crates/wasi/wit/deps/cli/run.wit b/crates/wasi/src/p2/wit/deps/cli/run.wit similarity index 100% rename from crates/wasi/wit/deps/cli/run.wit rename to crates/wasi/src/p2/wit/deps/cli/run.wit diff --git a/crates/wasi/wit/deps/cli/stdio.wit b/crates/wasi/src/p2/wit/deps/cli/stdio.wit similarity index 100% rename from crates/wasi/wit/deps/cli/stdio.wit rename to crates/wasi/src/p2/wit/deps/cli/stdio.wit diff --git a/crates/wasi/wit/deps/cli/terminal.wit b/crates/wasi/src/p2/wit/deps/cli/terminal.wit similarity index 100% rename from crates/wasi/wit/deps/cli/terminal.wit rename to crates/wasi/src/p2/wit/deps/cli/terminal.wit diff --git a/crates/wasi/wit/deps/clocks/monotonic-clock.wit b/crates/wasi/src/p2/wit/deps/clocks/monotonic-clock.wit similarity index 100% rename from crates/wasi/wit/deps/clocks/monotonic-clock.wit rename to crates/wasi/src/p2/wit/deps/clocks/monotonic-clock.wit diff --git a/crates/wasi/wit/deps/clocks/timezone.wit b/crates/wasi/src/p2/wit/deps/clocks/timezone.wit similarity index 100% rename from crates/wasi/wit/deps/clocks/timezone.wit rename to crates/wasi/src/p2/wit/deps/clocks/timezone.wit diff --git a/crates/wasi/wit/deps/clocks/wall-clock.wit b/crates/wasi/src/p2/wit/deps/clocks/wall-clock.wit similarity index 100% rename from crates/wasi/wit/deps/clocks/wall-clock.wit rename to crates/wasi/src/p2/wit/deps/clocks/wall-clock.wit diff --git a/crates/wasi/wit/deps/clocks/world.wit b/crates/wasi/src/p2/wit/deps/clocks/world.wit similarity index 100% rename from crates/wasi/wit/deps/clocks/world.wit rename to crates/wasi/src/p2/wit/deps/clocks/world.wit diff --git a/crates/wasi/wit/deps/filesystem/preopens.wit b/crates/wasi/src/p2/wit/deps/filesystem/preopens.wit similarity index 100% rename from crates/wasi/wit/deps/filesystem/preopens.wit rename to crates/wasi/src/p2/wit/deps/filesystem/preopens.wit diff --git a/crates/wasi/wit/deps/filesystem/types.wit b/crates/wasi/src/p2/wit/deps/filesystem/types.wit similarity index 100% rename from crates/wasi/wit/deps/filesystem/types.wit rename to crates/wasi/src/p2/wit/deps/filesystem/types.wit diff --git a/crates/wasi/wit/deps/filesystem/world.wit b/crates/wasi/src/p2/wit/deps/filesystem/world.wit similarity index 100% rename from crates/wasi/wit/deps/filesystem/world.wit rename to crates/wasi/src/p2/wit/deps/filesystem/world.wit diff --git a/crates/wasi/wit/deps/io/error.wit b/crates/wasi/src/p2/wit/deps/io/error.wit similarity index 100% rename from crates/wasi/wit/deps/io/error.wit rename to crates/wasi/src/p2/wit/deps/io/error.wit diff --git a/crates/wasi/wit/deps/io/poll.wit b/crates/wasi/src/p2/wit/deps/io/poll.wit similarity index 100% rename from crates/wasi/wit/deps/io/poll.wit rename to crates/wasi/src/p2/wit/deps/io/poll.wit diff --git a/crates/wasi/wit/deps/io/streams.wit b/crates/wasi/src/p2/wit/deps/io/streams.wit similarity index 100% rename from crates/wasi/wit/deps/io/streams.wit rename to crates/wasi/src/p2/wit/deps/io/streams.wit diff --git a/crates/wasi/wit/deps/io/world.wit b/crates/wasi/src/p2/wit/deps/io/world.wit similarity index 100% rename from crates/wasi/wit/deps/io/world.wit rename to crates/wasi/src/p2/wit/deps/io/world.wit diff --git a/crates/wasi/wit/deps/random/insecure-seed.wit b/crates/wasi/src/p2/wit/deps/random/insecure-seed.wit similarity index 100% rename from crates/wasi/wit/deps/random/insecure-seed.wit rename to crates/wasi/src/p2/wit/deps/random/insecure-seed.wit diff --git a/crates/wasi/wit/deps/random/insecure.wit b/crates/wasi/src/p2/wit/deps/random/insecure.wit similarity index 100% rename from crates/wasi/wit/deps/random/insecure.wit rename to crates/wasi/src/p2/wit/deps/random/insecure.wit diff --git a/crates/wasi/wit/deps/random/random.wit b/crates/wasi/src/p2/wit/deps/random/random.wit similarity index 100% rename from crates/wasi/wit/deps/random/random.wit rename to crates/wasi/src/p2/wit/deps/random/random.wit diff --git a/crates/wasi/wit/deps/random/world.wit b/crates/wasi/src/p2/wit/deps/random/world.wit similarity index 100% rename from crates/wasi/wit/deps/random/world.wit rename to crates/wasi/src/p2/wit/deps/random/world.wit diff --git a/crates/wasi/wit/deps/sockets/instance-network.wit b/crates/wasi/src/p2/wit/deps/sockets/instance-network.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/instance-network.wit rename to crates/wasi/src/p2/wit/deps/sockets/instance-network.wit diff --git a/crates/wasi/wit/deps/sockets/ip-name-lookup.wit b/crates/wasi/src/p2/wit/deps/sockets/ip-name-lookup.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/ip-name-lookup.wit rename to crates/wasi/src/p2/wit/deps/sockets/ip-name-lookup.wit diff --git a/crates/wasi/wit/deps/sockets/network.wit b/crates/wasi/src/p2/wit/deps/sockets/network.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/network.wit rename to crates/wasi/src/p2/wit/deps/sockets/network.wit diff --git a/crates/wasi/wit/deps/sockets/tcp-create-socket.wit b/crates/wasi/src/p2/wit/deps/sockets/tcp-create-socket.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/tcp-create-socket.wit rename to crates/wasi/src/p2/wit/deps/sockets/tcp-create-socket.wit diff --git a/crates/wasi/wit/deps/sockets/tcp.wit b/crates/wasi/src/p2/wit/deps/sockets/tcp.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/tcp.wit rename to crates/wasi/src/p2/wit/deps/sockets/tcp.wit diff --git a/crates/wasi/wit/deps/sockets/udp-create-socket.wit b/crates/wasi/src/p2/wit/deps/sockets/udp-create-socket.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/udp-create-socket.wit rename to crates/wasi/src/p2/wit/deps/sockets/udp-create-socket.wit diff --git a/crates/wasi/wit/deps/sockets/udp.wit b/crates/wasi/src/p2/wit/deps/sockets/udp.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/udp.wit rename to crates/wasi/src/p2/wit/deps/sockets/udp.wit diff --git a/crates/wasi/wit/deps/sockets/world.wit b/crates/wasi/src/p2/wit/deps/sockets/world.wit similarity index 100% rename from crates/wasi/wit/deps/sockets/world.wit rename to crates/wasi/src/p2/wit/deps/sockets/world.wit diff --git a/crates/wasi/wit/test.wit b/crates/wasi/src/p2/wit/test.wit similarity index 100% rename from crates/wasi/wit/test.wit rename to crates/wasi/src/p2/wit/test.wit diff --git a/crates/wasi/wit/world.wit b/crates/wasi/src/p2/wit/world.wit similarity index 100% rename from crates/wasi/wit/world.wit rename to crates/wasi/src/p2/wit/world.wit diff --git a/crates/wasi/src/write_stream.rs b/crates/wasi/src/p2/write_stream.rs similarity index 99% rename from crates/wasi/src/write_stream.rs rename to crates/wasi/src/p2/write_stream.rs index f1c6c3d66730..8545ceb0558b 100644 --- a/crates/wasi/src/write_stream.rs +++ b/crates/wasi/src/p2/write_stream.rs @@ -1,4 +1,4 @@ -use crate::{OutputStream, Pollable, StreamError}; +use crate::p2::{OutputStream, Pollable, StreamError}; use anyhow::anyhow; use bytes::Bytes; use std::sync::{Arc, Mutex}; diff --git a/crates/wasi/src/preview1.rs b/crates/wasi/src/preview1.rs index 07b7e3e2a858..c591f0014b07 100644 --- a/crates/wasi/src/preview1.rs +++ b/crates/wasi/src/preview1.rs @@ -19,8 +19,8 @@ //! support in this module is enough to run wasm modules but any customization //! beyond that [`WasiCtxBuilder`] already supports is not possible yet. //! -//! [`WasiCtxBuilder`]: crate::WasiCtxBuilder -//! [`build_p1`]: crate::WasiCtxBuilder::build_p1 +//! [`WasiCtxBuilder`]: crate::p2::WasiCtxBuilder +//! [`build_p1`]: crate::p2::WasiCtxBuilder::build_p1 //! [`Config::async_support`]: wasmtime::Config::async_support //! //! # Components vs Modules @@ -36,7 +36,7 @@ //! ```no_run //! use wasmtime::{Result, Engine, Linker, Module, Store}; //! use wasmtime_wasi::preview1::{self, WasiP1Ctx}; -//! use wasmtime_wasi::WasiCtxBuilder; +//! use wasmtime_wasi::p2::WasiCtxBuilder; //! //! // An example of executing a WASIp1 "command" //! fn main() -> Result<()> { @@ -63,7 +63,7 @@ //! } //! ``` -use crate::bindings::{ +use crate::p2::bindings::{ cli::{ stderr::Host as _, stdin::Host as _, stdout::Host as _, terminal_input, terminal_output, terminal_stderr::Host as _, terminal_stdin::Host as _, terminal_stdout::Host as _, @@ -71,7 +71,8 @@ use crate::bindings::{ clocks::{monotonic_clock, wall_clock}, filesystem::{preopens::Host as _, types as filesystem}, }; -use crate::{FsError, IsATTY, ResourceTable, WasiCtx, WasiImpl, WasiView}; +use crate::p2::{FsError, IsATTY, WasiCtx, WasiImpl, WasiView}; +use crate::ResourceTable; use anyhow::{bail, Context}; use std::collections::{BTreeMap, HashSet}; use std::mem::{self, size_of, size_of_val}; @@ -90,9 +91,9 @@ use wiggle::tracing::instrument; use wiggle::{GuestError, GuestMemory, GuestPtr, GuestType}; // Bring all WASI traits in scope that this implementation builds on. -use crate::bindings::cli::environment::Host as _; -use crate::bindings::filesystem::types::HostDescriptor as _; -use crate::bindings::random::random::Host as _; +use crate::p2::bindings::cli::environment::Host as _; +use crate::p2::bindings::filesystem::types::HostDescriptor as _; +use crate::p2::bindings::random::random::Host as _; use wasmtime_wasi_io::bindings::wasi::io::poll::Host as _; /// Structure containing state for WASIp1. @@ -105,15 +106,15 @@ use wasmtime_wasi_io::bindings::wasi::io::poll::Host as _; /// Instances of [`WasiP1Ctx`] are typically stored within the `T` of /// [`Store`](wasmtime::Store). /// -/// [`WasiCtxBuilder::build_p1`]: crate::WasiCtxBuilder::build_p1 -/// [`WasiCtxBuilder`]: crate::WasiCtxBuilder +/// [`WasiCtxBuilder::build_p1`]: crate::p2::WasiCtxBuilder::build_p1 +/// [`WasiCtxBuilder`]: crate::p2::WasiCtxBuilder /// /// # Examples /// /// ```no_run /// use wasmtime::{Result, Linker}; /// use wasmtime_wasi::preview1::{self, WasiP1Ctx}; -/// use wasmtime_wasi::WasiCtxBuilder; +/// use wasmtime_wasi::p2::WasiCtxBuilder; /// /// struct MyState { /// // ... custom state as necessary ... @@ -175,7 +176,7 @@ impl WasiView for WasiP1Ctx { #[derive(Debug)] struct File { - /// The handle to the preview2 descriptor of type [`crate::filesystem::Descriptor::File`]. + /// The handle to the preview2 descriptor of type [`crate::p2::filesystem::Descriptor::File`]. fd: Resource, /// The current-position pointer. @@ -266,14 +267,14 @@ enum Descriptor { stream: Resource, isatty: IsATTY, }, - /// A fd of type [`crate::filesystem::Descriptor::Dir`] + /// A fd of type [`crate::p2::filesystem::Descriptor::Dir`] Directory { fd: Resource, /// The path this directory was preopened as. /// `None` means this directory was opened using `open-at`. preopen_path: Option, }, - /// A fd of type [`crate::filesystem::Descriptor::File`] + /// A fd of type [`crate::p2::filesystem::Descriptor::File`] File(File), } @@ -550,7 +551,7 @@ impl WasiP1Ctx { /// Lazily initializes [`WasiPreview1Adapter`] returned by [`WasiPreview1View::adapter_mut`] /// and returns [`filesystem::Descriptor`] corresponding to `fd` - /// if it describes a [`Descriptor::File`] of [`crate::filesystem::File`] type + /// if it describes a [`Descriptor::File`] of [`crate::p2::filesystem::File`] type fn get_file_fd( &mut self, fd: types::Fd, @@ -563,7 +564,7 @@ impl WasiP1Ctx { /// Lazily initializes [`WasiPreview1Adapter`] returned by [`WasiPreview1View::adapter_mut`] /// and returns [`filesystem::Descriptor`] corresponding to `fd` /// if it describes a [`Descriptor::File`] or [`Descriptor::PreopenDirectory`] - /// of [`crate::filesystem::Dir`] type + /// of [`crate::p2::filesystem::Dir`] type fn get_dir_fd( &mut self, fd: types::Fd, @@ -816,7 +817,7 @@ pub fn add_to_linker_sync( linker: &mut wasmtime::Linker, f: impl Fn(&mut T) -> &mut WasiP1Ctx + Copy + Send + Sync + 'static, ) -> anyhow::Result<()> { - crate::preview1::sync::add_wasi_snapshot_preview1_to_linker(linker, f) + sync::add_wasi_snapshot_preview1_to_linker(linker, f) } // Generate the wasi_snapshot_preview1::WasiSnapshotPreview1 trait, @@ -2157,11 +2158,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx { .await?; let mut t = self.transact()?; let desc = match t.view.table().get(&fd)? { - crate::filesystem::Descriptor::Dir(_) => Descriptor::Directory { + crate::p2::filesystem::Descriptor::Dir(_) => Descriptor::Directory { fd, preopen_path: None, }, - crate::filesystem::Descriptor::File(_) => Descriptor::File(File { + crate::p2::filesystem::Descriptor::File(_) => Descriptor::File(File { fd, position: Default::default(), append: fdflags.contains(types::Fdflags::APPEND), diff --git a/crates/wasi/tests/all/main.rs b/crates/wasi/tests/all/main.rs index 9a7d0782ad83..ee9f28d290c2 100644 --- a/crates/wasi/tests/all/main.rs +++ b/crates/wasi/tests/all/main.rs @@ -1,93 +1 @@ -use anyhow::Result; -use tempfile::TempDir; -use wasmtime::{ - component::{Component, Linker, ResourceTable}, - Engine, Store, -}; -use wasmtime_wasi::preview1::WasiP1Ctx; -use wasmtime_wasi::{ - pipe::MemoryOutputPipe, DirPerms, FilePerms, IoView, WasiCtx, WasiCtxBuilder, WasiView, -}; - -struct Ctx { - stdout: MemoryOutputPipe, - stderr: MemoryOutputPipe, - wasi: WasiP1Ctx, -} - -impl IoView for Ctx { - fn table(&mut self) -> &mut ResourceTable { - self.wasi.table() - } -} -impl WasiView for Ctx { - fn ctx(&mut self) -> &mut WasiCtx { - self.wasi.ctx() - } -} - -fn prepare_workspace(exe_name: &str) -> Result { - let prefix = format!("wasi_components_{exe_name}_"); - let tempdir = tempfile::Builder::new().prefix(&prefix).tempdir()?; - Ok(tempdir) -} - -fn store( - engine: &Engine, - name: &str, - configure: impl FnOnce(&mut WasiCtxBuilder), -) -> Result<(Store, TempDir)> { - let stdout = MemoryOutputPipe::new(4096); - let stderr = MemoryOutputPipe::new(4096); - let workspace = prepare_workspace(name)?; - - // Create our wasi context. - let mut builder = WasiCtxBuilder::new(); - builder.stdout(stdout.clone()).stderr(stderr.clone()); - - builder - .args(&[name, "."]) - .inherit_network() - .allow_ip_name_lookup(true); - println!("preopen: {workspace:?}"); - builder.preopened_dir(workspace.path(), ".", DirPerms::all(), FilePerms::all())?; - for (var, val) in test_programs_artifacts::wasi_tests_environment() { - builder.env(var, val); - } - - configure(&mut builder); - let ctx = Ctx { - wasi: builder.build_p1(), - stderr, - stdout, - }; - - Ok((Store::new(&engine, ctx), workspace)) -} - -impl Drop for Ctx { - fn drop(&mut self) { - let stdout = self.stdout.contents(); - if !stdout.is_empty() { - println!("[guest] stdout:\n{}\n===", String::from_utf8_lossy(&stdout)); - } - let stderr = self.stderr.contents(); - if !stderr.is_empty() { - println!("[guest] stderr:\n{}\n===", String::from_utf8_lossy(&stderr)); - } - } -} - -// Assert that each of `sync` and `async` below are testing everything through -// assertion of the existence of the test function itself. -macro_rules! assert_test_exists { - ($name:ident) => { - #[expect(unused_imports, reason = "just here to ensure a name exists")] - use self::$name as _; - }; -} - -mod api; -mod async_; -mod preview1; -mod sync; +mod p2; diff --git a/crates/wasi/tests/all/api.rs b/crates/wasi/tests/all/p2/api.rs similarity index 93% rename from crates/wasi/tests/all/api.rs rename to crates/wasi/tests/all/p2/api.rs index 1218966d89c3..b0afe01abd19 100644 --- a/crates/wasi/tests/all/api.rs +++ b/crates/wasi/tests/all/p2/api.rs @@ -6,13 +6,13 @@ use std::sync::Mutex; use std::time::Duration; use wasmtime::component::{Component, Linker, ResourceTable}; use wasmtime::Store; -use wasmtime_wasi::bindings::Command; -use wasmtime_wasi::{ +use wasmtime_wasi::p2::bindings::Command; +use wasmtime_wasi::p2::{ add_to_linker_async, bindings::{clocks::wall_clock, filesystem::types as filesystem}, - DirPerms, FilePerms, HostMonotonicClock, HostWallClock, IoView, WasiCtx, WasiCtxBuilder, - WasiView, + IoView, WasiCtx, WasiCtxBuilder, WasiView, }; +use wasmtime_wasi::{DirPerms, FilePerms, HostMonotonicClock, HostWallClock}; struct CommandCtx { table: ResourceTable, @@ -131,9 +131,10 @@ fn api_proxy_streaming() {} fn api_proxy_forward_request() {} wasmtime::component::bindgen!({ + path: "src/p2/wit", world: "test-reactor", async: true, - with: { "wasi": wasmtime_wasi::bindings }, + with: { "wasi": wasmtime_wasi::p2::bindings }, ownership: Borrowing { duplicate_if_necessary: false } @@ -167,8 +168,8 @@ async fn api_reactor() -> Result<()> { // `host` and `wasi-common` crate. // Note, this works because of the add_to_linker invocations using the // `host` crate for `streams`, not because of `with` in the bindgen macro. - let writepipe = wasmtime_wasi::pipe::MemoryOutputPipe::new(4096); - let stream: wasmtime_wasi::DynOutputStream = Box::new(writepipe.clone()); + let writepipe = wasmtime_wasi::p2::pipe::MemoryOutputPipe::new(4096); + let stream: wasmtime_wasi::p2::DynOutputStream = Box::new(writepipe.clone()); let table_ix = store.data_mut().table().push(stream)?; let r = reactor.call_write_strings_to(&mut store, table_ix).await?; assert_eq!(r, Ok(())); diff --git a/crates/wasi/tests/all/async_.rs b/crates/wasi/tests/all/p2/async_.rs similarity index 99% rename from crates/wasi/tests/all/async_.rs rename to crates/wasi/tests/all/p2/async_.rs index 7d25423726b2..5094e8add29d 100644 --- a/crates/wasi/tests/all/async_.rs +++ b/crates/wasi/tests/all/p2/async_.rs @@ -1,8 +1,8 @@ use super::*; use std::path::Path; use test_programs_artifacts::*; -use wasmtime_wasi::add_to_linker_async; -use wasmtime_wasi::bindings::Command; +use wasmtime_wasi::p2::add_to_linker_async; +use wasmtime_wasi::p2::bindings::Command; async fn run(path: &str, inherit_stdio: bool) -> Result<()> { let path = Path::new(path); diff --git a/crates/wasi/tests/all/p2/mod.rs b/crates/wasi/tests/all/p2/mod.rs new file mode 100644 index 000000000000..c01ce27dfb7e --- /dev/null +++ b/crates/wasi/tests/all/p2/mod.rs @@ -0,0 +1,92 @@ +use anyhow::Result; +use tempfile::TempDir; +use wasmtime::{ + component::{Component, Linker, ResourceTable}, + Engine, Store, +}; +use wasmtime_wasi::p2::{pipe::MemoryOutputPipe, IoView, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::preview1::WasiP1Ctx; +use wasmtime_wasi::{DirPerms, FilePerms}; + +struct Ctx { + stdout: MemoryOutputPipe, + stderr: MemoryOutputPipe, + wasi: WasiP1Ctx, +} + +impl IoView for Ctx { + fn table(&mut self) -> &mut ResourceTable { + self.wasi.table() + } +} +impl WasiView for Ctx { + fn ctx(&mut self) -> &mut WasiCtx { + self.wasi.ctx() + } +} + +fn prepare_workspace(exe_name: &str) -> Result { + let prefix = format!("wasi_components_{exe_name}_"); + let tempdir = tempfile::Builder::new().prefix(&prefix).tempdir()?; + Ok(tempdir) +} + +fn store( + engine: &Engine, + name: &str, + configure: impl FnOnce(&mut WasiCtxBuilder), +) -> Result<(Store, TempDir)> { + let stdout = MemoryOutputPipe::new(4096); + let stderr = MemoryOutputPipe::new(4096); + let workspace = prepare_workspace(name)?; + + // Create our wasi context. + let mut builder = WasiCtxBuilder::new(); + builder.stdout(stdout.clone()).stderr(stderr.clone()); + + builder + .args(&[name, "."]) + .inherit_network() + .allow_ip_name_lookup(true); + println!("preopen: {workspace:?}"); + builder.preopened_dir(workspace.path(), ".", DirPerms::all(), FilePerms::all())?; + for (var, val) in test_programs_artifacts::wasi_tests_environment() { + builder.env(var, val); + } + + configure(&mut builder); + let ctx = Ctx { + wasi: builder.build_p1(), + stderr, + stdout, + }; + + Ok((Store::new(&engine, ctx), workspace)) +} + +impl Drop for Ctx { + fn drop(&mut self) { + let stdout = self.stdout.contents(); + if !stdout.is_empty() { + println!("[guest] stdout:\n{}\n===", String::from_utf8_lossy(&stdout)); + } + let stderr = self.stderr.contents(); + if !stderr.is_empty() { + println!("[guest] stderr:\n{}\n===", String::from_utf8_lossy(&stderr)); + } + } +} + +// Assert that each of `sync` and `async` below are testing everything through +// assertion of the existence of the test function itself. +macro_rules! assert_test_exists { + ($name:ident) => { + #[expect(unused_imports, reason = "just here to ensure a name exists")] + use self::$name as _; + }; +} + +mod api; +mod async_; +mod preview1; +mod sync; diff --git a/crates/wasi/tests/all/preview1.rs b/crates/wasi/tests/all/p2/preview1.rs similarity index 100% rename from crates/wasi/tests/all/preview1.rs rename to crates/wasi/tests/all/p2/preview1.rs diff --git a/crates/wasi/tests/all/sync.rs b/crates/wasi/tests/all/p2/sync.rs similarity index 99% rename from crates/wasi/tests/all/sync.rs rename to crates/wasi/tests/all/p2/sync.rs index 5629d5d3ffd8..435de4959cb1 100644 --- a/crates/wasi/tests/all/sync.rs +++ b/crates/wasi/tests/all/p2/sync.rs @@ -1,8 +1,8 @@ use super::*; use std::path::Path; use test_programs_artifacts::*; -use wasmtime_wasi::add_to_linker_sync; -use wasmtime_wasi::bindings::sync::Command; +use wasmtime_wasi::p2::add_to_linker_sync; +use wasmtime_wasi::p2::bindings::sync::Command; fn run(path: &str, inherit_stdio: bool) -> Result<()> { let path = Path::new(path); diff --git a/crates/wasi/tests/process_stdin.rs b/crates/wasi/tests/process_stdin.rs index 99e85a3b261b..49d8c14e7bac 100644 --- a/crates/wasi/tests/process_stdin.rs +++ b/crates/wasi/tests/process_stdin.rs @@ -1,6 +1,6 @@ use std::io::{BufRead, Write}; use std::process::Command; -use wasmtime_wasi::{InputStream, Pollable}; +use wasmtime_wasi::p2::{InputStream, Pollable}; const VAR_NAME: &str = "__CHILD_PROCESS"; @@ -31,7 +31,7 @@ fn main() { .block_on(async { 'task: loop { println!("child: creating stdin"); - let mut stdin = wasmtime_wasi::stdin(); + let mut stdin = wasmtime_wasi::p2::stdin(); println!("child: checking that stdin is not ready"); assert!( diff --git a/crates/wasmtime/src/runtime/component/mod.rs b/crates/wasmtime/src/runtime/component/mod.rs index ab9f4bf7a66c..330137d13a08 100644 --- a/crates/wasmtime/src/runtime/component/mod.rs +++ b/crates/wasmtime/src/runtime/component/mod.rs @@ -352,13 +352,13 @@ pub(crate) use self::store::ComponentStoreData; /// // This can be used to indicate that entire interfaces have /// // bindings generated elsewhere with a path pointing to the /// // bindinges-generated module. -/// "wasi:random/random": wasmtime_wasi::bindings::random::random, +/// "wasi:random/random": wasmtime_wasi::p2::bindings::random::random, /// /// // Similarly entire packages can also be specified. -/// "wasi:cli": wasmtime_wasi::bindings::cli, +/// "wasi:cli": wasmtime_wasi::p2::bindings::cli, /// /// // Or, if applicable, entire namespaces can additionally be mapped. -/// "wasi": wasmtime_wasi::bindings, +/// "wasi": wasmtime_wasi::p2::bindings, /// /// // Versions are supported if multiple versions are in play: /// "wasi:http/types@0.2.0": wasmtime_wasi_http::bindings::http::types, diff --git a/examples/min-platform/embedding/src/wasi.rs b/examples/min-platform/embedding/src/wasi.rs index 71a9f27d236c..b92365a09b89 100644 --- a/examples/min-platform/embedding/src/wasi.rs +++ b/examples/min-platform/embedding/src/wasi.rs @@ -150,7 +150,7 @@ fn deserialize(engine: &Engine, component: &[u8]) -> Result> { // Generate bindings for the entire wasi:cli command world. We won't impl and // link with all of these generated bindings for the sake of this example. wasmtime::component::bindgen!({ - path: "../../../crates/wasi/wit", + path: "../../../crates/wasi/src/p2/wit", world: "wasi:cli/command", async: { only_imports: [] }, trappable_imports: true, diff --git a/examples/wasip1-async/main.rs b/examples/wasip1-async/main.rs index 1c266752af88..0f7495b71f10 100644 --- a/examples/wasip1-async/main.rs +++ b/examples/wasip1-async/main.rs @@ -9,8 +9,8 @@ You can execute this example with: use anyhow::Result; use wasmtime::{Config, Engine, Linker, Module, Store}; +use wasmtime_wasi::p2::WasiCtxBuilder; use wasmtime_wasi::preview1::{self, WasiP1Ctx}; -use wasmtime_wasi::WasiCtxBuilder; #[tokio::main] async fn main() -> Result<()> { diff --git a/examples/wasip2-async/main.rs b/examples/wasip2-async/main.rs index 9289773c3de5..f7fa35788381 100644 --- a/examples/wasip2-async/main.rs +++ b/examples/wasip2-async/main.rs @@ -9,13 +9,13 @@ You can execute this example with: use wasmtime::component::{Component, Linker, ResourceTable}; use wasmtime::*; -use wasmtime_wasi::bindings::Command; -use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::p2::bindings::Command; +use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; pub struct ComponentRunStates { // These two are required basically as a standard way to enable the impl of IoView and // WasiView. - // impl of WasiView is required by [`wasmtime_wasi::add_to_linker_sync`] + // impl of WasiView is required by [`wasmtime_wasi::p2::add_to_linker_sync`] pub wasi_ctx: WasiCtx, pub resource_table: ResourceTable, // You can add other custom host states if needed @@ -39,7 +39,7 @@ async fn main() -> Result<()> { config.async_support(true); let engine = Engine::new(&config)?; let mut linker = Linker::new(&engine); - wasmtime_wasi::add_to_linker_async(&mut linker)?; + wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; // Create a WASI context and put it in a Store; all instances in the store // share this context. `WasiCtxBuilder` provides a number of ways to diff --git a/examples/wasip2/main.rs b/examples/wasip2/main.rs index bda98b541d6c..57d1f8e59bc7 100644 --- a/examples/wasip2/main.rs +++ b/examples/wasip2/main.rs @@ -8,13 +8,13 @@ You can execute this example with: use wasmtime::component::{Component, Linker, ResourceTable}; use wasmtime::*; -use wasmtime_wasi::bindings::sync::Command; -use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::p2::bindings::sync::Command; +use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView}; pub struct ComponentRunStates { // These two are required basically as a standard way to enable the impl of IoView and // WasiView. - // impl of WasiView is required by [`wasmtime_wasi::add_to_linker_sync`] + // impl of WasiView is required by [`wasmtime_wasi::p2::add_to_linker_sync`] pub wasi_ctx: WasiCtx, pub resource_table: ResourceTable, // You can add other custom host states if needed @@ -35,7 +35,7 @@ fn main() -> Result<()> { // Define the WASI functions globally on the `Config`. let engine = Engine::default(); let mut linker = Linker::new(&engine); - wasmtime_wasi::add_to_linker_sync(&mut linker)?; + wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; // Create a WASI context and put it in a Store; all instances in the store // share this context. `WasiCtxBuilder` provides a number of ways to diff --git a/src/commands/run.rs b/src/commands/run.rs index a63b521a1230..3b945169c25f 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex}; use std::thread; use wasi_common::sync::{ambient_authority, Dir, TcpListener, WasiCtxBuilder}; use wasmtime::{Engine, Func, Module, Store, StoreLimits, Val, ValType}; -use wasmtime_wasi::{IoView, WasiView}; +use wasmtime_wasi::p2::{IoView, WasiView}; #[cfg(feature = "wasi-nn")] use wasmtime_wasi_nn::wit::WasiNnView; @@ -498,7 +498,7 @@ impl RunCommand { if self.invoke.is_some() { self.invoke_component(&mut *store, component, linker).await } else { - let command = wasmtime_wasi::bindings::Command::instantiate_async( + let command = wasmtime_wasi::p2::bindings::Command::instantiate_async( &mut *store, component, linker, @@ -804,7 +804,7 @@ impl RunCommand { #[cfg(feature = "component-model")] CliLinker::Component(linker) => { let link_options = self.run.compute_wasi_features(); - wasmtime_wasi::add_to_linker_with_options_async(linker, &link_options)?; + wasmtime_wasi::p2::add_to_linker_with_options_async(linker, &link_options)?; self.set_preview2_ctx(store)?; } } @@ -1045,7 +1045,7 @@ impl RunCommand { } fn set_preview2_ctx(&self, store: &mut Store) -> Result<()> { - let mut builder = wasmtime_wasi::WasiCtxBuilder::new(); + let mut builder = wasmtime_wasi::p2::WasiCtxBuilder::new(); builder.inherit_stdio().args(&self.compute_argv()?); self.run.configure_wasip2(&mut builder)?; let ctx = builder.build_p1(); @@ -1120,7 +1120,7 @@ impl IoView for Host { } } impl WasiView for Host { - fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx { + fn ctx(&mut self) -> &mut wasmtime_wasi::p2::WasiCtx { self.preview2_ctx().ctx() } } diff --git a/src/commands/serve.rs b/src/commands/serve.rs index c6d89a21bd16..2a6d93f5a01a 100644 --- a/src/commands/serve.rs +++ b/src/commands/serve.rs @@ -14,7 +14,7 @@ use std::{ use tokio::sync::Notify; use wasmtime::component::{Component, Linker}; use wasmtime::{Engine, Store, StoreLimits, UpdateDeadline}; -use wasmtime_wasi::{IoView, StreamError, StreamResult, WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::p2::{IoView, StreamError, StreamResult, WasiCtx, WasiCtxBuilder, WasiView}; use wasmtime_wasi_http::bindings::http::types::Scheme; use wasmtime_wasi_http::bindings::ProxyPre; use wasmtime_wasi_http::io::TokioIo; @@ -274,7 +274,7 @@ impl ServeCommand { // uses. if cli == Some(true) { let link_options = self.run.compute_wasi_features(); - wasmtime_wasi::add_to_linker_with_options_async(linker, &link_options)?; + wasmtime_wasi::p2::add_to_linker_with_options_async(linker, &link_options)?; wasmtime_wasi_http::add_only_http_to_linker_async(linker)?; } else { wasmtime_wasi_http::add_to_linker_async(linker)?; @@ -792,8 +792,8 @@ impl LogStream { } } -impl wasmtime_wasi::StdoutStream for LogStream { - fn stream(&self) -> Box { +impl wasmtime_wasi::p2::StdoutStream for LogStream { + fn stream(&self) -> Box { Box::new(self.clone()) } @@ -807,7 +807,7 @@ impl wasmtime_wasi::StdoutStream for LogStream { } } -impl wasmtime_wasi::OutputStream for LogStream { +impl wasmtime_wasi::p2::OutputStream for LogStream { fn write(&mut self, bytes: bytes::Bytes) -> StreamResult<()> { let mut bytes = &bytes[..]; @@ -849,7 +849,7 @@ impl wasmtime_wasi::OutputStream for LogStream { } #[async_trait::async_trait] -impl wasmtime_wasi::Pollable for LogStream { +impl wasmtime_wasi::p2::Pollable for LogStream { async fn ready(&mut self) {} } diff --git a/src/common.rs b/src/common.rs index 618ef7479aa2..8dad50575d02 100644 --- a/src/common.rs +++ b/src/common.rs @@ -6,8 +6,8 @@ use std::net::TcpListener; use std::{fs::File, path::Path, time::Duration}; use wasmtime::{Engine, Module, Precompiled, StoreLimits, StoreLimitsBuilder}; use wasmtime_cli_flags::{opt::WasmtimeOptionValue, CommonOptions}; -use wasmtime_wasi::bindings::LinkOptions; -use wasmtime_wasi::WasiCtxBuilder; +use wasmtime_wasi::p2::bindings::LinkOptions; +use wasmtime_wasi::p2::WasiCtxBuilder; #[cfg(feature = "component-model")] use wasmtime::component::Component; diff --git a/tests/all/host_funcs.rs b/tests/all/host_funcs.rs index a0797772a71b..f47e1d2c98f6 100644 --- a/tests/all/host_funcs.rs +++ b/tests/all/host_funcs.rs @@ -731,7 +731,7 @@ fn wasi_imports() -> Result<()> { )?; let module = Module::new(&engine, wasm)?; - let mut store = Store::new(&engine, wasmtime_wasi::WasiCtxBuilder::new().build_p1()); + let mut store = Store::new(&engine, wasmtime_wasi::p2::WasiCtxBuilder::new().build_p1()); let instance = linker.instantiate(&mut store, &module)?; let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?; diff --git a/tests/all/traps.rs b/tests/all/traps.rs index 8da2418f940d..552570e0e85c 100644 --- a/tests/all/traps.rs +++ b/tests/all/traps.rs @@ -736,7 +736,7 @@ fn parse_dwarf_info() -> Result<()> { let module = Module::new(&engine, &wasm)?; let mut linker = Linker::new(&engine); wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |t| t)?; - let mut store = Store::new(&engine, wasmtime_wasi::WasiCtxBuilder::new().build_p1()); + let mut store = Store::new(&engine, wasmtime_wasi::p2::WasiCtxBuilder::new().build_p1()); linker.module(&mut store, "", &module)?; let run = linker.get_default(&mut store, "")?; let trap = run.call(&mut store, &[], &mut []).unwrap_err();