-
Notifications
You must be signed in to change notification settings - Fork 39
/
types.go
executable file
·296 lines (240 loc) · 6.51 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
// Copyright 2017~2022 The Bottos Authors
// This file is part of the Bottos Chain library.
// Created by Rocket Core Team of Bottos.
// This program is free software: you can distribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Bottos. If not, see <http://www.gnu.org/licenses/>.
// 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/bottos-project/bottos/vm/wasm/wasm/leb128"
)
// ValueType represents the type of a valid value in Wasm
type ValueType int8
const (
// ValueTypeI32 int32 type
ValueTypeI32 ValueType = -0x01
// ValueTypeI64 int64 type
ValueTypeI64 ValueType = -0x02
// ValueTypeF32 float32 type
ValueTypeF32 ValueType = -0x03
// ValueTypeF64 float64 type
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 readValueType(r io.Reader) (ValueType, error) {
v, err := leb128.ReadVarint32(r)
return ValueType(v), err
}
// BlockType represents the signature of a structured block
type BlockType ValueType // varint7
// BlockTypeEmpty block type empty
const BlockTypeEmpty BlockType = -0x40
func readBlockType(r io.Reader) (BlockType, error) {
b, err := leb128.ReadVarint32(r)
return BlockType(b), err
}
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 readElemType(r io.Reader) (ElemType, error) {
b, err := leb128.ReadVarint32(r)
return ElemType(b), 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)
}
// InvalidTypeConstructorError invalid type contructor
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 readFunction(r io.Reader) (FunctionSig, error) {
f := FunctionSig{}
form, err := leb128.ReadVarint32(r)
if err != nil {
return f, err
}
f.Form = int8(form)
paramCount, err := leb128.ReadVarUint32(r)
if err != nil {
return f, err
}
f.ParamTypes = make([]ValueType, paramCount)
for i := range f.ParamTypes {
f.ParamTypes[i], err = readValueType(r)
if err != nil {
return f, err
}
}
returnCount, err := leb128.ReadVarUint32(r)
if err != nil {
return f, err
}
f.ReturnTypes = make([]ValueType, returnCount)
for i := range f.ReturnTypes {
vt, err := readValueType(r)
if err != nil {
return f, err
}
f.ReturnTypes[i] = vt
}
return f, 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 readGlobalVar(r io.Reader) (*GlobalVar, error) {
g := &GlobalVar{}
var err error
g.Type, err = readValueType(r)
if err != nil {
return nil, err
}
m, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
g.Mutable = m == 1
return g, nil
}
// Table describes a table in a Wasm module.
type Table struct {
// The type of elements
ElementType ElemType
Limits ResizableLimits
}
func readTable(r io.Reader) (*Table, error) {
t := Table{}
var err error
t.ElementType, err = readElemType(r)
if err != nil {
return nil, err
}
lims, err := readResizableLimits(r)
if err != nil {
return nil, err
}
t.Limits = *lims
return &t, err
}
// Memory defind memory type
type Memory struct {
Limits ResizableLimits
}
func readMemory(r io.Reader) (*Memory, error) {
lim, err := readResizableLimits(r)
if err != nil {
return nil, err
}
return &Memory{*lim}, nil
}
// External describes the kind of the entry being imported or exported.
type External uint8
const (
// ExternalFunction external function type
ExternalFunction External = 0
// ExternalTable external table type
ExternalTable External = 1
// ExternalMemory external memory type
ExternalMemory External = 2
// ExternalGlobal external global type
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 readExternal(r io.Reader) (External, error) {
bytes, err := readBytes(r, 1)
return External(bytes[0]), 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 readResizableLimits(r io.Reader) (*ResizableLimits, error) {
lim := &ResizableLimits{
Maximum: 0,
}
f, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
lim.Flags = f
lim.Initial, err = leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
if lim.Flags&0x1 != 0 {
m, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
lim.Maximum = m
}
return lim, nil
}