forked from go-interpreter/wagon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
352 lines (298 loc) · 7.12 KB
/
types.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wasm
import (
"fmt"
"io"
"github.com/SummerCash/wagon/wasm/leb128"
)
type Marshaler interface {
// MarshalWASM encodes an object into w using WASM binary encoding.
MarshalWASM(w io.Writer) error
}
type Unmarshaler interface {
// UnmarshalWASM decodes an object from r using WASM binary encoding.
UnmarshalWASM(r io.Reader) error
}
// ValueType represents the type of a valid value in Wasm
type ValueType int8
const (
ValueTypeI32 ValueType = -0x01
ValueTypeI64 ValueType = -0x02
ValueTypeF32 ValueType = -0x03
ValueTypeF64 ValueType = -0x04
)
var valueTypeStrMap = map[ValueType]string{
ValueTypeI32: "i32",
ValueTypeI64: "i64",
ValueTypeF32: "f32",
ValueTypeF64: "f64",
}
func (t ValueType) String() string {
str, ok := valueTypeStrMap[t]
if !ok {
str = fmt.Sprintf("<unknown value_type %d>", int8(t))
}
return str
}
// TypeFunc represents the value type of a function
const TypeFunc int = -0x20
func (t *ValueType) UnmarshalWASM(r io.Reader) error {
v, err := leb128.ReadVarint32(r)
if err != nil {
return err
}
*t = ValueType(v)
return nil
}
func (t ValueType) MarshalWASM(w io.Writer) error {
_, err := leb128.WriteVarint64(w, int64(t))
return err
}
// BlockType represents the signature of a structured block
type BlockType ValueType // varint7
const BlockTypeEmpty BlockType = -0x40
func (b BlockType) String() string {
if b == BlockTypeEmpty {
return "<empty block>"
}
return ValueType(b).String()
}
// ElemType describes the type of a table's elements
type ElemType int // varint7
// ElemTypeAnyFunc descibres an any_func value
const ElemTypeAnyFunc ElemType = -0x10
func (t *ElemType) UnmarshalWASM(r io.Reader) error {
b, err := leb128.ReadVarint32(r)
if err != nil {
return err
}
*t = ElemType(b)
return nil
}
func (t ElemType) MarshalWASM(w io.Writer) error {
_, err := leb128.WriteVarint64(w, int64(t))
return err
}
func (t ElemType) String() string {
if t == ElemTypeAnyFunc {
return "anyfunc"
}
return "<unknown elem_type>"
}
// FunctionSig describes the signature of a declared function in a WASM module
type FunctionSig struct {
// value for the 'func` type constructor
Form int8
// The parameter types of the function
ParamTypes []ValueType
ReturnTypes []ValueType
}
func (f FunctionSig) String() string {
return fmt.Sprintf("<func %v -> %v>", f.ParamTypes, f.ReturnTypes)
}
type InvalidTypeConstructorError struct {
Wanted int
Got int
}
func (e InvalidTypeConstructorError) Error() string {
return fmt.Sprintf("wasm: invalid type constructor: wanted %d, got %d", e.Wanted, e.Got)
}
func (f *FunctionSig) UnmarshalWASM(r io.Reader) error {
form, err := leb128.ReadVarint32(r)
if err != nil {
return err
}
f.Form = int8(form)
paramCount, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
f.ParamTypes = make([]ValueType, paramCount)
for i := range f.ParamTypes {
err = f.ParamTypes[i].UnmarshalWASM(r)
if err != nil {
return err
}
}
returnCount, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
f.ReturnTypes = make([]ValueType, returnCount)
for i := range f.ReturnTypes {
err = f.ReturnTypes[i].UnmarshalWASM(r)
if err != nil {
return err
}
}
return nil
}
func (f *FunctionSig) MarshalWASM(w io.Writer) error {
_, err := leb128.WriteVarint64(w, int64(f.Form))
if err != nil {
return err
}
_, err = leb128.WriteVarUint32(w, uint32(len(f.ParamTypes)))
if err != nil {
return err
}
for _, p := range f.ParamTypes {
err = p.MarshalWASM(w)
if err != nil {
return err
}
}
_, err = leb128.WriteVarUint32(w, uint32(len(f.ReturnTypes)))
if err != nil {
return err
}
for _, p := range f.ReturnTypes {
err = p.MarshalWASM(w)
if err != nil {
return err
}
}
return nil
}
// GlobalVar describes the type and mutability of a declared global variable
type GlobalVar struct {
Type ValueType // Type of the value stored by the variable
Mutable bool // Whether the value of the variable can be changed by the set_global operator
}
func (g *GlobalVar) UnmarshalWASM(r io.Reader) error {
*g = GlobalVar{}
err := g.Type.UnmarshalWASM(r)
if err != nil {
return err
}
m, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
g.Mutable = m == 1
return nil
}
func (g *GlobalVar) MarshalWASM(w io.Writer) error {
if err := g.Type.MarshalWASM(w); err != nil {
return err
}
var m uint32
if g.Mutable {
m = 1
}
if _, err := leb128.WriteVarUint32(w, m); err != nil {
return err
}
return nil
}
// Table describes a table in a Wasm module.
type Table struct {
// The type of elements
ElementType ElemType
Limits ResizableLimits
}
func (t *Table) UnmarshalWASM(r io.Reader) error {
err := t.ElementType.UnmarshalWASM(r)
if err != nil {
return err
}
err = t.Limits.UnmarshalWASM(r)
if err != nil {
return err
}
return err
}
func (t *Table) MarshalWASM(w io.Writer) error {
if err := t.ElementType.MarshalWASM(w); err != nil {
return err
}
if err := t.Limits.MarshalWASM(w); err != nil {
return err
}
return nil
}
type Memory struct {
Limits ResizableLimits
}
func (m *Memory) UnmarshalWASM(r io.Reader) error {
return m.Limits.UnmarshalWASM(r)
}
func (m *Memory) MarshalWASM(w io.Writer) error {
return m.Limits.MarshalWASM(w)
}
// External describes the kind of the entry being imported or exported.
type External uint8
const (
ExternalFunction External = 0
ExternalTable External = 1
ExternalMemory External = 2
ExternalGlobal External = 3
)
func (e External) String() string {
switch e {
case ExternalFunction:
return "function"
case ExternalTable:
return "table"
case ExternalMemory:
return "memory"
case ExternalGlobal:
return "global"
default:
return "<unknown external_kind>"
}
}
func (e *External) UnmarshalWASM(r io.Reader) error {
bytes, err := readBytes(r, 1)
if err != nil {
return err
}
*e = External(bytes[0])
return nil
}
func (e External) MarshalWASM(w io.Writer) error {
_, err := w.Write([]byte{byte(e)})
return err
}
// ResizableLimits describe the limit of a table or linear memory.
type ResizableLimits struct {
Flags uint32 // 1 if the Maximum field is valid
Initial uint32 // initial length (in units of table elements or wasm pages)
Maximum uint32 // If flags is 1, it describes the maximum size of the table or memory
}
func (lim *ResizableLimits) UnmarshalWASM(r io.Reader) error {
*lim = ResizableLimits{}
f, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
lim.Flags = f
lim.Initial, err = leb128.ReadVarUint32(r)
if err != nil {
return err
}
if lim.Flags&0x1 != 0 {
m, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
lim.Maximum = m
}
return nil
}
func (lim *ResizableLimits) MarshalWASM(w io.Writer) error {
if _, err := leb128.WriteVarUint32(w, uint32(lim.Flags)); err != nil {
return err
}
if _, err := leb128.WriteVarUint32(w, uint32(lim.Initial)); err != nil {
return err
}
if lim.Flags&0x1 != 0 {
if _, err := leb128.WriteVarUint32(w, uint32(lim.Maximum)); err != nil {
return err
}
}
return nil
}