Golang到wasm的编译

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

Golang to wasm compilation

问题

这些错误是与将 Golang 代码编译为 wasm 并在 wasmtime 或 wasm3 上执行时出现的问题相关的。以下是对这些错误的解释和解决方法:

  1. 对于使用 wasmtime 执行时的错误:

    • 错误信息中提到了两个问题:failed to instantiate "main.wasm"unknown import: 'go::debug' has not been defined
    • 第一个错误可能是由于无法正确实例化 wasm 模块导致的。这可能是由于缺少必要的导入或其他问题导致的。
    • 第二个错误是由于找不到名为 go::debug 的导入而引起的。这可能是因为在编译时未正确设置导入或缺少必要的依赖项。
    • 要解决这些错误,您可以检查您的 Golang 代码和编译命令,确保正确设置了导入和依赖项。您还可以查看 wasm 模块的文档或相关资源,以了解正确的导入设置和依赖项。
  2. 对于使用 wasm3 执行时的错误:

    • 错误信息中提到了一个问题:function lookup failed ('_start')
    • 这个错误表示在 wasm 模块中找不到名为 _start 的函数。这可能是因为编译时或执行时的配置问题导致的。
    • 要解决这个错误,您可以检查您的 Golang 代码和编译命令,确保正确设置了入口函数。您还可以查看 wasm 模块的文档或相关资源,以了解正确的入口函数设置和配置。

请注意,这些错误的具体原因和解决方法可能因您的代码和环境而异。建议您仔细检查相关文档和资源,以找到适合您情况的解决方案。

英文:

I compile a Golang code to wasm using

GOOS=js GOARCH=wasm go build -o main.wasm

I get the following error when trying to execute with wasmtime

wasmtime main.wasm 
Error: failed to run main module `main.wasm`

Caused by:
    0: failed to instantiate "main.wasm"
    1: unknown import: `go::debug` has not been defined

When executing with wasm3, I get

wasm3 main.wasm 
Error: function lookup failed ('_start')

What do these errors mean and how to fix them?

答案1

得分: 2

目前还没有办法使用Go编译器在浏览器之外生成wasm。你示例中的main.wasm是用于与wasm_exec.js shim一起使用的。不过,在Node.js中有一些方法可以使用它。这里是我之前在同一主题上的回答。所以,你有以下几个选择:

  1. 使用Node.js,就像上面链接的问题中那样:
node wasm_exec.js main.wasm
  1. 使用支持wasi的tinygo进行编译:
tinygo build -target=wasi -o main.wasm main.go

wasmtime应该可以正常运行这个文件。

  1. 第三个秘密选项:Go实际上在浏览器之外对wasm有非常非常前沿的support。我不认为任何发布版本的Go编译器都启用了这个功能,所以你可能需要从源代码编译Go。编译完成后,你的命令应该是:
GOOS=wasip1 GOARCH=wasm go build -o main.wasm

没有其他变化。

对于你的问题,我建议选择(1),这样至少可以让它工作,如果你想要独立运行,可以选择(2)并使用wasmtime。如果你愿意等待而不自己编译,新的Go版本也很快会发布。

编辑:

我有时间自己研究了一下。似乎第三个选项并不难自己实现:

我按照这里的说明从源代码构建了最新的go,步骤如下:

go install golang.org/dl/gotip@latest
gotip download

你必须已经安装了go,以便能够进行引导,但是任何最近的版本都可以。按照这些说明后,你可以像使用go一样使用gotip。所以你的命令变成了:

GOOS=wasip1 GOARCH=wasm gotip build -o main.wasm

由于wasmtime支持wasi,你应该能够在不对命令进行其他修改的情况下运行你的程序。

编译gotip也不会花费太多时间,只要你的计算机足够强大。

英文:

There is currently no way to use the go compiler to produce wasm outside the broswer. The main.wasm in your example is meant to be used with the wasm_exec.js shim. There are ways to use it in Node, however. This is a previous answer of mine, on the same subject. So, your options are:

  1. use Node, as in the question linked above
node wasm_exec.js main.wasm
  1. compile with tinygo, with wasi support
tinygo build -target=wasi -o main.wasm main.go

wasmtime should run this just fine.

  1. Secret option three: Go actually has very, very bleeding edge support for wasm outside the browser. I don't think any release version of the go compiler have that enabled, so that means you will likely have to compile go from sources. After compilation, your command should become:
GOOS=wasip1 GOARCH=wasm go build -o main.wasm

with no other changes.

For your problem, I would say go with (1), in order to have at least something working, and (2) if you want to run it standalone with wasmtime. A new go release should also come soon enough, if you want to wait without compiling yourself.

EDIT:

I had time to look into it myself. It seems that option 3 is not that hard to do yourself:

I followed the instructions here to get the latest go built from sources, which are just:

go install golang.org/dl/gotip@latest
gotip download

You have to have go already installed, in order to be able to do the bootstrap, but any recent version will do. After those instructions, you can use gotip just like go. So your command becomes:

GOOS=wasip1 GOARCH=wasm gotip build -o main.wasm

As wasmtime supports wasi, you should be able to run you program without any other modifications to your commands.

The compilation of gotip should also not take too much, provided you have a powerful enough computer.

huangapple
  • 本文由 发表于 2023年4月24日 04:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76087007.html
匿名

发表评论

匿名网友

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

确定