Skip to content

Commit 0a074af

Browse files
authored
Simplify WASI internal implementations (#11365)
* Simplify WASI internal implementations This commit migrates the WASIp2 implementation to be closer to the upcoming WASIp3 implementation in terms of how things are implemented internally. Previously the way things worked with WASIp2 is: * Embedders call `add_to_linker` with `T: WasiView` * Internally `add_to_linker` is called which creates `WasiImpl<&mut T>` * All internal implementations were `impl<T> Host for WasiImpl<T> where T: WasiView` * A forwarding impl of `impl<T: WasiView> WasiView for &mut T` was required While this all worked it's a bit complicated for a few reasons: 1. Dealing with generically named structures like `WasiImpl` (or `IoImpl` or `WasiHttpImpl`) is a bit baroque and not always obvious as to what's going on. 2. The extra layer of generics in `impl<T> Host for WasiImpl<T>` adds a layer of conceptual indirection which is non-obvious. 3. Other WASI proposal implementations do not use this strategy and instead use "view" types or `impl Host for TheType` for example. 4. Internal incantations of `add_to_linker` had to deal with mixtures of `IoImpl` and `WasiImpl` and aligning everything just right. 5. An extra layer of generics on all impls meant that everything was generic meaning that `wasmtime-wasi`-the-crate didn't generate much code, causing longer codegen times for consumers. The goal of this commit is to migrate towards the style of what WASIp3 is prototyping for how impls are modeled. This is done to increase the amount of code that can be shared between WASIp2 and WASIp3. This has a number of benefits such as being easier to understand and also being more modular where `wasi:clocks` implementations of traits don't require filesystem context to be present (as is the case today). This in theory helps a more mix-and-match paradigm of blending together various bits and pieces of `wasmtime-wasi` implementations. Concretely the changes made here are: * `WasiView` no longer inherits from `IoView`, they're unrelated traits now. * `WasiView` now returns `WasiViewCtx<'a>` which has `ctx: &'a mut WasiCtx` and `table: &'a mut ResourceTable`. That means it basically does the same thing before but in a slightly different fashion. * Implementations of `Host` traits are now directly for `WasiCtxView<'_>` and don't involve any generics at all. These are hopefully easier to understand and also better from a codegen/compile-time perspective. * Embedders no longer need to implement `IoView` directly and instead fold that functionality into `WasiView`. * `WasiHttpView` no longer inherits from `IoView` and instead has a direct `fn table` method. Additionally `WasiHttpImpl` no longer embeds `IoImpl` inside of it. * Host traits for `wasi:io` are now implemented directly for `ResourceTable` instead of `IoImpl<T>`. The immediate goal of this refactoring is to enable more sharing along the lines of #11362. This was not possible prior because WASIp3 requires a simultaneous borrow on the table/ctx while the trait hierarchy previously gave you one-or-the-other. With this new organization it will be possible to get both at the same time meaning more structure/contexts/etc can be shared between implementations. prtest:full * CI fixes * More CI fixes * More CI fixes
1 parent 7ae053f commit 0a074af

47 files changed

Lines changed: 598 additions & 889 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/c-api/src/store.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,13 @@ pub struct WasmtimeStoreData {
103103
pub(crate) wasip2: Option<wasmtime_wasi::p2::WasiCtx>,
104104
}
105105

106-
#[cfg(all(feature = "component-model", feature = "wasi"))]
107-
impl wasmtime_wasi::p2::IoView for WasmtimeStoreData {
108-
fn table(&mut self) -> &mut wasmtime_wasi::ResourceTable {
109-
&mut self.resource_table
110-
}
111-
}
112-
113106
#[cfg(all(feature = "component-model", feature = "wasi"))]
114107
impl wasmtime_wasi::p2::WasiView for WasmtimeStoreData {
115-
fn ctx(&mut self) -> &mut wasmtime_wasi::p2::WasiCtx {
116-
self.wasip2.as_mut().unwrap()
108+
fn ctx(&mut self) -> wasmtime_wasi::p2::WasiCtxView<'_> {
109+
wasmtime_wasi::p2::WasiCtxView {
110+
ctx: self.wasip2.as_mut().unwrap(),
111+
table: &mut self.resource_table,
112+
}
117113
}
118114
}
119115

crates/misc/component-async-tests/src/borrowing_host.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anyhow::Result;
22
use wasmtime::component::Resource;
3-
use wasmtime_wasi::p2::IoView;
43

54
use super::Ctx;
65

@@ -20,16 +19,16 @@ pub struct MyX;
2019

2120
impl bindings::local::local::borrowing_types::HostX for &mut Ctx {
2221
fn new(&mut self) -> Result<Resource<MyX>> {
23-
Ok(IoView::table(self).push(MyX)?)
22+
Ok(self.table.push(MyX)?)
2423
}
2524

2625
fn foo(&mut self, x: Resource<MyX>) -> Result<()> {
27-
_ = IoView::table(self).get(&x)?;
26+
_ = self.table.get(&x)?;
2827
Ok(())
2928
}
3029

3130
fn drop(&mut self, x: Resource<MyX>) -> Result<()> {
32-
IoView::table(self).delete(x)?;
31+
self.table.delete(x)?;
3332
Ok(())
3433
}
3534
}

crates/misc/component-async-tests/src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
44
use std::task::Waker;
55

66
use wasmtime::component::{HasData, ResourceTable};
7-
use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView};
7+
use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiView};
88

