Stream: general

Topic: ✔ Needed imports when using wasmtime crate


view this post on Zulip Dennis Zhang (Nov 17 2022 at 07:28):

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

view this post on Zulip bjorn3 (Nov 17 2022 at 12:12):

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

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Dennis Zhang (Nov 17 2022 at 12:58):

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

view this post on Zulip bjorn3 (Nov 17 2022 at 14:33):

Did you create the store using let mut store = Store::new(&engine, wasi); where wasi is a WasiCtx?

view this post on Zulip Dennis Zhang (Nov 17 2022 at 14:55):

bjorn3 said:

Did you create the store using let mut store = Store::new(&engine, wasi); where wasi is a WasiCtx?

Yes I did, is that the reason?

    let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().build();
    let mut store = Store::new(&engine, wasi_ctx);

view this post on Zulip bjorn3 (Nov 17 2022 at 15:01):

No, that is how it should work.

view this post on Zulip bjorn3 (Nov 17 2022 at 15:02):

Can you post the code that doesn't work?

view this post on Zulip Dennis Zhang (Nov 17 2022 at 15:07):

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

view this post on Zulip Lann Martin (Nov 17 2022 at 15:10):

You have to use linker.instantiate instead of Instance::new

view this post on Zulip Dennis Zhang (Nov 17 2022 at 15:27):

Yes I need to use the instantiate, thanks a lot!

view this post on Zulip Notification Bot (Nov 17 2022 at 15:32):

Dennis Zhang has marked this topic as resolved.


Last updated: Nov 22 2024 at 16:03 UTC