Stream: git-wasmtime

Topic: wasmtime / PR #5925 support worlds which import and/or ex...


view this post on Zulip Wasmtime GitHub notifications bot (Mar 03 2023 at 23:27):

dicej opened PR #5925 from wit-templates to main:

Per https://github.com/WebAssembly/component-model/issues/172, this implements "part 1" of WIT templates, allowing WIT files to define interfaces which contain a single wildcard function, which worlds may import or export.

I've chosen to implement the bindings for host-implemented functions in such a way that the host may delay import resolution until the latest possible moment, i.e. when the guest is actually calling the function. This allows for fully dynamic resolution (e.g. using the function name as a key to be looked up in a remote key-value store) when desired. This does come at a small performance cost compared to doing resolution at e.g. link time instead.

In cases where the host wants to do resolution earlier (e.g. at deploy or instantiation time), that's certainly possible, e.g.:

let component = Component::new(&engine, wasm)?;
let funcs = component
    .names("imports")
    .map(|name| Ok((name.to_owned(), my_resolver(name)?)))
    .collect::<Result<HashMap<_, _>>>()?;

struct MyImports<F> {
   funcs: HashMap<String, F>
}

impl <F: Fn() -> Result<u32>> imports::Host for MyImports<F> {
    fn call(&mut self, name: &str) -> Result<u32> {
        (self.funcs.get(name).unwrap())()
    }
}

let mut store = Store::new(&engine, MyImports { funcs });
...

If we feel that early resolution is the more common case, we could consider adding a configuration option to the binding generator which indicates whether early or late resolution is desired, allowing the generator to optimize (ergonmically and performance-wise) accordingly.

Note that the generated add_to_linker functions now take a &Component parameter as well as a &mut Linker. This allows those functions to inspect the component in order to determine how many func_wrap{_async} calls to make, and with what names. I'm open to alternatives to this if there's a better way.

Finally, I've added a temporary dependency override to Cargo.toml, pointing to our fork of wasm-tools, which includes the necessary wit-parser changes. We're still iterating on that and will PR those changes separately. We also have a fork of wit-bindgen with a new "wildcards" test to verify everything works end-to-end: https://github.com/bytecodealliance/wit-bindgen/compare/main...dicej:wit-templates. I'll PR that last once everything else is stable.

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (Mar 04 2023 at 00:00):

dicej updated PR #5925 from wit-templates to main.

view this post on Zulip Wasmtime GitHub notifications bot (Mar 05 2023 at 22:06):

dicej closed without merge PR #5925.


Last updated: Dec 23 2024 at 12:05 UTC