Stream: general

Topic: Passing an exported resource into an imported function - wit


view this post on Zulip Mendy Berger (Jul 30 2024 at 16:47):

Is it possible to use a resource exported from a world as an parameter for an imported function?
I'm trying to have the guest create a resource and pass in into a function provided by the host.

Here's the wit I currently have:

package webidl:browser;

interface custom-element-export {
    resource define-element-base {
        connected-callback: func();
    }
}

interface custom-element-import {
    use custom-element-export.{define-element-base};
    define: func(name: string, options: define-element-base);
}

world browser {
    export start: func();
    export custom-element-export;
    import custom-element-import;
}

When generating the bindings with wit-bindgen, it seems to be creating two distinct define-element-basetypes, one for the import and another one for the export.

view this post on Zulip Joel Dice (Jul 30 2024 at 16:48):

No, it's not possible: https://github.com/WebAssembly/component-model/issues/272

The Component Model does not currently allow you to refer to exported types in imports, which feels asymmetric given that one can easily refer to imported types in exports. This becomes a practical...

view this post on Zulip Joel Dice (Jul 30 2024 at 16:51):

AFAIK, the only way to emulate that kind of callback is to export functions which take e.g. u32 handles which the guest can use as indexes into a table of state objects.

view this post on Zulip Mendy Berger (Jul 30 2024 at 16:55):

Okay, so no ergonomic way to do this.
I have an alternative idea which only requires imports that I can use instead. Not gonna be as nice, but not too bad either.
Thanks @Joel Dice for the quick response!


Last updated: Dec 23 2024 at 12:05 UTC