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 anyhow::Result; 6 use wasi_common::sync::WasiCtxBuilder; 7 use wasmtime::*; 8 9 fn main() -> Result<()> { 10 let engine = Engine::default(); 11 12 // First set up our linker which is going to be linking modules together. We 13 // want our linker to have wasi available, so we set that up here as well. 14 let mut linker = Linker::new(&engine); 15 wasi_common::sync::add_to_linker(&mut linker, |s| s)?; 16 17 // Load and compile our two modules 18 let linking1 = Module::from_file(&engine, "examples/linking1.wat")?; 19 let linking2 = Module::from_file(&engine, "examples/linking2.wat")?; 20 21 // Configure WASI and insert it into a `Store` 22 let wasi = WasiCtxBuilder::new() 23 .inherit_stdio() 24 .inherit_args()? 25 .build(); 26 let mut store = Store::new(&engine, wasi); 27 28 // Instantiate our first module which only uses WASI, then register that 29 // instance with the linker since the next linking will use it. 30 let linking2 = linker.instantiate(&mut store, &linking2)?; 31 linker.instance(&mut store, "linking2", linking2)?; 32 33 // And with that we can perform the final link and the execute the module. 34 let linking1 = linker.instantiate(&mut store, &linking1)?; 35 let run = linking1.get_typed_func::<(), ()>(&mut store, "run")?; 36 run.call(&mut store, ())?; 37 Ok(()) 38 } 39