bytecodealliance / bytecodealliance/wasmtime
Implement `add_export` on `LinkerInstance`
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 126
Description
Seems that there is no simple way to link an `Instance`'s export to another `Instance`'s import. The `Linker` seems to assume that all `import`s and `export`s are fullfilled by the `Host` and that components have been prelinked before instantiation. However it seems that being able to do this at dynamically at runtime is a very useful feature. A motivating example would be loading a view-provider with a stream returned by a network-provider based on a mime-type.
The API would work something like this:
```rust
use wasmtime::component::{Component, Linker, Store};
// 1. Instantiate the dependency first
let mut linker: Linker = Linker::new(&engine);
let dependency_instance = linker.instantiate(&mut store, &dependency_component)?;
// 2. Setup a fresh, clean linker for the plugin
let mut plugin_linker: Linker = Linker::new(&engine);
// 3. Fetch the entire interface block from the dependency
let content_export = dependency_instance
.get_export(&mut store, None, "ns:pkg/content")
.expect("Dependency must export the interface");
// 4. Feed the entire exported block into the plugin's required import slot
plugin_linker.instance("ns:pkg/content")?
.add_export(&content_export)?; // This maps all functions AND resources natively
// 5. Instantiate the plugin safely
let plugin_instance = plugin_linker.instantiate(&mut store, &plugin_component)?;
```
Contributor guide
Assessment
This issue has not been assessed yet.