Stream: general

Topic: WIT resources in browser with wasm-bindgen host backing ?


view this post on Zulip Matt (Nov 14 2024 at 15:27):

I recently attempted to use winit and wgpu to create host bindings for a little experiment interface to get a window surface and draw rectangles with a specified color, border width, border color, and radius for rounding corners. The idea was for the guest to be akin to a primitive gui application, just implement an entry point and then draw whatever rectangles the app developer wants.
This is completely fine with wasmtime, but I wanted the same guest binary to work in a browser. winit and wgpu can compile to wasm32-unknown-unknown and both use wasm-bindgen, so I also wasm-bindgen an impl for the host struct, then in the trait impl for host bindings i just call those regular struct impls.
It compiles and I can instantiate and link in a js module with an importObject but none of the calls from the guest work at all, and attempting to pass for example a list into a function completely breaks. At the end of the day, I guess it's really just modules, so it's limiting.
Is there a nicer way to do something like this?

view this post on Zulip Alex Crichton (Nov 15 2024 at 23:14):

For the same exact binary to be usable in both a browser and out-of-browser contexts you'll probably want to go the WASI route since out-of-browser contexts typically don't have a JS runtime host. That means you'll need to write a bit more glue though and crates like winit and wgpu will need WASI support (which they may not have)


Last updated: Dec 23 2024 at 12:05 UTC