如何在Chrome扩展中包含由Golang编译的WASM文件?

9
我已经尝试了这里的所有方法: Golang到wasm的编译 但是我无法解决以下问题:
WebAssembly.instantiate(buffer, {wasi_snapshot_preview1: ""})
WebAssembly.instantiate(buffer, {go: {debug: ""}})

// (i'm making them empty strings just to show I need to add the key but obviously they are not strings.)

我经常遇到类似的错误:
Failed to load WebAssembly module: TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function

或者模块debugruntime.resetMemoryDataViewwasm_exec.js中的所有内容
但是如果我使用simple.wasm来自:

https://github.com/inflatablegrade/Extension-with-WASM

太棒了!那个 wasm 必须是由 C 或 Rust 编译的,而不是 Golang?有没有办法让它在 Golang 中工作?

P.S. 那个 Extension-with-WASM 是 2.0 版本的清单,但我已经让它在 3.0 版本中工作了:

https://github.com/inflatablegrade/Extension-with-WASM/issues/1


我对此并不完全确定,但我相信Go语言中的WASI快照错误可能是因为Go仍不支持WASI(Go 1.21将会支持,计划在今年8月发布https://tip.golang.org/doc/go1.21)。 - Joy Singh
我对此并不完全确定,但我相信Go语言中的WASI快照错误可能是因为Go仍不支持WASI(Go 1.21将在今年8月份支持,详情请参考https://tip.golang.org/doc/go1.21)。 - Joy Singh
啊,谢谢!这很有帮助! - Andrew Arrow
啊,谢谢!这真的帮了我很多! - Andrew Arrow
啊,谢谢!这真的帮了我很多! - undefined
1个回答

0

尝试使用 -Oz 标志进行编译:GOOS=js GOARCH=wasm go build -o main.wasm -ldflags="-s -w" -gcflags="-trimpath=${PWD}" -trimpath .

并且构建:GOOS=js GOARCH=wasm go build -o main.wasm -tags=js_wasm_profiling -ldflags="-s -w" -gcflags="-trimpath=${PWD}" -trimpath .


加载WebAssembly模块失败:TypeError: WebAssembly.instantiate(): 导入#0模块="go"错误:模块不是对象或函数。 - Andrew Arrow
你使用的 Go 版本是什么?运行:go version - Robert Mihai Ionas
go版本为go1.19.2 darwin/arm64,但我现在将升级到最新版本... - Andrew Arrow
与go版本go1.20.5 darwin/arm64相同的错误。 - Andrew Arrow
请查看https://golangbot.com/webassembly-using-go/,你是否正确导入了syscall/js模块? - Robert Mihai Ionas
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接