使用TinyGo将Golang编译为WebAssembly(wasm),并使用wasmtime执行。

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

Golang to wasm compilation using tinygo. Execution using wasmtime

问题

我有以下测试代码:

func main() {
    path, err := os.Getwd()
    log.Println(path, err)
    files, err := ioutil.ReadDir("/etc/dse")
    log.Println(files, err)
}

我使用以下命令将其编译为 wasm:

tinygo build -target wasi -o list.wasm list.go

然后我使用以下命令执行它:

wasmtime list.wasm

输出结果为:

2023/05/05 16:00:50 / <nil>
2023/05/05 16:00:50 [] open /etc/dse: errno 76

同时,目录 /etc/dse 存在且具有权限 777。

这个错误的原因是什么,如何修复它?

英文:

I have the following test code

func main() {
	path, err := os.Getwd()
	log.Println(path, err)
	files, err := ioutil.ReadDir(&quot;/etc/dse&quot;)
	log.Println(files, err)
}

I compile it to wasm using

tinygo build -target wasi -o list.wasm list.go

Then I executeit using

wasmtime list.wasm

The output is

2023/05/05 16:00:50 / &lt;nil&gt;
2023/05/05 16:00:50 [] open /etc/dse: errno 76

At the same time, the directory /etc/dse exists and has permissions 777.

What is the source of this error and how to fix this?

答案1

得分: 1

errno 76 表示 capabilities insufficient

你需要给它提供访问所需目录中的文件的能力,使用 wasmtime--dir 选项(参见 wasmtime WASI 教程):

> --dir= 选项指示 wasmtime 预打开一个目录,并将其作为能力提供给程序,以便在该目录中打开文件。

$ wasmtime --dir=/etc/dse list.wasm
2023/05/06 01:36:31 / &lt;nil&gt;
2023/05/06 01:36:31 [] readdir unimplemented : errno 54

不幸的是,TinyGo 中尚未实现 readdir

参考资料:

英文:

errno 76 means capabilities insufficient.

You have to give it capabilities to access files in the requisite directories, with the wasmtime option --dir (see wasmtime WASI tutorial):

> The --dir= option instructs wasmtime to preopen a directory, and make it available to the program as a capability which can be used to open files inside that directory.

$ wasmtime --dir=/etc/dse list.wasm
2023/05/06 01:36:31 / &lt;nil&gt;
2023/05/06 01:36:31 [] readdir unimplemented : errno 54

Unfortunately, readdir is not implemented in TinyGo yet.

References:

huangapple
  • 本文由 发表于 2023年5月6日 00:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76184105.html
匿名

发表评论

匿名网友

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

确定