Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/examples/linking.rs
1685 views
1
//! Example of instantiating two modules which link to each other.
2
3
// You can execute this example with `cargo run --example linking`
4
5
use wasmtime::*;
6
use wasmtime_wasi::WasiCtx;
7
8
fn main() -> Result<()> {
9
let engine = Engine::default();
10
11
// First set up our linker which is going to be linking modules together. We
12
// want our linker to have wasi available, so we set that up here as well.
13
let mut linker = Linker::new(&engine);
14
wasmtime_wasi::p1::add_to_linker_sync(&mut linker, |s| s)?;
15
16
// Load and compile our two modules
17
let linking1 = Module::from_file(&engine, "examples/linking1.wat")?;
18
let linking2 = Module::from_file(&engine, "examples/linking2.wat")?;
19
20
// Configure WASI and insert it into a `Store`
21
let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build_p1();
22
let mut store = Store::new(&engine, wasi);
23
24
// Instantiate our first module which only uses WASI, then register that
25
// instance with the linker since the next linking will use it.
26
let linking2 = linker.instantiate(&mut store, &linking2)?;
27
linker.instance(&mut store, "linking2", linking2)?;
28
29
// And with that we can perform the final link and the execute the module.
30
let linking1 = linker.instantiate(&mut store, &linking1)?;
31
let run = linking1.get_typed_func::<(), ()>(&mut store, "run")?;
32
run.call(&mut store, ())?;
33
Ok(())
34
}
35
36