在Go中调用Webassembly代码时如何返回Go字节数组?

问题描述

我想计算斐波那契数列结果。这是我的Rust代码,它将返回给定数字的序列。

#[no_mangle]
pub extern "C" fn fibonacci(n: usize) -> Vec<i32> {
    let mut x = vec![1,1];
    for i in 2..n {
        let next_x = x[i - 1] + x[i - 2];
        x.push(next_x)
    }
    x
}

@H_404_4@

货物运行[1,1,2]@H_404_4@的结果 当我在go-ext-wasmer中调用代码时,得到的输出无效。

    bytes,_ := wasm.ReadBytes("fib.wasm")
    instance,_ := wasm.NewInstance(bytes)
    defer instance.Close()
    randoms,err := instance.Exports["fibonacci"](3,0)

    resp := randoms.ToI32()
    if err != nil {
        fmt.Println("error",err)
    }
    memory = instance.Memory.Data()
    fmt.Println("fibonacci:",memory[resp:resp+10])
@H_404_4@

转到fibonacci: [10 0 0 144 0 17 0 2 0 0 0 2 0 0 0]@H_404_4@的结果

我如何从转账中获得正确的回报?我想要一个应该为[] byte格式的系列

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)