1 package binary
2
3 import (
4 "testing"
5
6 "github.com/tetratelabs/wazero/internal/testing/binaryencoding"
7 "github.com/tetratelabs/wazero/internal/testing/require"
8 "github.com/tetratelabs/wazero/internal/wasm"
9 )
10
11 func TestEncodeImport(t *testing.T) {
12 ptrOfUint32 := func(v uint32) *uint32 {
13 return &v
14 }
15
16 tests := []struct {
17 name string
18 input *wasm.Import
19 expected []byte
20 }{
21 {
22 name: "func no module, no name, type index 0",
23 input: &wasm.Import{
24 Type: wasm.ExternTypeFunc,
25 Module: "",
26 Name: "",
27 DescFunc: 0,
28 },
29 expected: []byte{wasm.ExternTypeFunc, 0x00, 0x00, 0x00},
30 },
31 {
32 name: "func module, no name, type index 0",
33 input: &wasm.Import{
34 Type: wasm.ExternTypeFunc,
35 Module: "test",
36 Name: "",
37 DescFunc: 0,
38 },
39 expected: []byte{
40 0x04, 't', 'e', 's', 't',
41 0x00,
42 wasm.ExternTypeFunc,
43 0x00,
44 },
45 },
46 {
47 name: "func module, name, type index 0",
48 input: &wasm.Import{
49 Type: wasm.ExternTypeFunc,
50 Module: "math",
51 Name: "pi",
52 DescFunc: 0,
53 },
54 expected: []byte{
55 0x04, 'm', 'a', 't', 'h',
56 0x02, 'p', 'i',
57 wasm.ExternTypeFunc,
58 0x00,
59 },
60 },
61 {
62 name: "func module, name, type index 10",
63 input: &wasm.Import{
64 Type: wasm.ExternTypeFunc,
65 Module: "math",
66 Name: "pi",
67 DescFunc: 10,
68 },
69 expected: []byte{
70 0x04, 'm', 'a', 't', 'h',
71 0x02, 'p', 'i',
72 wasm.ExternTypeFunc,
73 0x0a,
74 },
75 },
76 {
77 name: "global const",
78 input: &wasm.Import{
79 Type: wasm.ExternTypeGlobal,
80 Module: "math",
81 Name: "pi",
82 DescGlobal: wasm.GlobalType{ValType: wasm.ValueTypeF64},
83 },
84 expected: []byte{
85 0x04, 'm', 'a', 't', 'h',
86 0x02, 'p', 'i',
87 wasm.ExternTypeGlobal,
88 wasm.ValueTypeF64, 0x00,
89 },
90 },
91 {
92 name: "global var",
93 input: &wasm.Import{
94 Type: wasm.ExternTypeGlobal,
95 Module: "math",
96 Name: "pi",
97 DescGlobal: wasm.GlobalType{ValType: wasm.ValueTypeF64, Mutable: true},
98 },
99 expected: []byte{
100 0x04, 'm', 'a', 't', 'h',
101 0x02, 'p', 'i',
102 wasm.ExternTypeGlobal,
103 wasm.ValueTypeF64, 0x01,
104 },
105 },
106 {
107 name: "table",
108 input: &wasm.Import{
109 Type: wasm.ExternTypeTable,
110 Module: "my",
111 Name: "table",
112 DescTable: wasm.Table{Min: 1, Max: ptrOfUint32(2)},
113 },
114 expected: []byte{
115 0x02, 'm', 'y',
116 0x05, 't', 'a', 'b', 'l', 'e',
117 wasm.ExternTypeTable,
118 wasm.RefTypeFuncref,
119 0x1, 0x1, 0x2,
120 },
121 },
122 {
123 name: "memory",
124 input: &wasm.Import{
125 Type: wasm.ExternTypeMemory,
126 Module: "my",
127 Name: "memory",
128 DescMem: &wasm.Memory{Min: 1, Max: 2, IsMaxEncoded: true},
129 },
130 expected: []byte{
131 0x02, 'm', 'y',
132 0x06, 'm', 'e', 'm', 'o', 'r', 'y',
133 wasm.ExternTypeMemory,
134 0x1, 0x1, 0x2,
135 },
136 },
137 {
138 name: "memory - defaultt max",
139 input: &wasm.Import{
140 Type: wasm.ExternTypeMemory,
141 Module: "my",
142 Name: "memory",
143 DescMem: &wasm.Memory{Min: 1, Max: wasm.MemoryLimitPages, IsMaxEncoded: false},
144 },
145 expected: []byte{
146 0x02, 'm', 'y',
147 0x06, 'm', 'e', 'm', 'o', 'r', 'y',
148 wasm.ExternTypeMemory,
149 0x0, 0x1,
150 },
151 },
152 }
153
154 for _, tt := range tests {
155 tc := tt
156
157 t.Run(tc.name, func(t *testing.T) {
158 bytes := binaryencoding.EncodeImport(tc.input)
159 require.Equal(t, tc.expected, bytes)
160 })
161 }
162 }
163
View as plain text