rylev opened issue #7646:
Feature
Add support through the
bindgen!
macro for adding a component instance to the linker as the backing implementation for a import.Currently
bindgen!
gives support throughadd_to_linker
for specifying an implementation for an import that dispatches to a type that conforms to a trait which matches the import's signature. This feature would add an additional functionadd_component_to_linker
which would do the same but instead of dispatching to a trait function implemented on a type, it would instead dispatch to a component instance.Benefit
This has all the benefits associated with WebAssembly itself. Import implementations can be sandboxed so that even untrusted code can be run as an import implementation. This provides an easy way for users of wasmtime to implement "plugin" systems for their import implementations.
Implementation
Most likely the
bindgen!
macro would gain the ability to generate a functionadd_component_to_linker
function on world structs much like the currentadd_to_linker
function.This function would have the following signature:
/// Add component as a backing implementation to `linker` /// /// The component will run against `store` and can have its own imports satisfied by `other_linker` fn add_component_to_linker<T, U>( linker: &mut Linker<T>, other_linker: &Linker<U>, other_store: Store<U>, other_component: &Component, ) -> anyhow::Result<()>;
This signature would allow user's complete control over the linked
other_component
including the ability to turn on wasi support or even implement imports for that component.The implementation would instantiate
component
againstother_linker
andother_store
which would be wrapped in aMutex
and moved inside the wrapped import oflinker
's root instance. Whenlinker
's wrapped import function gets called, the mutex will be locked and theother_component
instance will be called with the exact same arguments coming from the outer component.Alternatives
The signature for
add_component_to_linker
is a bit awkward (especially due to taking a second linker). This is to maximize the user's ability to control theother_component
instance. We could simplify the signature at the expense of this user control.It's also possible for users to implement this without the help of wasmtime or the
bindgen!
macro; it's just fairly finicky code. While it would certainly be helpful for wasmtime to provide this functionality, it does not unlock new use cases that weren't possible before. Therefore, an alternative would be to just not do this.
rylev edited issue #7646:
Feature
Add support through the
bindgen!
macro for adding a component instance to the linker as the backing implementation for a import.Currently
bindgen!
gives support throughadd_to_linker
for specifying an implementation for an import that dispatches to a type that conforms to a trait which matches the import's signature. This feature would add an additional functionadd_component_to_linker
which would do the same but instead of dispatching to a trait function implemented on a type, it would instead dispatch to a component instance.Benefit
This has all the benefits associated with WebAssembly itself. Import implementations can be sandboxed so that even untrusted code can be run as an import implementation. This provides an easy way for users of wasmtime to implement "plugin" systems for their import implementations.
Implementation
Most likely the
bindgen!
macro would gain the ability to generate a functionadd_component_to_linker
function on world structs much like the currentadd_to_linker
function.This function would have the following signature:
/// Add component as a backing implementation to `linker` /// /// The component will run against `store` and can have its own imports satisfied by `other_linker` fn add_component_to_linker<T, U: Send + 'static>( linker: &mut Linker<T>, other_linker: &Linker<U>, other_store: Arc<Mutex<Store<U>>>, other_component: &Component, ) -> anyhow::Result<()>;
This signature would allow user's complete control over the linked
other_component
including the ability to turn on wasi support or even implement imports for that component. Unfortunately, the implementation needsother_store
to be'static
and for it to be possible to get unique references to the underlyingStore
. If we want to allow multiple callers ofadd_component_to_linker
to use the sameother_store
, that forces our hand to takeArc<Mutex<Store<U>>>
.The implementation would instantiate
component
againstother_linker
andother_store
which would be moved inside the wrapped import oflinker
's root instance. Whenlinker
's wrapped import function gets called, the Store's mutex will be locked and theother_component
instance will be called with the exact same arguments coming from the outer component.Alternatives
The signature for
add_component_to_linker
is a bit awkward (especially due to taking a second linker). This is to maximize the user's ability to control theother_component
instance. We could simplify the signature at the expense of this user control.It's also possible for users to implement this without the help of wasmtime or the
bindgen!
macro; it's just fairly finicky code. While it would certainly be helpful for wasmtime to provide this functionality, it does not unlock new use cases that weren't possible before. Therefore, an alternative would be to just not do this.
BrytonLee commented on issue #7646:
I think this feature is needed. I am confused by the fn name
add_to_linker
and it blocks compiler as well. In my case, I want to include WASI support, thus newed a linker fromwasmtime::Linker
, suppose I can usewasmtime_wasi::add_to_linker()
to enable WASI for engine, but I also want to usewasmtime::component
tobindgen!
my WIT file,wasmtime::component
requires itself linker underwasmtime::component::Linker
, althought bothwasmtime_wasi
andwasmtime::component
use the same nameadd_to_linker
, but they accept different linker types. Compiler wouldn't compile this code.Rename
wasmtime::component::add_to_linker
to something else would definitely help, and I also sincely hope thatwasmtime::component
provides a fn to return a normalwasmtime::Linker
instance that can be interacted with other part likewasmtime_wasi
.BTW: could you please share your insights if you know how to instantiate a wasmtime engine with both
wasmtime_wasi
andwasmtime::component
support? Really appreciate.
Last updated: Dec 23 2024 at 13:07 UTC