我正在使用 tinygo 为简单的功能生成一个 wasm://export onInputfunc onInput() map[string]interface{} { return map[string]interface{}{ "key": 60, "remove": 1, }}然后我使用 wasm 目标使用 tinygo 构建,如:tinygo build -o main.wasm -target wasm ./main.go当我调用该方法wasm.exports.onInput()时,我得到一个数字,例如:102752我将如何获取 JS 对象作为返回值,例如:{ key: 60, remove: 1 }// Or array [60, 1] if possible笔记:tinygo 文档说:WebAssembly 目标不会直接返回 JavaScript 无法处理的变量(参见上面关于 i64,还有 struct,i64,多个返回值等)。相反,它们存储在由调用者作为第一个参数传递的指针中。如果那是问题的原因,我将如何将返回值作为来自 javascript 的指针传递?编辑我无法弄清楚如何从 go 函数返回数组、字符串或映射。我会满足于以上任何一种。
1 回答

蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
根据tinygo github 上的示例,您可以尝试这样的操作:
package main
import "syscall/js"
func main() {
wait := make(chan struct{}, 0)
js.Global().Set("onInput", js.FuncOf(onInput))
<-wait
}
// note that there is no export as we registered this function in global
func onInput(this js.Value, args []js.Value) interface{} {
return js.ValueOf(map[string]interface{}{
"key": 60,
"remove": 1,
})
}
在你的 js 代码中使用 just onInput, without wasmModule.instance.exportsprefix
- 1 回答
- 0 关注
- 74 浏览
添加回答
举报
0/150
提交
取消