如何从TinyGo WebAssembly目标返回对象

huangapple go评论59阅读模式
英文:

How to return object from tinygo webassembly target

问题

我正在使用tinygo生成一个简单函数的wasm:

//export onInput
func 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 }

// 或者如果可能的话,返回数组 [60, 1]

注意:

tinygo文档中提到:

WebAssembly目标不能直接返回JavaScript无法处理的变量(例如i64、struct、多个返回值等)。相反,它们被存储在由调用者作为第一个参数传递的指针中。

如果这是问题的原因,我该如何将返回值作为指针从JavaScript传递?

编辑:

我无法弄清楚如何从go函数返回数组、字符串或映射中的任何一个。我愿意接受上述任何一种返回值。

英文:

I am using tinygo to generate a wasm for simple function:

//export onInput
func onInput() map[string]interface{} {
	return map[string]interface{}{
		"key": 60,
		"remove": 1,
	}
}

Then I am using wasm target to build using tinygo, as:

tinygo build -o main.wasm -target wasm ./main.go

And when I call the method wasm.exports.onInput() I am getting a number such as: 102752

How would I get the JS object as a return value like:

{ key: 60, remove: 1 }

// Or array [60, 1] if possible

Note:

The tinygo documentation says:

> The WebAssembly target does not return variables directly that cannot be handled by JavaScript (see above about i64, also struct, i64, multiple return values, etc). Instead, they are stored into a pointer passed as the first parameter by the caller.

If that's the cause of the issue, how would I pass the return value as a pointer from javascript?

Edit

I wasn’t able to figure out how I would return any of: arrays, strings or maps from a go function. I would settle for any of the above.

答案1

得分: 1

根据 tinygo 的 GitHub 上的示例,你可以尝试以下代码:

package main

import "syscall/js"

func main() {
    wait := make(chan struct{}, 0)
    js.Global().Set("onInput", js.FuncOf(onInput))
    <-wait
}

// 注意,此函数没有导出,因为我们在全局注册了这个函数
func onInput(this js.Value, args []js.Value) interface{} {
    return js.ValueOf(map[string]interface{}{
        "key":    60,
        "remove": 1,
    })
}

在你的 JavaScript 代码中,只需使用 onInput,而不需要 wasmModule.instance.exports 前缀。

你可以参考这个链接获取更多信息:https://github.com/tinygo-org/tinygo/blob/138befd8a25d2a5c65e2e740038a8c3a44e95222/src/examples/wasm/slices/wasm.go

英文:

According to example at tinygo github you can try something like this:

package main

import &quot;syscall/js&quot;

func main() {
	wait := make(chan struct{}, 0)
	js.Global().Set(&quot;onInput&quot;, js.FuncOf(onInput))
	&lt;-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{}{
		&quot;key&quot;:    60,
		&quot;remove&quot;: 1,
	})
}

And in your js code use just onInput, without wasmModule.instance.exports prefix

huangapple
  • 本文由 发表于 2022年6月7日 02:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/72522309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定