Add a wasmtime::Linker type (#1384)
* Add a `wasmtime::Linker` type This commit adds a new type to the `wasmtime` crate, a `Linker`. This linker is intended to vastly simplify calling `Instance::new` by easily performing name resolution and incrementally defining state over time. The goal here is to start down a path of making linking wasm modules in `wasmtime` a first-class and ergonomic operation. This is highly likely to evolve over time and get tweaked through releases as we iterate towards a design well-suited for `wasmtime`, but this is intended to at least be the initial foundation for such functionality. This commit additionally also adds a C API for the linker and switches the existing linking examples to using this linker in both Rust and C. One piece of future work I'd like to tackle next is to integrate WASI into the `wasmtime` crate in a more first-class manner. This [`Linker`] type provides a great location to hook into the instantiation process to easily instantiate modules with WASI imports. That's a relatively large refactoring for now though and I figured it'd be best left for a different time. Closes #727
This commit is contained in:
@@ -9,47 +9,23 @@ use wasmtime_wasi::{Wasi, WasiCtx};
|
||||
fn main() -> Result<()> {
|
||||
let store = Store::default();
|
||||
|
||||
// First set up our linker which is going to be linking modules together. We
|
||||
// want our linker to have wasi available, so we set that up here as well.
|
||||
let mut linker = Linker::new(&store);
|
||||
let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
|
||||
wasi.add_to_linker(&mut linker)?;
|
||||
|
||||
// Load and compile our two modules
|
||||
let linking1 = Module::from_file(&store, "examples/linking1.wat")?;
|
||||
let linking2 = Module::from_file(&store, "examples/linking2.wat")?;
|
||||
|
||||
// Instantiate the first, `linking2`, which uses WASI imports
|
||||
let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
|
||||
let mut imports = Vec::new();
|
||||
for import in linking2.imports() {
|
||||
if import.module() == "wasi_snapshot_preview1" {
|
||||
if let Some(export) = wasi.get_export(import.name()) {
|
||||
imports.push(Extern::from(export.clone()));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"couldn't find import for `{}::{}`",
|
||||
import.module(),
|
||||
import.name()
|
||||
);
|
||||
}
|
||||
let linking2 = Instance::new(&linking2, &imports)?;
|
||||
// Instantiate our first module which only uses WASI, then register that
|
||||
// instance with the linker since the next linking will use it.
|
||||
let linking2 = linker.instantiate(&linking2)?;
|
||||
linker.instance("linking2", &linking2)?;
|
||||
|
||||
// And using the previous instance we can create the imports for `linking1`,
|
||||
// using the previous exports.
|
||||
let mut imports = Vec::new();
|
||||
for import in linking1.imports() {
|
||||
if import.module() == "linking2" {
|
||||
if let Some(export) = linking2.get_export(import.name()) {
|
||||
imports.push(export.clone());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"couldn't find import for `{}::{}`",
|
||||
import.module(),
|
||||
import.name()
|
||||
);
|
||||
}
|
||||
let linking1 = Instance::new(&linking1, &imports)?;
|
||||
|
||||
// And once everything is instantiated we can run!
|
||||
// And with that we can perform the final link and the execute the module.
|
||||
let linking1 = linker.instantiate(&linking1)?;
|
||||
let run = linking1.get_export("run").and_then(|e| e.func()).unwrap();
|
||||
let run = run.get0::<()>()?;
|
||||
run()?;
|
||||
|
||||
Reference in New Issue
Block a user