Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give wasmtime::component::Linker ability to stub out implementations #8645

Closed
rylev opened this issue May 17, 2024 · 1 comment · Fixed by #8672
Closed

Give wasmtime::component::Linker ability to stub out implementations #8645

rylev opened this issue May 17, 2024 · 1 comment · Fixed by #8672

Comments

@rylev
Copy link
Contributor

rylev commented May 17, 2024

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 call LinkerInstance::func_new with a closure that calls panic!("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
Copy link
Member

Sounds reasonable to me! I think this'd be the equivalent of wasmtime::Linker::define_unknown_imports_as_traps for wasmtime::component::Linker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants