Hi I am a newbee to wasm and wasmtime, I build a wasm file which uses println macro and several rust structs, it could be run when using wasmtime cli, but failed on load module using wasmtime crate, why is that?
Error: expected 5 imports, found 0
You probably need to add wasmtime-wasi to the Linker you use to instantiate the wasm module. See for example https://github.com/bytecodealliance/wasmtime/blob/56daa8a199ab9a79b6404c5a9cd551497594f31b/examples/linking.rs#L15
bjorn3 said:
You probably need to add wasmtime-wasi to the Linker you use to instantiate the wasm module. See for example https://github.com/bytecodealliance/wasmtime/blob/56daa8a199ab9a79b6404c5a9cd551497594f31b/examples/linking.rs#L15
Hi I already add it before but it does not work, also it seems that the API has changed? because I need to give the specific type for cx
let engine = Engine::default();
let module = Module::from_file(&engine, "libadd.wasm")?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |cx: &mut WasiCtx| cx)?;
If I remove the type of cx I simply can not compile
Did you create the store using let mut store = Store::new(&engine, wasi);
where wasi
is a WasiCtx
?
bjorn3 said:
Did you create the store using
let mut store = Store::new(&engine, wasi);
wherewasi
is aWasiCtx
?
Yes I did, is that the reason?
let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().build();
let mut store = Store::new(&engine, wasi_ctx);
No, that is how it should work.
Can you post the code that doesn't work?
use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder};
fn main() -> Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "libadd.wasm")?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |cx: &mut WasiCtx| cx)?;
let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().build();
let mut store = Store::new(&engine, wasi_ctx);
let instance = Instance::new(&mut store, &module, &[])?;
let add = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "add")?;
// And finally we can call the wasm!
println!("1 + 2 = {}", add.call(&mut store, (1, 2))?);
Ok(())
}
This is the code could not work for me
You have to use linker.instantiate
instead of Instance::new
Yes I need to use the instantiate, thanks a lot!
Dennis Zhang has marked this topic as resolved.
Last updated: Nov 22 2024 at 16:03 UTC