Stream: wasmtime

Topic: Using a component's resources


view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 20:43):

Hi,

view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 21:29):

(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!

view this post on Zulip Alex Crichton (Mar 01 2024 at 21:32):

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.

view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 21:55):

(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?

view this post on Zulip Alex Crichton (Mar 01 2024 at 21:57):

Yeah that was recently added to main and is what's happening

view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 22:18):

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?

view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 22:22):

But even if there's a better way, that does seem to work - I've got a match. Thanks again!

view this post on Zulip Alex Crichton (Mar 01 2024 at 22:29):

Yes Instance::exports will return an iterator

view this post on Zulip Alex Crichton (Mar 01 2024 at 22:30):

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

view this post on Zulip Chris Emerson/jugglerchris (Mar 01 2024 at 22:35):

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

view this post on Zulip Alex Crichton (Mar 01 2024 at 22:41):

oh that's my bad, I lied, I forgot this is only implemented for modules

view this post on Zulip Alex Crichton (Mar 01 2024 at 22:42):

definitely something we should add though!

view this post on Zulip Ralph (Mar 04 2024 at 15:52):

yaaassssss

view this post on Zulip Ralph (Mar 04 2024 at 15:52):

in the fullness of time, as they say


Last updated: Nov 22 2024 at 16:03 UTC