forked from tetratelabs/wazero
/
type.go
150 lines (124 loc) · 2.98 KB
/
type.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
package wasm
import (
"errors"
"fmt"
"io"
"github.com/aghosn/gasm/wasm/leb128"
)
var (
ErrInvalidByte = errors.New("invalid byte")
)
type FunctionType struct {
InputTypes, ReturnTypes []ValueType
}
func readFunctionType(r io.Reader) (*FunctionType, error) {
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("read leading byte: %w", err)
}
if b[0] != 0x60 {
return nil, fmt.Errorf("%w: %#x != 0x60", ErrInvalidByte, b[0])
}
s, _, err := leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("get the size of input value types: %w", err)
}
ip, err := readValueTypes(r, s)
if err != nil {
return nil, fmt.Errorf("read value types of inputs: %w", err)
}
s, _, err = leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("get the size of output value types: %w", err)
}
op, err := readValueTypes(r, s)
if err != nil {
return nil, fmt.Errorf("read value types of outputs: %w", err)
}
return &FunctionType{
InputTypes: ip,
ReturnTypes: op,
}, nil
}
type LimitsType struct {
Min uint32
Max *uint32
}
func readLimitsType(r io.Reader) (*LimitsType, error) {
b := make([]byte, 1)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, fmt.Errorf("read leading byte: %w", err)
}
ret := &LimitsType{}
switch b[0] {
case 0x00:
ret.Min, _, err = leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("read min of limit: %w", err)
}
case 0x01:
ret.Min, _, err = leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("read min of limit: %w", err)
}
m, _, err := leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("read min of limit: %w", err)
}
ret.Max = &m
default:
return nil, fmt.Errorf("%w for limits: %#x != 0x00 or 0x01", ErrInvalidByte, b[0])
}
return ret, nil
}
type TableType struct {
Elem byte
Limit *LimitsType
}
func readTableType(r io.Reader) (*TableType, error) {
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("read leading byte: %w", err)
}
if b[0] != 0x70 {
return nil, fmt.Errorf("%w: invalid element type %#x != %#x", ErrInvalidByte, b[0], 0x70)
}
lm, err := readLimitsType(r)
if err != nil {
return nil, fmt.Errorf("read limits: %w", err)
}
return &TableType{
Elem: 0x70,
Limit: lm,
}, nil
}
type MemoryType = LimitsType
func readMemoryType(r io.Reader) (*MemoryType, error) {
return readLimitsType(r)
}
type GlobalType struct {
Value ValueType
Mutable bool
}
func readGlobalType(r io.Reader) (*GlobalType, error) {
vt, err := readValueTypes(r, 1)
if err != nil {
return nil, fmt.Errorf("read value type: %w", err)
}
ret := &GlobalType{
Value: vt[0],
}
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("read mutablity: %w", err)
}
switch mut := b[0]; mut {
case 0x00:
case 0x01:
ret.Mutable = true
default:
return nil, fmt.Errorf("%w for mutability: %#x != 0x00 or 0x01", ErrInvalidByte, mut)
}
return ret, nil
}