Stream: git-wasmtime

Topic: wasmtime / issue #8645 Give `wasmtime::component::Linker`...


view this post on Zulip Wasmtime GitHub notifications bot (May 17 2024 at 10:31):

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 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.

view this post on Zulip Wasmtime GitHub notifications bot (May 17 2024 at 14:26):

alexcrichton commented on issue #8645:

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

view this post on Zulip Wasmtime GitHub notifications bot (May 21 2024 at 19:12):

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 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.


Last updated: Dec 23 2024 at 12:05 UTC