-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwasm_test.go
More file actions
61 lines (54 loc) · 1.86 KB
/
wasm_test.go
File metadata and controls
61 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"os"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
func TestWasmAdd(t *testing.T) {
ctx := context.Background()
// JIT模式,性能一般,但是兼容性好
//r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
// AOT编译模式,性能好,存在操作系统兼容性问题
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)
bytes, err := os.ReadFile(datadir + "/wasm/add.wasm")
if err != nil {
t.Error(err)
}
// Instantiate WASI, which implements host functions needed for TinyGo to
// implement `panic`.
wasi_snapshot_preview1.MustInstantiate(ctx, r)
// go 1.24 配置初始化函数
config := wazero.NewModuleConfig().WithStartFunctions("_initialize")
mod, err := r.InstantiateWithConfig(ctx, bytes, config)
// tinygo
//mod, err := r.Instantiate(ctx, bytes)
if err != nil {
t.Error(err)
}
res, err := mod.ExportedFunction("add").Call(ctx, 100, 200)
//res, err := mod.ExportedFunction("add").Call(ctx, api.EncodeI32(100), api.EncodeI32(200))
if err != nil {
t.Error(err)
}
fmt.Println(res[0])
}