-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule_test.go
93 lines (74 loc) · 2.39 KB
/
module_test.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package wazergo_test
import (
"context"
"os"
"testing"
"github.com/stealthrocket/wazergo"
. "github.com/stealthrocket/wazergo/types"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
var hostModule wazergo.HostModule[*hostInstance] = hostFunctions{
"answer": wazergo.F0((*hostInstance).Answer),
}
type hostFunctions wazergo.Functions[*hostInstance]
func (m hostFunctions) Name() string {
return "test"
}
func (m hostFunctions) Functions() wazergo.Functions[*hostInstance] {
return (wazergo.Functions[*hostInstance](m))
}
func (m hostFunctions) Instantiate(ctx context.Context, opts ...wazergo.Option[*hostInstance]) (*hostInstance, error) {
ins := new(hostInstance)
wazergo.Configure(ins, opts...)
return ins, nil
}
type hostInstance struct {
answer int
}
func (m *hostInstance) Close(ctx context.Context) error {
return nil
}
func (m *hostInstance) Answer(ctx context.Context) Int32 {
return Int32(m.answer)
}
func answer(a int) wazergo.Option[*hostInstance] {
return wazergo.OptionFunc(func(m *hostInstance) { m.answer = a })
}
func TestMultipleHostModuleInstances(t *testing.T) {
ctx := context.Background()
runtime := wazero.NewRuntime(ctx)
defer runtime.Close(ctx)
// three copies, all share the same host module name but different state
instance0 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(0))
instance1 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(21))
instance2 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(42))
defer instance0.Close(ctx)
defer instance1.Close(ctx)
defer instance2.Close(ctx)
guest, err := loadModule(ctx, runtime, "testdata/answer.wasm")
if err != nil {
t.Fatal(err)
}
defer guest.Close(ctx)
answer := guest.ExportedFunction("answer")
r0, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance0))
r1, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance1))
r2, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance2))
for i, test := range [...]struct{ want, got int }{
{want: 0, got: int(r0[0])},
{want: 21, got: int(r1[0])},
{want: 42, got: int(r2[0])},
} {
if test.want != test.got {
t.Errorf("result %d is wrong: want=%d got=%d", i, test.want, test.got)
}
}
}
func loadModule(ctx context.Context, runtime wazero.Runtime, filePath string) (api.Module, error) {
b, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return runtime.Instantiate(ctx, b)
}