-
Notifications
You must be signed in to change notification settings - Fork 80
/
module_test.go
123 lines (106 loc) · 3.74 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package wasmtime
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestModule(t *testing.T) {
_, err := NewModule(NewEngine(), []byte{})
require.Error(t, err)
_, err = NewModule(NewEngine(), []byte{1})
require.Error(t, err)
}
func TestModuleValidate(t *testing.T) {
require.NotNil(t, ModuleValidate(NewEngine(), []byte{}), "expected an error")
require.NotNil(t, ModuleValidate(NewEngine(), []byte{1}), "expected an error")
wasm, err := Wat2Wasm(`(module)`)
require.NoError(t, err)
require.Nil(t, ModuleValidate(NewEngine(), wasm), "expected valid module")
}
func TestModuleImports(t *testing.T) {
wasm, err := Wat2Wasm(`
(module
(import "" "f" (func))
(import "a" "g" (global i32))
(import "" "" (table 1 funcref))
(import "" "" (memory 1))
)
`)
require.NoError(t, err)
module, err := NewModule(NewEngine(), wasm)
require.NoError(t, err)
defer module.Close()
imports := module.Imports()
require.Len(t, imports, 4)
require.Equal(t, "", imports[0].Module())
require.Equal(t, "f", *imports[0].Name())
require.NotNil(t, imports[0].Type().FuncType())
require.Len(t, imports[0].Type().FuncType().Params(), 0)
require.Len(t, imports[0].Type().FuncType().Results(), 0)
require.Equal(t, "a", imports[1].Module())
require.Equal(t, "g", *imports[1].Name())
require.NotNil(t, imports[1].Type().GlobalType())
require.Equal(t, KindI32, imports[1].Type().GlobalType().Content().Kind())
require.Empty(t, imports[2].Module())
require.Empty(t, *imports[2].Name())
require.NotNil(t, imports[2].Type().TableType())
require.Equal(t, KindFuncref, imports[2].Type().TableType().Element().Kind())
require.Empty(t, imports[3].Module())
require.Empty(t, *imports[3].Name())
require.NotNil(t, imports[3].Type().MemoryType())
require.Equal(t, uint64(1), imports[3].Type().MemoryType().Minimum())
}
func TestModuleExports(t *testing.T) {
wasm, err := Wat2Wasm(`
(module
(func (export "f"))
(global (export "g") i32 (i32.const 0))
(table (export "t") 1 funcref)
(memory (export "m") 1)
)
`)
require.NoError(t, err)
module, err := NewModule(NewEngine(), wasm)
require.NoError(t, err)
exports := module.Exports()
require.Len(t, exports, 4)
require.Equal(t, "f", exports[0].Name())
require.NotNil(t, exports[0].Type().FuncType())
require.Len(t, exports[0].Type().FuncType().Params(), 0)
require.Len(t, exports[0].Type().FuncType().Results(), 0)
require.Equal(t, "g", exports[1].Name())
require.NotNil(t, exports[1].Type().GlobalType())
require.Equal(t, KindI32, exports[1].Type().GlobalType().Content().Kind())
require.Equal(t, "t", exports[2].Name())
require.NotNil(t, exports[2].Type().TableType())
require.Equal(t, KindFuncref, exports[2].Type().TableType().Element().Kind())
require.Equal(t, "m", exports[3].Name())
require.NotNil(t, exports[3].Type().MemoryType())
require.Equal(t, uint64(1), exports[3].Type().MemoryType().Minimum())
}
func TestModuleSerialize(t *testing.T) {
engine := NewEngine()
wasm, err := Wat2Wasm(`
(module
(func (export "f"))
(global (export "g") i32 (i32.const 0))
(table (export "t") 1 funcref)
(memory (export "m") 1)
)
`)
require.NoError(t, err)
module, err := NewModule(engine, wasm)
require.NoError(t, err)
bytes, err := module.Serialize()
require.NoError(t, err)
_, err = NewModuleDeserialize(engine, bytes)
require.NoError(t, err)
tmpfile, err := os.CreateTemp("", "example")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
_, err = tmpfile.Write(bytes)
require.NoError(t, err)
tmpfile.Close()
_, err = NewModuleDeserializeFile(engine, tmpfile.Name())
require.NoError(t, err)
}