Stream: git-wasmtime

Topic: wasmtime / issue #6146 How to pass the String to TypedFunc


view this post on Zulip Wasmtime GitHub notifications bot (Apr 05 2023 at 12:37):

Reknij opened issue #6146:

I want pass my json struct to the wasm function. I read the document then i serialize the struct to string then pass it to function, but it don't work.

pub async fn resolve_message(&self, message: &str) {
            let mut store = &mut *self.store.lock().await;
            let func = i.get_typed_func::<(&str,), (String,)>(&mut store, "resolve_message");
            match func {
                Ok(f)=> {
                    let result = f.call_async(&mut store, (message)).await;
                },
                Err(err)=> {
                    error!("{}", err);
                }
            }
}

view this post on Zulip Wasmtime GitHub notifications bot (Apr 05 2023 at 16:29):

alexcrichton commented on issue #6146:

but it don't work

Could you clarify this a bit and describe how it doesn't work? Are you getting a runtime error message? A compile-time error message?

One issue could be that you're passing (message) where I believe that needs to be (message,). I don't know if that's the entirety of your issue, though.

view this post on Zulip Wasmtime GitHub notifications bot (Apr 06 2023 at 13:33):

Reknij commented on issue #6146:

but it don't work

Could you clarify this a bit and describe how it doesn't work? Are you getting a runtime error message? A compile-time error message?

One issue could be that you're passing (message) where I believe that needs to be (message,). I don't know if that's the entirety of your issue, though.

error[E0277]: the trait bound `(&str,): WasmParams` is not satisfied
   --> src/plugin_system.rs:79:43
    |
79  |             let func = i.get_typed_func::<(&str,), (String,)>(&mut store, "resolve_message");
    |                                           ^^^^^^^ the trait `WasmParams` is not implemented for `(&str,)`
    |
    = help: the following other types implement trait `WasmParams`:
              ()
              (A1, A2)
              (A1, A2, A3)
              (A1, A2, A3, A4)
              (A1, A2, A3, A4, A5)
              (A1, A2, A3, A4, A5, A6)
              (A1, A2, A3, A4, A5, A6, A7)
              (A1, A2, A3, A4, A5, A6, A7, A8)
            and 9 others
note: required by a bound in `wasmtime::Instance::get_typed_func`
   --> /home/jinker/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-7.0.0/src/instance.rs:461:17
    |
461 |         Params: crate::WasmParams,
    |                 ^^^^^^^^^^^^^^^^^ required by this bound in `Instance::get_typed_func`

It is compile-time error and message in here.

view this post on Zulip Wasmtime GitHub notifications bot (Apr 06 2023 at 14:46):

alexcrichton commented on issue #6146:

The issue here is that you're using a wasmtime::Func which is a core wasm function. The documentation you linked is for wasmtime::component::Func which is for wasm components, which are not the same as core wasm. The core wasm functions only take integer primitives and floats as arguments. Components take a richer set of arguments.

view this post on Zulip Wasmtime GitHub notifications bot (Apr 06 2023 at 15:28):

Reknij commented on issue #6146:

The issue here is that you're using a wasmtime::Func which is a core wasm function. The documentation you linked is for wasmtime::component::Func which is for wasm components, which are not the same as core wasm. The core wasm functions only take integer primitives and floats as arguments. Components take a richer set of arguments.

Nice, is working. I corrected the code according to what you said, and now the code is normal without compile-time errors.

Sorry for wasting your time on my careless mistake

For latecomers who see this issue, just change wasmtime::{Linker, Instance} to wasmtime::{component::{Linker, Instance}}. It's worth mentioning that wasmtime::component::Linker requires wasmtime::component::Component instead of wasmtime::Module

view this post on Zulip Wasmtime GitHub notifications bot (Apr 06 2023 at 15:28):

Reknij closed issue #6146:

I want pass my json struct to the wasm function. I read the document then i serialize the struct to string then pass it to function, but it don't work.

pub async fn resolve_message(&self, message: &str) {
            let mut store = &mut *self.store.lock().await;
            let func = i.get_typed_func::<(&str,), (String,)>(&mut store, "resolve_message");
            match func {
                Ok(f)=> {
                    let result = f.call_async(&mut store, (message)).await;
                },
                Err(err)=> {
                    error!("{}", err);
                }
            }
}


Last updated: Dec 23 2024 at 12:05 UTC