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 manyfunc_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 necessarywit-parser
changes. We're still iterating on that and will PR those changes separately. We also have a fork ofwit-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.
- [ ] This has been discussed in issue #..., or if not, please tell us why
here.- [ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.- [ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
dicej updated PR #5925 from wit-templates
to main
.
dicej closed without merge PR #5925.
Last updated: Nov 22 2024 at 17:03 UTC