Currently playing around with components, trying to understand how everything fits together. I think I understand the gist of it, but I really struggle with running a function exported from an interface.
I'm using this WIT:
package cluster:main@0.1.0;
interface main {
test: func(message: string);
}
interface runnable {
run: func();
}
world cluster {
import main;
export runnable;
}
If, instead of export runnable;
, I use export run: func();
, I can use instance.get_typed_func::<(), ()>(&mut store, "run")
on the instance of the component to get a function I can then execute.
However, how do I get this run function if the world is instead exporting this runnable
interface?
Using the same invocation, get_typed_func
returns an Err
. Instead of "run"
, I have also tried "runnable/run"
, "cluster:main/run"
, and some other variations to no avail. The documentation is not particularly helpful here, the Instance::exports
function does not appear very helpful either. The option to list everything that is being exported would be quite useful here.
You can do this with the wasm-tools cli command e.g. wasm-tools dump <wasm> | grep Export
Programmatically there is also a more recently added introspection mechanism that gives more detail. You can access that via https://docs.rs/wasmtime/latest/wasmtime/component/struct.Linker.html#method.substituted_component_type
Aha! Thank you very much!
I did try wasm-tools component wit
, but that wasn't very helpful. With wasm-tools dump
, I do find cluster:main/runnable@0.1.0#run
, but that is not immediately runnable like that, either.
With the introspection, though, I noticed that it's a separate instance, and I can get the function with
instance
.exports(&mut store)
.instance("cluster:main/runnable@0.1.0")
.unwrap()
.typed_func::<(), ()>("run")
.unwrap();
This does mean I need to know the exact version, but I suppose I can use the introspection API to match it more loosely.
I will say, though, substituted_component_type
is not something I would have thought useful from the name, nor would I have expected to look for something in Linker. It makes sense in hindsight, though.
FWIW the main branch has Component::component_type
which should be much more discoverable
Oh yeah, that is much better. Especially with that documentation - very nice
Last updated: Dec 23 2024 at 14:03 UTC