Stream: general

Topic: How to share (WIT) resources in Wasmtime across components?


view this post on Zulip Troy Edwards (Sep 19 2024 at 00:12):

I'm importing and exporting the same WIT API. How do I plug these things together in Wasmtime for host / cross-component references to resources?

Here I'm trying to coerce the resource type from one component that exports the resource into the type of the component that imports that same resource type definition, using the bindgen!() macro generated API for the host implementation of the resource's constructor:
https://github.com/toxoidengine/toxoidv2_experiments/blob/806920a9924108181ce2740e7ae4d264d0d40b5d/crates/toxoid_wasm_runtime/src/main.rs#L53

The constructor does not like the type, and I'm wondering how "automagically" this would work or where I can provide the host shim to be able to manage a reference to the resource in the second / importing component, and be able to call the methods on that resource from the other component, as I try to do here with the host bindings in the component code: https://github.com/toxoidengine/toxoidv2_experiments/blob/806920a9924108181ce2740e7ae4d264d0d40b5d/crates/toxoid_wasm_component/src/lib.rs#L10

WIT files:
https://github.com/toxoidengine/toxoidv2_experiments/blob/806920a9924108181ce2740e7ae4d264d0d40b5d/crates/toxoid_wasm_component/wit/world.wit
https://github.com/toxoidengine/toxoidv2_experiments/blob/806920a9924108181ce2740e7ae4d264d0d40b5d/crates/toxoid_api/wit/world.wit

Experimenting with a cleaner, refactored version of Toxoid Engine based on 2024 / modern standards for WASM. - toxoidengine/toxoidv2_experiments
Experimenting with a cleaner, refactored version of Toxoid Engine based on 2024 / modern standards for WASM. - toxoidengine/toxoidv2_experiments
Experimenting with a cleaner, refactored version of Toxoid Engine based on 2024 / modern standards for WASM. - toxoidengine/toxoidv2_experiments
Experimenting with a cleaner, refactored version of Toxoid Engine based on 2024 / modern standards for WASM. - toxoidengine/toxoidv2_experiments

view this post on Zulip Troy Edwards (Sep 19 2024 at 09:17):

Ended up creating a host resource proxy struct. Please let me know if this is the wrong approach / if there is a way to reduce indirection / overhead mapping these resources.

https://github.com/toxoidengine/toxoidv2_experiments/blob/7b35f19ae9617df6223823dfc8e338c7ce13b191/crates/toxoid_wasm_runtime/src/main.rs#L80

Experimenting with a cleaner, refactored version of Toxoid Engine based on 2024 / modern standards for WASM. - toxoidengine/toxoidv2_experiments

view this post on Zulip Alex Crichton (Sep 20 2024 at 18:30):

I probbaly don't have the complete and total context here to help precisely how you might want, but what I can say is that bindgen! should be able to "hook up" resource type bindings from one to another. There's also a bit of a difference in how resource types work at the component model level vs the generated bindings level in that some confusion may be coming from that too?

In any case though it looks like you've got it working for now?

view this post on Zulip Troy Edwards (Sep 21 2024 at 02:28):

@Alex Crichton It's working yes, with a proxy struct for the host resource that has a pointer to the instantiated resource from the other component, and manually defining the methods for the imports by grabbing the resource from the resource table in the (shared?) store. This looks like it involves a lot of indirection and probably isn't the correct way to link ("hook up" / "plug") the two I imagine.

struct ResourceAnyPtr(*mut ResourceAny);
unsafe impl Send for ResourceAnyPtr {}
pub struct ComponentProxy {
    id: u64,
    ptr: Arc<Mutex<ResourceAnyPtr>>
}

view this post on Zulip Alex Crichton (Sep 23 2024 at 15:12):

Ah ok, yeah you definitely shouldn't have to resort to unsafe code at the very least. Since ResourceAny implements Clone and doesn't actually represent ownership would it be possible to do that instead of having Arc/Mutex and unsafe?


Last updated: Dec 23 2024 at 12:05 UTC