Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

wasmtime-wasi: Split a new IoView trait off of WasiView #10016

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/wasi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! component::{Linker, ResourceTable},
//! Config, Engine, Result, Store,
//! };
//! use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
//!
//! #[tokio::main]
Expand Down Expand Up @@ -53,8 +53,10 @@
//! wasi_config_vars: WasiConfigVariables,
//! }
//!
//! impl WasiView for Ctx {
//! impl IoView for Ctx {
//! fn table(&mut self) -> &mut ResourceTable { &mut self.table }
//! }
//! impl WasiView for Ctx {
//! fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi_ctx }
//! }
//! ```
Expand Down
9 changes: 6 additions & 3 deletions crates/wasi-config/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use wasmtime::{
component::{Component, Linker, ResourceTable},
Store,
};
use wasmtime_wasi::{add_to_linker_async, bindings::Command, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::{
add_to_linker_async, bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView,
};
use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};

struct Ctx {
Expand All @@ -13,11 +15,12 @@ struct Ctx {
wasi_config_vars: WasiConfigVariables,
}

impl WasiView for Ctx {
impl IoView for Ctx {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}

}
impl WasiView for Ctx {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.wasi_ctx
}
Expand Down
1 change: 1 addition & 0 deletions crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bytes::Bytes;
use http_body_util::{BodyExt, Empty};
use hyper::Method;
use wasmtime::component::Resource;
use wasmtime_wasi::IoView;

impl<T> outgoing_handler::Host for WasiHttpImpl<T>
where
Expand Down
59 changes: 33 additions & 26 deletions crates/wasi-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
//! use tokio::net::TcpListener;
//! use wasmtime::component::{Component, Linker, ResourceTable};
//! use wasmtime::{Config, Engine, Result, Store};
//! use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
//! use wasmtime_wasi_http::bindings::ProxyPre;
//! use wasmtime_wasi_http::bindings::http::types::Scheme;
//! use wasmtime_wasi_http::body::HyperOutgoingBody;
Expand Down Expand Up @@ -193,23 +193,21 @@
//! http: WasiHttpCtx,
//! table: ResourceTable,
//! }
//!
//! impl IoView for MyClientState {
//! fn table(&mut self) -> &mut ResourceTable {
//! &mut self.table
//! }
//! }
//! impl WasiView for MyClientState {
//! fn ctx(&mut self) -> &mut WasiCtx {
//! &mut self.wasi
//! }
//! fn table(&mut self) -> &mut ResourceTable {
//! &mut self.table
//! }
//! }
//!
//! impl WasiHttpView for MyClientState {
//! fn ctx(&mut self) -> &mut WasiHttpCtx {
//! &mut self.http
//! }
//! fn table(&mut self) -> &mut ResourceTable {
//! &mut self.table
//! }
//! }
//! ```

Expand All @@ -236,7 +234,7 @@ pub use crate::types::{
WasiHttpCtx, WasiHttpImpl, WasiHttpView, DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,
DEFAULT_OUTGOING_BODY_CHUNK_SIZE,
};

use wasmtime_wasi::IoImpl;
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
///
/// This function will add the `async` variant of all interfaces into the
Expand All @@ -251,7 +249,7 @@ pub use crate::types::{
/// ```
/// use wasmtime::{Engine, Result, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi::{WasiCtx, WasiView};
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// fn main() -> Result<()> {
Expand All @@ -272,25 +270,27 @@ pub use crate::types::{
/// table: ResourceTable,
/// }
///
/// impl IoView for MyState {
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiHttpView for MyState {
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiView for MyState {
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// ```
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView,
{
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(t));
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::io::poll::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::io::streams::add_to_linker_get_host(l, closure)?;
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)?;
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)?;
Expand All @@ -313,6 +313,12 @@ where
{
val
}
fn type_annotate_io<T, F>(val: F) -> F
where
F: Fn(&mut T) -> wasmtime_wasi::IoImpl<&mut T>,
{
val
}

/// A slimmed down version of [`add_to_linker_async`] which only adds
/// `wasi:http` interfaces to the linker.
Expand All @@ -325,7 +331,7 @@ pub fn add_only_http_to_linker_async<T>(
where
T: WasiHttpView,
{
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(t));
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));
crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;

Expand All @@ -343,7 +349,7 @@ where
/// ```
/// use wasmtime::{Engine, Result, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi::{WasiCtx, WasiView};
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// fn main() -> Result<()> {
Expand All @@ -362,27 +368,28 @@ where
/// http_ctx: WasiHttpCtx,
/// table: ResourceTable,
/// }
///
/// impl IoView for MyState {
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiHttpView for MyState {
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiView for MyState {
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// ```
pub fn add_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView,
{
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(t));
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));

wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::sync::io::poll::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::sync::io::streams::add_to_linker_get_host(l, closure)?;
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, closure)?;
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::bindings::io::error::add_to_linker_get_host(l, io_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)?;
Expand All @@ -402,7 +409,7 @@ pub fn add_only_http_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -
where
T: WasiHttpView,
{
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(t));
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));

crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;
Expand Down
45 changes: 18 additions & 27 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::time::Duration;
use tokio::net::TcpStream;
use tokio::time::timeout;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, Subscribe};
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, IoImpl, IoView, Subscribe};

/// Capture the state necessary for use in the wasi-http API implementation.
#[derive(Debug)]
Expand All @@ -39,7 +39,7 @@ impl WasiHttpCtx {
///
/// ```
/// use wasmtime::component::ResourceTable;
/// use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// struct MyState {
Expand All @@ -48,14 +48,15 @@ impl WasiHttpCtx {
/// table: ResourceTable,
/// }
///
/// impl IoView for MyState {
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiHttpView for MyState {
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
///
/// impl WasiView for MyState {
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
///
/// impl MyState {
Expand All @@ -73,13 +74,10 @@ impl WasiHttpCtx {
/// }
/// }
/// ```
pub trait WasiHttpView: Send {
pub trait WasiHttpView: IoView {
/// Returns a mutable reference to the WASI HTTP context.
fn ctx(&mut self) -> &mut WasiHttpCtx;

/// Returns a mutable reference to the WASI HTTP resource table.
fn table(&mut self) -> &mut ResourceTable;

/// Create a new incoming request resource.
fn new_incoming_request<B>(
&mut self,
Expand Down Expand Up @@ -150,10 +148,6 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for &mut T {
T::ctx(self)
}

fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}

fn new_response_outparam(
&mut self,
result: tokio::sync::oneshot::Sender<
Expand Down Expand Up @@ -189,10 +183,6 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for Box<T> {
T::ctx(self)
}

fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}

fn new_response_outparam(
&mut self,
result: tokio::sync::oneshot::Sender<
Expand Down Expand Up @@ -236,15 +226,16 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for Box<T> {
/// [`add_to_linker_sync`](crate::add_to_linker_sync)
/// and doesn't need to be manually configured.
#[repr(transparent)]
pub struct WasiHttpImpl<T>(pub T);
pub struct WasiHttpImpl<T>(pub IoImpl<T>);

impl<T: IoView> IoView for WasiHttpImpl<T> {
fn table(&mut self) -> &mut ResourceTable {
T::table(&mut self.0 .0)
}
}
impl<T: WasiHttpView> WasiHttpView for WasiHttpImpl<T> {
fn ctx(&mut self) -> &mut WasiHttpCtx {
self.0.ctx()
}

fn table(&mut self) -> &mut ResourceTable {
self.0.table()
self.0 .0.ctx()
}

fn new_response_outparam(
Expand All @@ -253,27 +244,27 @@ impl<T: WasiHttpView> WasiHttpView for WasiHttpImpl<T> {
Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>,
>,
) -> wasmtime::Result<Resource<HostResponseOutparam>> {
self.0.new_response_outparam(result)
self.0 .0.new_response_outparam(result)
}

fn send_request(
&mut self,
request: hyper::Request<HyperOutgoingBody>,
config: OutgoingRequestConfig,
) -> crate::HttpResult<HostFutureIncomingResponse> {
self.0.send_request(request, config)
self.0 .0.send_request(request, config)
}

fn is_forbidden_header(&mut self, name: &HeaderName) -> bool {
self.0.is_forbidden_header(name)
self.0 .0.is_forbidden_header(name)
}

fn outgoing_body_buffer_chunks(&mut self) -> usize {
self.0.outgoing_body_buffer_chunks()
self.0 .0.outgoing_body_buffer_chunks()
}

fn outgoing_body_chunk_size(&mut self) -> usize {
self.0.outgoing_body_chunk_size()
self.0 .0.outgoing_body_chunk_size()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::str::FromStr;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::{
bindings::io::streams::{InputStream, OutputStream},
Pollable, ResourceTableError,
IoView, Pollable, ResourceTableError,
};

impl<T> crate::bindings::http::types::Host for WasiHttpImpl<T>
Expand Down
10 changes: 4 additions & 6 deletions crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use wasmtime::{
component::{Component, Linker, ResourceTable},
Config, Engine, Store,
};
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, IoView, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{
bindings::http::types::{ErrorCode, Scheme},
body::HyperOutgoingBody,
Expand All @@ -38,10 +38,12 @@ struct Ctx {
rejected_authority: Option<String>,
}

impl WasiView for Ctx {
impl IoView for Ctx {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
}
impl WasiView for Ctx {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.wasi
}
Expand All @@ -52,10 +54,6 @@ impl WasiHttpView for Ctx {
&mut self.http
}

fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}

fn send_request(
&mut self,
request: hyper::Request<HyperOutgoingBody>,
Expand Down
Loading
Loading