Stream: general

Topic: Can two components export the same resource type?


view this post on Zulip Christof Petig (May 04 2024 at 17:37):

While designing a plugin interface with the component model I came across an interesting problem: Can two components theoretically export the same resource type as a result of a function?

This would be very convenient for the user as the resulting object type would be identical for importers. The runtime should remember which component to call into for methods on the object.

I was able to create these components but wasm-tools can't link them, would it even be feasible?

thread 'main' panicked at crates/wasm-compose/src/encoding.rs:616:21:
internal error: entered unreachable code: should have been handled in `TypeEncoder::component_entity_type`

Source code available at https://github.com/cpetig/wasm-plugins/blob/broken/wit/plugin.wit

As long as the resource remains part of the factory type (thus having two distinct plugin resource types) it works ("working" branch), thanks to @Bailey Hayes for the inspiration on how to get plugin2 compatible with app2 (adapter trick).

Experiment with plugin composition. Contribute to cpetig/wasm-plugins development by creating an account on GitHub.

view this post on Zulip Alex Crichton (May 06 2024 at 15:52):

It's possible, yeah, if they both import a resource and then export a function referring to that resource. If both are instantiated with the same resource then they'll have the same types.

That being said I wouldn't be overly surprised if wasm-compose has a few lurking bugs here. If you've got a reproducer it might be good to open a bug on wasm-tools

view this post on Zulip Christof Petig (May 06 2024 at 20:14):

In this case both plugins export the same resource type plugin, but the creation function is different (two different factory interfaces):

interface plugin-i {
    resource plugin {
        name: func() -> string;
    }
}

interface factory {
    use plugin-i.{plugin};
    create: func() -> plugin;
}

world plugin {
    export plugin-i;
    export factory;
}

So I guess this might or might not be an intended use case by the component model. I will file a bug report nevertheless.

view this post on Zulip Alex Crichton (May 06 2024 at 20:50):

Oh if they both export the plugin resource then that'll be unique for each instance of each plugin, there's no way for those to be the same resource

view this post on Zulip Christof Petig (May 06 2024 at 20:57):

I agree that the type would be different to the host, but to an application using them it could be the same, as ids given by the host would distinguish the objects.

But given your wording I understand that this would create two distinct types of the same name, with an identical API.

view this post on Zulip Alex Crichton (May 06 2024 at 21:03):

Yeah currently if a component were to import two instances of the plugin world, for example, then that component would see two unique resources. Those two unique resources might be the same thing behind the scenes but there's no means by which that can be determined internally within the component though

view this post on Zulip Alex Crichton (May 06 2024 at 21:04):

not semantically to the component at least


Last updated: Dec 23 2024 at 13:07 UTC