Hi,
(Sorry, hit enter too soon, and then got distracted by an idea)
I'm trying to call into a component from a dynamic language, so I'm trying to use a lot of introspection rather than knowing in advance what exports there are. I'm now trying to get a resource type to work, but I haven't figured out how to extract the information I need.
So far I can get a list of exports with wasmtime::component::types::Component::exports()
(and recursing into ComponentInstance::exports()
), which includes the (currently only) resource type by name:
[src/lib.rs:764:25] (&item_name, &item) = (
"zipfile",
Resource(
ResourceType {
kind: Uninstantiated {
component: 94136215617120,
index: ResourceIndex(
4,
),
},
},
),
)
If I look at the functions return type (Func::results()
), or the restype.ty()
on the Val::Resource
, the resource type looks like:
[src/lib.rs:984:13] ty = ResourceType {
kind: Guest {
store: StoreId(
1,
),
instance: 94036607155888,
id: DefinedResourceIndex(
0,
),
},
}
I've tried just comparing them (with ==
) and they don't compare equal. What am I missing to be able to tie resource types as returned from guest functions with the types in the exports? I think I need to do this to get the list of available methods.
Thanks for any help!
I think you're running into two things: (1) resources are subtle and (2) we need more introspection APIs.
For the first part, the subtle part about exported resources is that each time you instantiate a component it's generating fresh new types for each resource. This means that if you instantiate twice you'll also see that the same-named resource from each component does not compare equal to the other. This is similar to an uninstantiated component where that isn't equivalent to other instantiated components since fresh new types were made.
For (2) we should probably have introspection APIs on an Instance
itself so you can learn the exact type of the resource being created here.
(2) Sounds good!
(1) Also sounds good, as it probably means I'm unintentionally instantiating more than once and so if I just stop doing that, things will look good again.
Oh, does the Uninstantiated
in the first output mean I'm actually doing the opposite and comparing with exports before instantiating?
Yeah that was recently added to main
and is what's happening
Is there actually a way to get a list of exports on an instantiated component? I keep being confused by lots of similar/same-name types. Do I have to iterate through the uninstantiated one and then look up each name in the instantiated one?
But even if there's a better way, that does seem to work - I've got a match. Thanks again!
Yes Instance::exports
will return an iterator
and if you want to drop notes here about your confusion or file issues, that'd be much appreciated! There's quite a lot more we can do in the way of documentation
Alex Crichton said:
Yes
Instance::exports
will return an iterator
Really? I can't see an Iterator or IntoIterator there, just methods to look things up by name
oh that's my bad, I lied, I forgot this is only implemented for modules
definitely something we should add though!
yaaassssss
in the fullness of time, as they say
Last updated: Nov 22 2024 at 16:03 UTC