Files
wasmtime/examples/linking.rs
Pat Hickey 0f5bdc6497 only wasi_cap_std_sync and wasi_tokio need to define WasiCtxBuilders (#2917)
* wasmtime-wasi: re-exporting this WasiCtxBuilder was shadowing the right one

wasi-common's WasiCtxBuilder is really only useful wasi_cap_std_sync and
wasi_tokio to implement their own Builder on top of.

This re-export of wasi-common's is 1. not useful and 2. shadow's the
re-export of the right one in sync::*.

* wasi-common: eliminate WasiCtxBuilder, make the builder methods on WasiCtx instead

* delete wasi-common::WasiCtxBuilder altogether

just put those methods directly on &mut WasiCtx.

As a bonus, the sync and tokio WasiCtxBuilder::build functions
are no longer fallible!

* bench fixes

* more test fixes
2021-05-21 12:59:39 -05:00

40 lines
1.3 KiB
Rust

//! Example of instantiating two modules which link to each other.
// You can execute this example with `cargo run --example linking`
use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::sync::{Wasi, WasiCtxBuilder};
fn main() -> Result<()> {
let engine = Engine::default();
let store = Store::new(&engine);
// 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,
WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()?
.build(),
);
wasi.add_to_linker(&mut linker)?;
// Load and compile our two modules
let linking1 = Module::from_file(&engine, "examples/linking1.wat")?;
let linking2 = Module::from_file(&engine, "examples/linking2.wat")?;
// 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 with that we can perform the final link and the execute the module.
let linking1 = linker.instantiate(&linking1)?;
let run = linking1.get_typed_func::<(), ()>("run")?;
run.call(())?;
Ok(())
}