forked from lestrrat-go/xslate
/
op.go
189 lines (161 loc) · 4.48 KB
/
op.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
package vm
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
)
// OpType is an integer identifying the type of op code
type OpType int
// Type returns the ... OpType. This seems redundunt, but having this method
// allows us to embed OpType in Op and have the ability to call Typ()
// without having to re-declare it
func (o OpType) Type() OpType {
return o
}
// String returns the textual representation of an OpType
func (o OpType) String() string {
return opnames[o]
}
// OpHandler describes an op's actual code
type OpHandler func(*State)
// Op represents a single op. It has an OpType, OpHandler, and an optional
// parameter to be used
type Op struct {
OpType
OpHandler
uArg interface{}
}
// NewOp creates a new Op
func NewOp(o OpType, args ...interface{}) *Op {
h := optypeToHandler(o)
var arg interface{}
if len(args) > 0 {
arg = args[0]
} else {
arg = nil
}
return &Op{o, h, arg}
}
// MarshalBinary is used to serialize an Op into a binary form. This
// is used to cache the ByteCode
func (o Op) MarshalBinary() ([]byte, error) {
buf := &bytes.Buffer{}
// Write the code/opcode
if err := binary.Write(buf, binary.LittleEndian, int64(o.OpType)); err != nil {
return nil, fmt.Errorf("error: Op.MarshalBinary failed: %s", err)
}
// If this has args, we need to encode the args
tArg := reflect.TypeOf(o.uArg)
hasArg := tArg != nil
if hasArg {
binary.Write(buf, binary.LittleEndian, int64(1))
} else {
binary.Write(buf, binary.LittleEndian, int64(0))
}
if hasArg {
switch tArg.Kind() {
case reflect.Int:
binary.Write(buf, binary.LittleEndian, int64(2))
binary.Write(buf, binary.LittleEndian, int64(o.uArg.(int)))
case reflect.Int64:
binary.Write(buf, binary.LittleEndian, int64(2))
binary.Write(buf, binary.LittleEndian, int64(o.uArg.(int64)))
case reflect.Slice:
if tArg.Elem().Kind() != reflect.Uint8 {
panic("Slice of what?")
}
binary.Write(buf, binary.LittleEndian, int64(5))
binary.Write(buf, binary.LittleEndian, int64(len(o.uArg.([]byte))))
for _, v := range o.uArg.([]byte) {
binary.Write(buf, binary.LittleEndian, v)
}
case reflect.String:
binary.Write(buf, binary.LittleEndian, int64(6))
binary.Write(buf, binary.LittleEndian, int64(len(o.uArg.(string))))
for _, v := range []byte(o.uArg.(string)) {
binary.Write(buf, binary.LittleEndian, v)
}
default:
panic("Unknown type " + tArg.String())
}
}
return buf.Bytes(), nil
}
// UnmarshalBinary is used to deserialize an Op from binary form.
func (o *Op) UnmarshalBinary(data []byte) error {
buf := bytes.NewReader(data)
var t int64
if err := binary.Read(buf, binary.LittleEndian, &t); err != nil {
return fmt.Errorf("error: Op.UnmarshalBinary optype check failed: %s", err)
}
o.OpType = OpType(t)
o.OpHandler = optypeToHandler(o.OpType)
var hasArg int64
if err := binary.Read(buf, binary.LittleEndian, &hasArg); err != nil {
return fmt.Errorf("error: Op.UnmarshalBinary hasArg check failed: %s", err)
}
if hasArg == 0 {
// No args
return nil
}
var tArg int64
if err := binary.Read(buf, binary.LittleEndian, &tArg); err != nil {
return fmt.Errorf("error: Op.UnmarshalBinary arg type check failed: %s", err)
}
switch tArg {
case 2:
var i int64
if err := binary.Read(buf, binary.LittleEndian, &i); err != nil {
return err
}
o.uArg = i
case 5:
var l int64
if err := binary.Read(buf, binary.LittleEndian, &l); err != nil {
return err
}
b := make([]byte, l)
for i := int64(0); i < l; i++ {
if err := binary.Read(buf, binary.LittleEndian, &b[i]); err != nil {
return err
}
}
o.uArg = b
default:
panic(fmt.Sprintf("Unknown tArg: %d", tArg))
}
return nil
}
// Call executes the Op code in the context of given vm.State
func (o *Op) Call(st *State) {
o.OpHandler(st)
}
// SetArg sets the argument to this Op
func (o *Op) SetArg(v interface{}) {
o.uArg = v
}
// Arg returns the Op code's argument
func (o *Op) Arg() interface{} {
return o.uArg
}
// ArgInt returns the integer representation of the argument
func (o *Op) ArgInt() int {
v := interfaceToNumeric(o.uArg)
return int(v.Int())
}
// ArgString returns the string representatin of the argument
func (o *Op) ArgString() string {
// In most cases we do this because it's a sring
if v, ok := o.uArg.(string); ok {
return v
}
return interfaceToString(o.uArg)
}
func (o *Op) String() string {
// TODO: also print out register id's and stuff
if o.uArg != nil {
return fmt.Sprintf("Op[%s] (%q)", o.Type(), o.ArgString())
}
return fmt.Sprintf("Op[%s]", o.Type())
}