forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
const_expr.go
119 lines (107 loc) · 2.65 KB
/
const_expr.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
package wasm
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"github.com/aghosn/gasm/wasm/leb128"
)
type ConstantExpression struct {
optCode OptCode
data []byte
}
func (m *Module) executeConstExpression(expr *ConstantExpression) (v interface{}, err error) {
r := bytes.NewBuffer(expr.data)
switch expr.optCode {
case OptCodeI32Const:
v, _, err = leb128.DecodeInt32(r)
if err != nil {
return nil, fmt.Errorf("read int32: %w", err)
}
case OptCodeI64Const:
v, _, err = leb128.DecodeInt64(r)
if err != nil {
return nil, fmt.Errorf("read int64: %w", err)
}
case OptCodeF32Const:
v, err = readFloat32(r)
if err != nil {
return nil, fmt.Errorf("read f34: %w", err)
}
case OptCodeF64Const:
v, err = readFloat64(r)
if err != nil {
return nil, fmt.Errorf("read f64: %w", err)
}
case OptCodeGlobalGet:
id, _, err := leb128.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("read index of global: %w", err)
}
if uint32(len(m.IndexSpace.Globals)) <= id {
return nil, fmt.Errorf("global index out of range")
}
v = m.IndexSpace.Globals[id].Val
default:
return nil, fmt.Errorf("invalid opt code: %#x", expr.optCode)
}
return v, nil
}
func readConstantExpression(r io.Reader) (*ConstantExpression, error) {
b := make([]byte, 1)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, fmt.Errorf("read optcode: %w", err)
}
buf := new(bytes.Buffer)
teeR := io.TeeReader(r, buf)
optCode := OptCode(b[0])
switch optCode {
case OptCodeI32Const:
_, _, err = leb128.DecodeInt32(teeR)
case OptCodeI64Const:
_, _, err = leb128.DecodeInt64(teeR)
case OptCodeF32Const:
_, err = readFloat32(teeR)
case OptCodeF64Const:
_, err = readFloat64(teeR)
case OptCodeGlobalGet:
_, _, err = leb128.DecodeUint32(teeR)
default:
return nil, fmt.Errorf("%w for opt code: %#x", ErrInvalidByte, b[0])
}
if err != nil {
return nil, fmt.Errorf("read value: %w", err)
}
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("look for end optcode: %w", err)
}
if b[0] != byte(OptCodeEnd) {
return nil, fmt.Errorf("constant expression has not terminated")
}
return &ConstantExpression{
optCode: optCode,
data: buf.Bytes(),
}, nil
}
// IEEE 754
func readFloat32(r io.Reader) (float32, error) {
buf := make([]byte, 4)
_, err := io.ReadFull(r, buf)
if err != nil {
return 0, err
}
raw := binary.LittleEndian.Uint32(buf)
return math.Float32frombits(raw), nil
}
// IEEE 754
func readFloat64(r io.Reader) (float64, error) {
buf := make([]byte, 8)
_, err := io.ReadFull(r, buf)
if err != nil {
return 0, err
}
raw := binary.LittleEndian.Uint64(buf)
return math.Float64frombits(raw), nil
}