Stream: git-wasmtime

Topic: wasmtime / Issue #1696 Some errors when instantiating was...


view this post on Zulip Wasmtime GitHub notifications bot (May 14 2020 at 09:42):

leonwanghui opened Issue #1696:

Summary

Hi there, I was trying to use wasmtime to instantiate *.wasm file, and some errors occurred as below:

error[E0277]: the trait bound `[f32; 5]: wasmtime::func::WasmTy` is not satisfied
  --> src/main.rs:13:10
   |
13 |         .get2::<[f32;5], f32, [f32;5]>()?;
   |          ^^^^ the trait `wasmtime::func::WasmTy` is not implemented for `[f32; 5]`

error: aborting due to previous error

From the tutorial code (https://bytecodealliance.github.io/wasmtime/examples-rust-gcd.html), I know that i32, i64, f32 and f64 are supported to be called as parameter, but what if I want to use more complex type (such like array, slice, etc), is there some approach to achieve that?

Would be appreciated if anyone could offer some helps : )

The attached info

Here is the source code:

#[no_mangle]
pub extern "C" fn add(a: [f32;5], b: f32) -> [f32;5] {
    let mut out: [f32;5] = [0.0, 0.0, 0.0, 0.0, 0.0];
    for i in (1..5).rev() {
        out[i] = a[i] + b;
    }
    return out;
}

Here is the target code:

use anyhow::Result;
use wasmtime::*;

fn main() -> Result<()> {
    let store = Store::default();
    let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?;
    let instance = Instance::new(&module, &[])?;

    // Invoke `add` export
    let add = instance
        .get_func("add")
        .ok_or(anyhow::format_err!("failed to find `add` function export"))?
        .get2::<[f32;5], f32, [f32;5]>()?;

    let x = [1.1, 2.2, 3.3, 4.4, 5.5];
    let y = 3.0;
    println!("add({:?}, {:?}) = {:?}", x, y, add(x, y)?);
    Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (May 14 2020 at 09:42):

leonwanghui labeled Issue #1696:

Summary

Hi there, I was trying to use wasmtime to instantiate *.wasm file, and some errors occurred as below:

error[E0277]: the trait bound `[f32; 5]: wasmtime::func::WasmTy` is not satisfied
  --> src/main.rs:13:10
   |
13 |         .get2::<[f32;5], f32, [f32;5]>()?;
   |          ^^^^ the trait `wasmtime::func::WasmTy` is not implemented for `[f32; 5]`

error: aborting due to previous error

From the tutorial code (https://bytecodealliance.github.io/wasmtime/examples-rust-gcd.html), I know that i32, i64, f32 and f64 are supported to be called as parameter, but what if I want to use more complex type (such like array, slice, etc), is there some approach to achieve that?

Would be appreciated if anyone could offer some helps : )

The attached info

Here is the source code:

#[no_mangle]
pub extern "C" fn add(a: [f32;5], b: f32) -> [f32;5] {
    let mut out: [f32;5] = [0.0, 0.0, 0.0, 0.0, 0.0];
    for i in (1..5).rev() {
        out[i] = a[i] + b;
    }
    return out;
}

Here is the target code:

use anyhow::Result;
use wasmtime::*;

fn main() -> Result<()> {
    let store = Store::default();
    let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?;
    let instance = Instance::new(&module, &[])?;

    // Invoke `add` export
    let add = instance
        .get_func("add")
        .ok_or(anyhow::format_err!("failed to find `add` function export"))?
        .get2::<[f32;5], f32, [f32;5]>()?;

    let x = [1.1, 2.2, 3.3, 4.4, 5.5];
    let y = 3.0;
    println!("add({:?}, {:?}) = {:?}", x, y, add(x, y)?);
    Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (May 14 2020 at 14:06):

alexcrichton commented on Issue #1696:

Thanks for the report! The get* family of methods work on core wasm types, not the original types of the source language. The ABIs here will not necessarily align, for example add will not return a 5-tuple in the ABI via multi-value, but rather the Rust compiles down to a return-pointer today.

You'll likely want to rework the ABI to be closer to match what wasm is natively (only i32/f64/etc), and after that you should be able to call the function alright!

view this post on Zulip Wasmtime GitHub notifications bot (May 14 2020 at 14:06):

alexcrichton closed Issue #1696:

Summary

Hi there, I was trying to use wasmtime to instantiate *.wasm file, and some errors occurred as below:

error[E0277]: the trait bound `[f32; 5]: wasmtime::func::WasmTy` is not satisfied
  --> src/main.rs:13:10
   |
13 |         .get2::<[f32;5], f32, [f32;5]>()?;
   |          ^^^^ the trait `wasmtime::func::WasmTy` is not implemented for `[f32; 5]`

error: aborting due to previous error

From the tutorial code (https://bytecodealliance.github.io/wasmtime/examples-rust-gcd.html), I know that i32, i64, f32 and f64 are supported to be called as parameter, but what if I want to use more complex type (such like array, slice, etc), is there some approach to achieve that?

Would be appreciated if anyone could offer some helps : )

The attached info

Here is the source code:

#[no_mangle]
pub extern "C" fn add(a: [f32;5], b: f32) -> [f32;5] {
    let mut out: [f32;5] = [0.0, 0.0, 0.0, 0.0, 0.0];
    for i in (1..5).rev() {
        out[i] = a[i] + b;
    }
    return out;
}

Here is the target code:

use anyhow::Result;
use wasmtime::*;

fn main() -> Result<()> {
    let store = Store::default();
    let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?;
    let instance = Instance::new(&module, &[])?;

    // Invoke `add` export
    let add = instance
        .get_func("add")
        .ok_or(anyhow::format_err!("failed to find `add` function export"))?
        .get2::<[f32;5], f32, [f32;5]>()?;

    let x = [1.1, 2.2, 3.3, 4.4, 5.5];
    let y = 3.0;
    println!("add({:?}, {:?}) = {:?}", x, y, add(x, y)?);
    Ok(())
}

Last updated: Nov 22 2024 at 17:03 UTC