99
pub mod borrowing_host;
1010
pub mod closed_streams;
@@ -25,15 +25,12 @@ pub struct Ctx {
2525
pub continue_: bool,
2626
}
2727

28-
impl IoView for Ctx {
29-
fn table(&mut self) -> &mut ResourceTable {
30-
&mut self.table
31-
}
32-
}
33-
3428
impl WasiView for Ctx {
35-
fn ctx(&mut self) -> &mut WasiCtx {
36-
&mut self.wasi
29+
fn ctx(&mut self) -> WasiCtxView {
30+
WasiCtxView {
31+
ctx: &mut self.wasi,
32+
table: &mut self.table,
33+
}
3734
}
3835
}
3936

crates/misc/component-async-tests/src/resource_stream.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::Result;
22
use wasmtime::component::{
33
Accessor, AccessorTask, GuardedStreamWriter, Resource, StreamReader, StreamWriter,
44
};
5-
use wasmtime_wasi::p2::IoView;
65

76
use super::Ctx;
87

@@ -24,12 +23,12 @@ pub struct ResourceStreamX;
2423

2524
impl bindings::local::local::resource_stream::HostX for Ctx {
2625
fn foo(&mut self, x: Resource<ResourceStreamX>) -> Result<()> {
27-
self.table().get(&x)?;
26+
self.table.get(&x)?;
2827
Ok(())
2928
}
3029

3130
fn drop(&mut self, x: Resource<ResourceStreamX>) -> Result<()> {
32-
IoView::table(self).delete(x)?;
31+
self.table.delete(x)?;
3332
Ok(())
3433
}
3534
}
@@ -49,8 +48,7 @@ impl bindings::local::local::resource_stream::HostWithStore for Ctx {
4948
async fn run(self, accessor: &Accessor<T, Ctx>) -> Result<()> {
5049
let mut tx = GuardedStreamWriter::new(accessor, self.tx);
5150
for _ in 0..self.count {
52-
let item =
53-
accessor.with(|mut view| view.get().table().push(ResourceStreamX))?;
51+
let item = accessor.with(|mut view| view.get().table.push(ResourceStreamX))?;
5452
tx.write_all(Some(item)).await;
5553
}
5654
Ok(())

crates/wasi-config/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! component::{Linker, ResourceTable},
1818
//! Config, Engine, Result, Store,
1919
//! };
20-
//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
20+
//! use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiCtxBuilder, WasiView};
2121
//! use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
2222
//!
2323
//! #[tokio::main]
@@ -53,11 +53,10 @@
5353
//! wasi_config_vars: WasiConfigVariables,
5454
//! }
5555
//!
56-
//! impl IoView for Ctx {
57-
//! fn table(&mut self) -> &mut ResourceTable { &mut self.table }
58-
//! }
5956
//! impl WasiView for Ctx {
60-
//! fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi_ctx }
57+
//! fn ctx(&mut self) -> WasiCtxView<'_> {
58+
//! WasiCtxView { ctx: &mut self.wasi_ctx, table: &mut self.table }
59+
//! }
6160
//! }
6261
//! ```
6362
//!

crates/wasi-config/tests/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use wasmtime::{
55
component::{Component, Linker, ResourceTable},
66
};
77
use wasmtime_wasi::p2::{
8-
IoView, WasiCtx, WasiCtxBuilder, WasiView, add_to_linker_async, bindings::Command,
8+
WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView, add_to_linker_async, bindings::Command,
99
};
1010
use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
1111

@@ -15,14 +15,12 @@ struct Ctx {
1515
wasi_config_vars: WasiConfigVariables,
1616
}
1717

18-
impl IoView for Ctx {
19-
fn table(&mut self) -> &mut ResourceTable {
20-
&mut self.table
21-
}
22-
}
2318
impl WasiView for Ctx {
24-
fn ctx(&mut self) -> &mut WasiCtx {
25-
&mut self.wasi_ctx
19+
fn ctx(&mut self) -> WasiCtxView<'_> {
20+
WasiCtxView {
21+
ctx: &mut self.wasi_ctx,
22+
table: &mut self.table,
23+
}
2624
}
2725
}
2826

crates/wasi-http/src/http_impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use bytes::Bytes;
1414
use http_body_util::{BodyExt, Empty};
1515
use hyper::Method;
1616
use wasmtime::component::Resource;
17-
use wasmtime_wasi::p2::IoView;
1817

1918
impl<T> outgoing_handler::Host for WasiHttpImpl<T>
2019
where

crates/wasi-http/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
//! use tokio::net::TcpListener;
7272
//! use wasmtime::component::{Component, Linker, ResourceTable};
7373
//! use wasmtime::{Config, Engine, Result, Store};
74-
//! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
74+
//! use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiCtxBuilder, WasiView};
7575
//! use wasmtime_wasi_http::bindings::ProxyPre;
7676
//! use wasmtime_wasi_http::bindings::http::types::Scheme;
7777
//! use wasmtime_wasi_http::body::HyperOutgoingBody;
@@ -197,21 +197,21 @@
197197
//! http: WasiHttpCtx,
198198
//! table: ResourceTable,
199199
//! }
200-
//! impl IoView for MyClientState {
201-
//! fn table(&mut self) -> &mut ResourceTable {
202-
//! &mut self.table
203-
//! }
204-
//! }
200+
//!
205201
//! impl WasiView for MyClientState {
206-
//! fn ctx(&mut self) -> &mut WasiCtx {
207-
//! &mut self.wasi
202+
//! fn ctx(&mut self) -> WasiCtxView<'_> {
203+
//! WasiCtxView { ctx: &mut self.wasi, table: &mut self.table }
208204
//! }
209205
//! }
210206
//!
211207
//! impl WasiHttpView for MyClientState {
212208
//! fn ctx(&mut self) -> &mut WasiHttpCtx {
213209
//! &mut self.http
214210
//! }
211+
//!
212+
//! fn table(&mut self) -> &mut ResourceTable {
213+
//! &mut self.table
214+
//! }
215215
//! }
216216
//! ```
217217
@@ -238,7 +238,6 @@ pub use crate::types::{
238238
WasiHttpImpl, WasiHttpView,
239239
};
240240
use wasmtime::component::{HasData, Linker};
241-
use wasmtime_wasi::p2::IoImpl;
242241

243242
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
244243
///
@@ -254,7 +253,7 @@ use wasmtime_wasi::p2::IoImpl;
254253
/// ```
255254
/// use wasmtime::{Engine, Result, Config};
256255
/// use wasmtime::component::{ResourceTable, Linker};
257-
/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView};
256+
/// use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiView};
258257
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
259258
///
260259
/// fn main() -> Result<()> {
@@ -275,14 +274,15 @@ use wasmtime_wasi::p2::IoImpl;
275274
/// table: ResourceTable,
276275
/// }
277276
///
278-
/// impl IoView for MyState {
279-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
280-
/// }
281277
/// impl WasiHttpView for MyState {
282278
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
279+
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
283280
/// }
281+
///
284282
/// impl WasiView for MyState {
285-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
283+
/// fn ctx(&mut self) -> WasiCtxView<'_> {
284+
/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
285+
/// }
286286
/// }
287287
/// ```
288288
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
@@ -306,10 +306,10 @@ where
306306
{
307307
let options = crate::bindings::LinkOptions::default(); // FIXME: Thread through to the CLI options.
308308
crate::bindings::http::outgoing_handler::add_to_linker::<_, WasiHttp<T>>(l, |x| {
309-
WasiHttpImpl(IoImpl(x))
309+
WasiHttpImpl(x)
310310
})?;
311311
crate::bindings::http::types::add_to_linker::<_, WasiHttp<T>>(l, &options.into(), |x| {
312-
WasiHttpImpl(IoImpl(x))
312+
WasiHttpImpl(x)
313313
})?;
314314

315315
Ok(())
@@ -332,7 +332,7 @@ impl<T: 'static> HasData for WasiHttp<T> {
332332
/// ```
333333
/// use wasmtime::{Engine, Result, Config};
334334
/// use wasmtime::component::{ResourceTable, Linker};
335-
/// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView};
335+
/// use wasmtime_wasi::p2::{WasiCtx, WasiCtxView, WasiView};
336336
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
337337
///
338338
/// fn main() -> Result<()> {
@@ -351,14 +351,14 @@ impl<T: 'static> HasData for WasiHttp<T> {
351351
/// http_ctx: WasiHttpCtx,
352352
/// table: ResourceTable,
353353
/// }
354-
/// impl IoView for MyState {
355-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
356-
/// }
357354
/// impl WasiHttpView for MyState {
358355
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
356+
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
359357
/// }
360358
/// impl WasiView for MyState {
361-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
359+
/// fn ctx(&mut self) -> WasiCtxView<'_> {
360+
/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
361+
/// }
362362
/// }
363363
/// ```
364364
pub fn add_to_linker_sync<T>(l: &mut Linker<T>) -> anyhow::Result<()>
@@ -380,10 +380,10 @@ where
380380
{
381381
let options = crate::bindings::LinkOptions::default(); // FIXME: Thread through to the CLI options.
382382
crate::bindings::sync::http::outgoing_handler::add_to_linker::<_, WasiHttp<T>>(l, |x| {
383-
WasiHttpImpl(IoImpl(x))
383+
WasiHttpImpl(x)
384384
})?;
385385
crate::bindings::sync::http::types::add_to_linker::<_, WasiHttp<T>>(l, &options.into(), |x| {
386-
WasiHttpImpl(IoImpl(x))
386+
WasiHttpImpl(x)
387387
})?;
388388

389389
Ok(())

0 commit comments

Comments
 (0)