Arrays 在Go中调用WebAssembly代码时如何使用Go切片?
我想在Go中使用WebAssembly计算数组的和:Arrays 在Go中调用WebAssembly代码时如何使用Go切片?,arrays,go,webassembly,Arrays,Go,Webassembly,我想在Go中使用WebAssembly计算数组的和: package main import ( "encoding/binary" "encoding/hex" "fmt" wasm "github.com/wasmerio/go-ext-wasm/wasmer" ) const length int32 = 4 func main() { // Instantiate the module. wasmbyte, _ := wasm.Re
package main
import (
"encoding/binary"
"encoding/hex"
"fmt"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
)
const length int32 = 4
func main() {
// Instantiate the module.
wasmbyte, _ := wasm.ReadBytes("test.wasm")
instance, _ := wasm.NewInstance(wasmbyte)
defer instance.Close()
hasmemory := instance.HasMemory()
fmt.Println("it has memory:", (hasmemory))
a := []int32{1, 2, 3, 4}
var i int32
var ptr [length]*int32
for i = 0; i < length; i++ {
ptr[i] = &a[i]
fmt.Printf("Value of a[%d] = %d\n", i, *ptr[i])
// pass int value
// lengths := binary.LittleEndian.Uint32(a)
// fmt.Printf("customLen=%d\n", int32(lengths))
// result := int32(lengths)
allocateResult, err := instance.Exports["bar"](*ptr[i], length)
if err != nil {
fmt.Println("error is here", err)
}
binary.LittleEndian.PutUint32(instance.Memory.Data()[0:4], uint32(length))
inputPointer := allocateResult.ToI32()
// Write the subject into the memory.
memory := instance.Memory.Data()[inputPointer:]
binary.LittleEndian.Uint32(memory)
resp := hex.EncodeToString(memory)
fmt.Println("resp:", resp)
}
}
感谢将数据复制到WebAssembly内存,例如WebAssembly内存地址0:
a := []int32{10, 20, 30, 40}
// Copy data to wasm memory:
bytes := instance.Memory.Data()
for i, v := range a {
binary.LittleEndian.PutUint32(bytes[4*i:], uint32(v))
}
从WebAssembly实例获取导出的bar函数:
bar := instance.Exports["bar"]
使用WebAssembly内存地址和长度调用导出的函数:
result, err := bar(0, 4)
if err != nil {
panic(err)
}
fmt.Println(result)
main.go文件:
输出转到运行main.go:
以下src/lib.rs文件具有相同的wasm二进制文件,因为Rust切片只有指针和长度:
result, err := bar(0, 4)
if err != nil {
panic(err)
}
fmt.Println(result)
[没有损坏]
发布外部C fn条切片:&[i32]->i32{
slice.iter.sum
}
转换为test.wasm:
rustc-目标wasm32未知-O-板条箱类型=cdylib src/lib.rs-O test.wasm
输出转换为wasm2wat test.wasm-o test.wat以查看函数$bar arguments param i32 i32:
谢谢你的回答。为什么我需要在lib.rs中添加锈代码我不能使用main.rs吗?并且想要使用cargo build-target=wasm32 UnknownUp,这两个代码都在工作。但是在lib.rs中编写代码时,使用此命令rustc-target wasm32 unknown-O-cratetype=cdylib src/lib.rs-O test.wasm。我想知道为什么它对cargo build不起作用。我添加了这一点,并在运行go代码时仍然运行cargo build。结果是0 Let us。
it has memory: true
100
(module
(type (;0;) (func (param i32 i32) (result i32)))
(func $bar (type 0) (param i32 i32) (result i32)
(local i32)
block ;; label = @1
local.get 1
br_if 0 (;@1;)
i32.const 0
return
end
local.get 1
i32.const 2
i32.shl
local.set 2
i32.const 0
local.set 1
loop ;; label = @1
local.get 0
i32.load
local.get 1
i32.add
local.set 1
local.get 0
i32.const 4
i32.add
local.set 0
local.get 2
i32.const -4
i32.add
local.tee 2
br_if 0 (;@1;)
end
local.get 1)
(table (;0;) 1 1 funcref)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "bar" (func $bar))
(export "__data_end" (global 1))
(export "__heap_base" (global 2)))