如何在使用tinygo编译的WASM中使用–import-memory选项?

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

How do I make --import-memory work for WASM compiled by tinygo

问题

我正在尝试使用tinygo从go编译WASM,以便在go-wasmer中使用。我已经成功导入了AssemblyScript的内存,但是当我使用来自go的WASM标志并尝试创建实例时,我遇到了以下错误:
Host env initialization error: Missing export memory
有没有办法手动导出内存,然后再由导入的内存替换掉?
这是我的target.json文件:

{
    "inherits": ["wasm"],
    "linker": "wasm-ld",
    "libc": "wasi-libc",
    "goarch": "wasm",
    "cflags": [
        "--target=wasm32--wasi",
        "--sysroot={root}/lib/wasi-libc/sysroot",
        "-Oz"
    ],
    "ldflags": [
        "--import-memory",
        "--max-memory=1310720",
        "--initial-memory=131072"
    ]
}
英文:

I am trying to compile WASM from go using tinygo for use in go-wasmer. I have the import memory working for AssemblyScript but when I use the flag for the WASM from go and try to create my instance I get this error
Host env initialization error: Missing export memory
Is there a way to manually export memory that will then be replaced by the imported memory?
Here is my target.json

{
    "inherits": ["wasm"],
    "linker": "wasm-ld",
    "libc": "wasi-libc",
    "goarch": "wasm",
    "cflags": [
        "--target=wasm32--wasi",
        "--sysroot={root}/lib/wasi-libc/sysroot",
        "-Oz"
      ],
    "ldflags": [
        "--import-memory",
        "--max-memory=1310720",
        "--initial-memory=131072"
    ]
}

答案1

得分: 1

你从哪里得到了target.json,为什么需要它?只使用-target wasi应该可以得到一个可用的结果。

但更具体地说,错误提示是告诉你Missing export memory,但你正在尝试使用--import-memory。你可以删除--import-memory,内存导入将变为导出。如果这样还不行,你应该将设置go-wasmer的代码粘贴出来。另外,使用wasmer inspect yourbinary.wasm查看导入和导出列表的情况。

顺便说一下,通过从wasm继承,你会得到对恶心的Go wasm-js实现特定导入(例如env.syscall/js.valueGet)的要求。有一些实现可以解决这些问题(例如这个),但由于你已经在使用wasi,你可以直接从wasi继承(并删除goarch)。这样你的生活可能会更轻松一些。

英文:

Where did you get that target.json from, and why do you need it? Just using -target wasi should give you something that works.

But more specifically: the error is telling you that Missing export memory, but you're trying to --import-memory. You can delete the --import-memory, and the memory import will turn into an export. If that doesn't work, you should probably paste your code setting up go-wasmer. Also, have a look at wasmer inspect yourbinary.wasm, to see what's going on with the list of imports and exports.

By the way, by inheriting from wasm, you get requirements for nasty Go wasm-js implementation specific imports like env.syscall/js.valueGet. There are implementations for those (this e.g.), but since you're already using wasi, you can just inherit from wasi (and delete goarch). Your life will probably be easier.

huangapple
  • 本文由 发表于 2023年2月15日 12:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75455366.html
匿名

发表评论

匿名网友

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

确定