bytecodealliance/wasmtime-go
GitHub: bytecodealliance/wasmtime-go
wasmtime-go 是一个 Go 语言的 WebAssembly 运行时绑定库,让 Go 程序能够加载并执行 WebAssembly 模块。
Stars: 907 | Forks: 98
## 安装
```
go get -u github.com/bytecodealliance/wasmtime-go/v46@v46.0.1
```
请务必查看 [API 文档][api]!
这个 Go 库使用 CGO 来调用由 Rust 编写的 [Wasmtime 项目][wasmtime] 的 C API。Wasmtime 的预编译二进制文件在打标签的发布版本中会被提交到此存储库中,因此您无需在本地安装 Wasmtime,但这意味着该项目目前仅支持 Linux x86\_64、macOS x86\_64 和 Windows x86\_64。在其他平台上构建需要自行安排构建 Wasmtime,并使用 `CGO_*` 环境变量来进行正确编译。该项目已在 Go 1.13 或更高版本上经过测试。
如果您是 bazel 用户,请将以下内容添加到您的 WORKSPACE 文件中:
```
go_repository(
name = "com_github_bytecodealliance_wasmtime_go",
importpath = "github.com/bytecodealliance/wasmtime-go/v46",
version = "v46.0.1",
)
```
## 使用方法
一个使用此包的“Hello, world!”示例如下:
```
package main
import (
"fmt"
"github.com/bytecodealliance/wasmtime-go/v46"
)
func main() {
// Almost all operations in wasmtime require a contextual `store`
// argument to share, so create that first
store := wasmtime.NewStore(wasmtime.NewEngine())
// Compiling modules requires WebAssembly binary input, but the wasmtime
// package also supports converting the WebAssembly text format to the
// binary format.
wasm, err := wasmtime.Wat2Wasm(`
(module
(import "" "hello" (func $hello))
(func (export "run")
(call $hello))
)
`)
check(err)
// Once we have our binary `wasm` we can compile that into a `*Module`
// which represents compiled JIT code.
module, err := wasmtime.NewModule(store.Engine, wasm)
check(err)
// Our `hello.wat` file imports one item, so we create that function
// here.
item := wasmtime.WrapFunc(store, func() {
fmt.Println("Hello from Go!")
})
// Next up we instantiate a module which is where we link in all our
// imports. We've got one import so we pass that in here.
instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{item})
check(err)
// After we've instantiated we can lookup our `run` function and call
// it.
run := instance.GetFunc(store, "run")
if run == nil {
panic("not a function")
}
_, err = run.Call(store)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}
```
然后您就可以顺利开始使用了!
### 发布检查清单
首先运行:
```
$ python3 ci/download-wasmtime.py
$ go test
```
确保当前版本的所有测试都能通过。
接下来运行:
```
$ git ls-files | xargs sed -i 's/v16/v17/g'
$ python3 ci/download-wasmtime.py
$ go test
```
修复所有错误,然后提交并创建一个 PR。
合并后,检出 `main` 分支并执行:
```
$ rm .gitignore
$ python3 ci/download-wasmtime.py
$ git add .
$ git commit -m 'v46.0.0 release artifacts'
$ git tag v46.0.0
```
然后推送该 tag
标签:AI工具, CGO, EVTX分析, Go, Ruby工具, Wasmtime, WebAssembly, 可视化界面, 嵌入式引擎, 日志审计, 运行时