1 Star 0 Fork 0

凹语言开发工作室/wazero

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
example_test.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
chai2010 提交于 2023-07-07 23:07 . 模块路径改为 wa-lang.org/wazero
package wazero_test
import (
"context"
_ "embed"
"fmt"
"log"
"wa-lang.org/wazero"
"wa-lang.org/wazero/imports/wasi_snapshot_preview1"
)
// addWasm was generated by the following:
//
// cd examples/basic/testdata; tinygo build -o add.wasm -target=wasi add.go
//
//go:embed examples/basic/testdata/add.wasm
var addWasm []byte
// This is an example of how to extend a Go application with an addition
// function defined in WebAssembly.
//
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure
// WASI host imports.
//
// A complete project that does the same as this is available here:
// https://github.com/tetratelabs/wazero/tree/main/examples/basic
func Example() {
// Choose the context to use for function calls.
ctx := context.Background()
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.
// Instantiate WASI, which implements host functions needed for TinyGo to
// implement `panic`.
wasi_snapshot_preview1.MustInstantiate(ctx, r)
// Instantiate the guest Wasm into the same runtime. It exports the `add`
// function, implemented in WebAssembly.
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
if err != nil {
log.Panicln(err)
}
// Call the `add` function and print the results to the console.
x, y := uint64(1), uint64(2)
results, err := mod.ExportedFunction("add").Call(ctx, x, y)
if err != nil {
log.Panicln(err)
}
fmt.Printf("%d + %d = %d\n", x, y, results[0])
// Output:
// 1 + 2 = 3
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wa-lang/wazero.git
git@gitee.com:wa-lang/wazero.git
wa-lang
wazero
wazero
master

搜索帮助