I'm trying to figure out the best way to handle dynamic WASM modules in my Rust host. Here's what I'm trying to do:
I've got a bunch of WASM modules (starting with a simple calculator that has add/subtract functions) that I need to load and call at runtime in the host. The tricky part is that for new modules, I won't know the function names at host compile time. The host will read both the module and function names from a file as strings.
All my WASM functions will use the same pattern:
I've been looking at two approaches:
The Val approach seems workable for runtime loading, though it requires more boilerplate code and needs a wrapper for type safety. WIT and the component model look great but might not be possible for my use case since they seem to require binding generation and component model setup at compile time.
Key questions:
Any insight is greatly appreciated.
There are also dynamic interfaces for component model types, e.g. wasmtime::component::Val
Yeah take a look at component::Val and the rest of the component module, there’s ways to do it all dynamically. E.g. you can get whatever functions exported by a component instance https://docs.rs/wasmtime/latest/wasmtime/component/struct.Instance.html#method.get_func
Wit is most useful to describe component interfaces and worlds ahead of time, if you’re creating those on the fly and handling them at runtime dynamically then wit might not be useful. Wit is “just” a human friendly syntax for the type system that all components have, using wit isn’t required to use components
Last updated: Nov 22 2024 at 16:03 UTC