Stream: git-wasmtime

Topic: wasmtime / issue #3200 [Help] Use struct as argument


view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 08:53):

silence-coding edited issue #3200:

How to use struct as a parameter? I don't find any useful information in the example.

#[derive(Debug, Default)]
#[repr(C)]
pub struct Msg {
    id: u64,
    data: String,
}

use model::Msg;

#[no_mangle]
pub extern "C" fn print_msg(msg: Msg) {
    println!("Hello: {:?}", msg);
}

pub fn test_struct() -> Result<(), Box<dyn Error>> {
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
    let module = Module::from_file(&engine, "target/wasm32-wasi/debug/struct.wasm")?;
    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let mut store = Store::new(&engine, wasi);
    let instance = linker.instantiate(&mut store, &module)?;
    let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
    greet.call(&mut store, Msg::default())?;
    Ok(())
}

error[E0277]: the trait bound `Msg: WasmTy` is not satisfied
  --> run\src\struct2.rs:17:26
   |
17 |     let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
   |                          ^^^^^^^^^^^^^^ the trait `WasmTy` is not implemented for `Msg`
   |
   = note: required because of the requirements on the impl of `WasmParams` for `Msg`

view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 12:28):

bjorn3 commented on issue #3200:

You will need to serialize the struct into raw bytes on one side and write it to the linear memory and then pass the address of the linear memory. On the wasm side you can then load the struct.

Alternatively I think you can use wiggle: https://bytecodealliance.org/articles/implementing-wasi-nn-in-wasmtime

view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 14:47):

alexcrichton commented on issue #3200:

Thanks for the report here, but this is a scenario where the literal way the wasm module is communicated with is much more strict than using a type. If you'd like to explore typed communication between the runtime and a wasm module this is what the WebAssembly interface types proposal is for. You can demo and use that today with witx-bindgen which should eanble what you've got here with a Msg structure.

If neither of those helps though let us know and we can try to figure out a solution!

view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 14:47):

alexcrichton closed issue #3200:

How to use struct as a parameter? I don't find any useful information in the example.

#[derive(Debug, Default)]
#[repr(C)]
pub struct Msg {
    id: u64,
    data: String,
}

use model::Msg;

#[no_mangle]
pub extern "C" fn print_msg(msg: Msg) {
    println!("Hello: {:?}", msg);
}

pub fn test_struct() -> Result<(), Box<dyn Error>> {
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
    let module = Module::from_file(&engine, "target/wasm32-wasi/debug/struct.wasm")?;
    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let mut store = Store::new(&engine, wasi);
    let instance = linker.instantiate(&mut store, &module)?;
    let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
    greet.call(&mut store, Msg::default())?;
    Ok(())
}

error[E0277]: the trait bound `Msg: WasmTy` is not satisfied
  --> run\src\struct2.rs:17:26
   |
17 |     let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
   |                          ^^^^^^^^^^^^^^ the trait `WasmTy` is not implemented for `Msg`
   |
   = note: required because of the requirements on the impl of `WasmParams` for `Msg`

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2021 at 02:54):

silence-coding commented on issue #3200:

@alexcrichton Thanks

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2021 at 05:01):

silence-coding commented on issue #3200:

@alexcrichton I read the wix-bindgen. It seems that the map and set types are not supported. Is there a perfect solution? For example, I want to use the request object of hyper as an argument.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2021 at 05:06):

silence-coding commented on issue #3200:

@bjorn3 Does this mean that I understand the memory status of each complex parameter myself? Is there an example for me to learn? I just started to get into Wasmtime recently and found that there was a lack of perfect development materials. Thank you very much.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2021 at 14:34):

alexcrichton commented on issue #3200:

@silence-coding yes the interface types grammar is intentionally not as rich as having sets/maps. For that you'll want to use lists, you won't be able to take types off the shelf from libraries and pass them over a wasm boundary.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 23 2021 at 01:44):

silence-coding commented on issue #3200:

Well, I don't think wasm needs rich data types, but some common ones, I think, can be reasonably provided.


Last updated: Oct 23 2024 at 20:03 UTC