rylev opened issue #8645:
Feature
Several times I've simply wanted the linker to stub out all implementations for a component that it has not yet seen.
Essentially I've wanted to do roughly the following:
let mut linker = wasmtime::component::Linker::<MyData>::new(&engine); wasmtime_wasi::add_to_linker_sync(&mut linker)?; linker.stub_imports(&component)?;
With the above configuration, any imports configured by
wasmtime_wasi
would work correctly, but any other imports would simply trap when invoked.Benefit
This would provide users a simple way to get the linker to be happy and turn instantiation errors into runtime errors. This can be useful in cases where you want to instantiate a component and call one of its exports, and you are fine with calls to certain imports to simply trap if they occur.
Implementation
The implementation should be fairly straight forward I believe through the use of
Component::env_component
to do the necessary reflection to know when to callLinkerInstance::func_new
with a closure that callspanic!("stubbed function called")
.Future possibilities
In the future, we could expose an additional API that would allow the user to supply a closure that is invoked on each import invocation so that the user can customize the behavior of the stub.
alexcrichton commented on issue #8645:
Sounds reasonable to me! I think this'd be the equivalent of
wasmtime::Linker::define_unknown_imports_as_traps
forwasmtime::component::Linker
.
alexcrichton closed issue #8645:
Feature
Several times I've simply wanted the linker to stub out all implementations for a component that it has not yet seen.
Essentially I've wanted to do roughly the following:
let mut linker = wasmtime::component::Linker::<MyData>::new(&engine); wasmtime_wasi::add_to_linker_sync(&mut linker)?; linker.stub_imports(&component)?;
With the above configuration, any imports configured by
wasmtime_wasi
would work correctly, but any other imports would simply trap when invoked.Benefit
This would provide users a simple way to get the linker to be happy and turn instantiation errors into runtime errors. This can be useful in cases where you want to instantiate a component and call one of its exports, and you are fine with calls to certain imports to simply trap if they occur.
Implementation
The implementation should be fairly straight forward I believe through the use of
Component::env_component
to do the necessary reflection to know when to callLinkerInstance::func_new
with a closure that callspanic!("stubbed function called")
.Future possibilities
In the future, we could expose an additional API that would allow the user to supply a closure that is invoked on each import invocation so that the user can customize the behavior of the stub.
Last updated: Dec 23 2024 at 12:05 UTC