use anyhow::Result; use wasmtime::{Config, Engine, Instance, Linker, Module, Store}; test_helpers::runtime_tests_wasmtime!(); fn default_config() -> Result { // Create an engine with caching enabled to assist with iteration in this // project. let mut config = Config::new(); config.cache_config_load_default()?; config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); Ok(config) } fn default_wasi() -> wasmtime_wasi::WasiCtx { wasmtime_wasi::sync::WasiCtxBuilder::new() .inherit_stdio() .build() } struct Context { wasi: wasmtime_wasi::WasiCtx, imports: I, exports: E, } fn instantiate( wasm: &str, add_imports: impl FnOnce(&mut Linker>) -> Result<()>, mk_exports: impl FnOnce( &mut Store>, &Module, &mut Linker>, ) -> Result<(T, Instance)>, ) -> Result<(T, Store>)> { let engine = Engine::new(&default_config()?)?; let module = Module::from_file(&engine, wasm)?; let mut linker = Linker::new(&engine); add_imports(&mut linker)?; wasmtime_wasi::add_to_linker(&mut linker, |cx| &mut cx.wasi)?; let mut store = Store::new( &engine, Context { wasi: default_wasi(), imports: I::default(), exports: E::default(), }, ); let (exports, _instance) = mk_exports(&mut store, &module, &mut linker)?; Ok((exports, store)) }