alexcrichton opened PR #11365 from alexcrichton:wasi-refactor to bytecodealliance:main:
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_linkerwithT: WasiView- Internally
add_to_linkeris called which createsWasiImpl<&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 Twas requiredWhile this all worked it's a bit complicated for a few reasons:
- Dealing with generically named structures like
WasiImpl(orIoImplorWasiHttpImpl) is a bit baroque and not always obvious as to what's going on.- The extra layer of generics in
impl<T> Host for WasiImpl<T>adds a layer of conceptual indirection which is non-obvious.- Other WASI proposal implementations do not use this strategy and instead use "view" types or
impl Host for TheTypefor example.- Internal incantations of
add_to_linkerhad to deal with mixtures ofIoImplandWasiImpland aligning everything just right.- 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:clocksimplementations 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 ofwasmtime-wasiimplementations.Concretely the changes made here are:
WasiViewno longer inherits fromIoView, they're unrelated traits now.WasiViewnow returnsWasiViewCtx<'a>which hasctx: &'a mut WasiCtxandtable: &'a mut ResourceTable. That means it basically does the same thing before but in a slightly different fashion.- Implementations of
Hosttraits are now directly forWasiCtxView<'_>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
IoViewdirectly and instead fold that functionality intoWasiView.WasiHttpViewno longer inherits fromIoViewand instead has a directfn tablemethod. AdditionallyWasiHttpImplno longer embedsIoImplinside of it.- Host traits for
wasi:ioare now implemented directly forResourceTableinstead ofIoImpl<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
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
alexcrichton requested wasmtime-wasi-reviewers for a review on PR #11365.
alexcrichton requested wasmtime-core-reviewers for a review on PR #11365.
alexcrichton requested fitzgen for a review on PR #11365.
alexcrichton commented on PR #11365:
I can sort of preemptively apologize to embedders about this. I feel like every few versions of Wasmtime we rejigger how WASI traits work and how the impls are set up. I feel like we continue to march towards something that fits for all use cases and feels right but this is inevitably an iterative progress where we can't leapfrog to the end.
If anyone runs across this and has difficulty updating I'm happy to help out in updates, feel free to ping me on Zulip or here or an issue.
pchickey submitted PR review.
pchickey commented on PR #11365:
There are still some more places to thread through this change (CI found wasi-keyvalue) but the design change looks good. Glad this is possible now that we got rid of get_host for HasData.
alexcrichton updated PR #11365.
alexcrichton updated PR #11365.
alexcrichton has enabled auto merge for PR #11365.
alexcrichton updated PR #11365.
alexcrichton merged PR #11365.
Last updated: Dec 06 2025 at 07:03 UTC