1e245e6ddSAlex Crichton //! Example of instantiating two modules which link to each other.
2e245e6ddSAlex Crichton
3e245e6ddSAlex Crichton // You can execute this example with `cargo run --example linking`
4e245e6ddSAlex Crichton
5e245e6ddSAlex Crichton use wasmtime::*;
6f7a5aa34SAlex Crichton use wasmtime_wasi::WasiCtx;
7e245e6ddSAlex Crichton
main() -> Result<()>8e245e6ddSAlex Crichton fn main() -> Result<()> {
915c68f2cSYury Delendik let engine = Engine::default();
10e245e6ddSAlex Crichton
110d4bde4aSAlex Crichton // First set up our linker which is going to be linking modules together. We
120d4bde4aSAlex Crichton // want our linker to have wasi available, so we set that up here as well.
137a1b7cdfSAlex Crichton let mut linker = Linker::new(&engine);
14*4ac219fdSAlex Crichton wasmtime_wasi::p1::add_to_linker_sync(&mut linker, |s| s)?;
150d4bde4aSAlex Crichton
16e245e6ddSAlex Crichton // Load and compile our two modules
1715c68f2cSYury Delendik let linking1 = Module::from_file(&engine, "examples/linking1.wat")?;
1815c68f2cSYury Delendik let linking2 = Module::from_file(&engine, "examples/linking2.wat")?;
19e245e6ddSAlex Crichton
207a1b7cdfSAlex Crichton // Configure WASI and insert it into a `Store`
21f7a5aa34SAlex Crichton let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build_p1();
227a1b7cdfSAlex Crichton let mut store = Store::new(&engine, wasi);
237a1b7cdfSAlex Crichton
240d4bde4aSAlex Crichton // Instantiate our first module which only uses WASI, then register that
250d4bde4aSAlex Crichton // instance with the linker since the next linking will use it.
267a1b7cdfSAlex Crichton let linking2 = linker.instantiate(&mut store, &linking2)?;
277a1b7cdfSAlex Crichton linker.instance(&mut store, "linking2", linking2)?;
28e245e6ddSAlex Crichton
290d4bde4aSAlex Crichton // And with that we can perform the final link and the execute the module.
307a1b7cdfSAlex Crichton let linking1 = linker.instantiate(&mut store, &linking1)?;
31b0939f66SAlex Crichton let run = linking1.get_typed_func::<(), ()>(&mut store, "run")?;
327a1b7cdfSAlex Crichton run.call(&mut store, ())?;
33e245e6ddSAlex Crichton Ok(())
34e245e6ddSAlex Crichton }
35