Stream: wasmtime

Topic: implementing a world that imports wasi + wasi-http


view this post on Zulip Marek Vavrusa (Mar 19 2025 at 20:45):

Hi! I'm trying to implement a world that imports both wasi types and wasi-http types and running into issues with satisfying wasmtime bindgen Host trait requirements. So if I use just wasi then I can implement all my interfaces for WasiImpl<MyState> and it automatically satisfies all the wasi import traits because they are implemented for WasiImpl<T>, but then I can't add wasi-http implementations since they are only implemented for WasiHttpImpl<T> newtype, and the wasmtime bindgen generated world requires the World::add_to_linker(linker, state) to have state satisfy FnOnce(T) -> U, where U implements all my world interfaces, wasi interfaces, and wasi-http interfaces in one type, which seems impossible unless I create a new newtype and implement all wasi and wasi-http implementations for it?

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 20:55):

I guess I can implement a custom add_to_linker without the Host traits as they are satisfied via newtype conversions already

view this post on Zulip Alex Crichton (Mar 19 2025 at 20:57):

Are you implementing the wasi traits by hand? Or using the impls in the wasmtime-wasi* crates? If you're only doing your own functionality by hand then you might also be interested in the *_get_host variant of the add_to_linker functions that are generated

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 20:59):

I'm using impls from wasmtime-wasi* crates

view this post on Zulip Alex Crichton (Mar 19 2025 at 21:02):

ah ok, in that case what led you to work with WasiImpl? In theory that's just an implementation detail of those crates

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 21:06):

The wasmtime-wasi* crates add_to_linker()are fine because they require T to be WasiView/WasiHttpView and then wrap the T in newtype and add bindings for that. The problem is bindgen generated add_to_linker for my world which generates a function that looks like this:

pub fn add_to_linker<T, U>(
            linker: &mut wasmtime::component::Linker<T>,
            get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
        ) -> wasmtime::Result<()>
        where
            T: Send, U: host::app::init::Host +  __with_name9::network::Host  + __with_name5::wall_clock::Host ...

But the wasi traits are implemented only for WasiImpl<U> and not U.

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 21:07):

Like ideally bindgen would generate U constraints like host::app::init::Host + WasiView

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 21:16):

I ignored the generated add_to_linker, and instead implemented add_to_linker manually for my concrete MyState which seems like the best solution

view this post on Zulip Alex Crichton (Mar 19 2025 at 21:28):

Makes sense, I'd recommend avoiding the generated add_to_linker for wasi, you probably want the top-level handwritten one instead which avoids dealing with WasiImpl

view this post on Zulip Marek Vavrusa (Mar 19 2025 at 21:33):

Yeah makes sense thanks! I was thinking about this for a while but only after writing this it occurred to me that I can just write my own add_to_linker :melting_face:

view this post on Zulip Andrew Gazelka (Mar 22 2025 at 00:58):

any recommended tutorials for using wasmtime with hot reloading? trying to figure it out for a game I am making


Last updated: Apr 09 2025 at 11:03 UTC