forked from mattn/anko
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vmLetExpr.go
395 lines (342 loc) · 10 KB
/
vmLetExpr.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package vm
import (
"fmt"
"reflect"
"github.com/dgrr/pako/ast"
"github.com/dgrr/pako/env"
)
func (runInfo *runInfoStruct) invokeLetExpr() {
switch expr := runInfo.expr.(type) {
// IdentExpr
case *ast.IdentExpr:
var v reflect.Value
runInfo.allocMemUsage(runInfo.rv)
v, runInfo.err = runInfo.env.SetValueEvict(expr.Lit, runInfo.rv)
if runInfo.err != nil {
runInfo.err = runInfo.env.DefineValue(expr.Lit, runInfo.rv)
} else {
runInfo.deallocMemUsage(v)
}
// MemberExpr
case *ast.MemberExpr:
value := runInfo.rv
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
if env, ok := runInfo.rv.Interface().(*env.Env); ok {
runInfo.err = env.SetValue(expr.Name, value)
if runInfo.err != nil {
runInfo.err = newError(expr, runInfo.err)
runInfo.rv = nilValue
}
return
}
if v, ok := runInfo.rv.Interface().(*vmStruct); ok {
runInfo.rv = v.v
}
if runInfo.rv.Kind() == reflect.Ptr {
runInfo.rv = runInfo.rv.Elem()
}
switch runInfo.rv.Kind() {
// Struct
case reflect.Struct:
field, found := runInfo.rv.Type().FieldByName(expr.Name)
if !found {
runInfo.err = newStringError(expr, "no member named '"+expr.Name+"' for struct")
runInfo.rv = nilValue
return
}
runInfo.rv = runInfo.rv.FieldByIndex(field.Index)
// From reflect CanSet:
// A Value can be changed only if it is addressable and was not obtained by the use of unexported struct fields.
// Often a struct has to be passed as a pointer to be set
if !runInfo.rv.CanSet() {
runInfo.err = newStringError(expr, "struct member '"+expr.Name+"' cannot be assigned")
runInfo.rv = nilValue
return
}
value, runInfo.err = convertReflectValueToType(value, runInfo.rv.Type())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+runInfo.rv.Type().String()+" for struct")
runInfo.rv = nilValue
return
}
runInfo.rv.Set(value)
return
// Map
case reflect.Map:
value, runInfo.err = convertReflectValueToType(value, runInfo.rv.Type().Elem())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+runInfo.rv.Type().Elem().String()+" for map")
runInfo.rv = nilValue
return
}
if runInfo.rv.IsNil() {
// make new map
item := reflect.MakeMap(runInfo.rv.Type())
item.SetMapIndex(reflect.ValueOf(expr.Name), value)
// assign new map
runInfo.rv = item
runInfo.expr = expr.Expr
runInfo.invokeLetExpr()
runInfo.rv = item.MapIndex(reflect.ValueOf(expr.Name))
return
}
runInfo.rv.SetMapIndex(reflect.ValueOf(expr.Name), value)
default:
runInfo.err = newStringError(expr, "type "+runInfo.rv.Kind().String()+" does not support member operation")
runInfo.rv = nilValue
}
// ItemExpr
case *ast.ItemExpr:
value := runInfo.rv
runInfo.expr = expr.Item
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
item := runInfo.rv
runInfo.expr = expr.Index
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if item.Kind() == reflect.Interface && !item.IsNil() {
item = item.Elem()
}
switch item.Kind() {
// Slice && Array
case reflect.Slice, reflect.Array:
var index int
index, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
if index == item.Len() {
// try to do automatic append
value, runInfo.err = convertReflectValueToType(value, item.Type().Elem())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+item.Type().Elem().String()+" for slice index")
runInfo.rv = nilValue
return
}
item = reflect.Append(item, value)
runInfo.rv = item
runInfo.expr = expr.Item
runInfo.invokeLetExpr()
runInfo.rv = item.Index(index)
return
}
if index < 0 || index >= item.Len() {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
item = item.Index(index)
if !item.CanSet() {
runInfo.err = newStringError(expr, "index cannot be assigned")
runInfo.rv = nilValue
return
}
value, runInfo.err = convertReflectValueToType(value, item.Type())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+item.Type().String()+" for slice index")
runInfo.rv = nilValue
return
}
item.Set(value)
runInfo.rv = item
// Map
case reflect.Map:
runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, item.Type().Key())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index type "+runInfo.rv.Type().String()+" cannot be used for map index type "+item.Type().Key().String())
runInfo.rv = nilValue
return
}
value, runInfo.err = convertReflectValueToType(value, item.Type().Elem())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+item.Type().Elem().String()+" for map")
runInfo.rv = nilValue
return
}
if item.IsNil() {
// make new map
item = reflect.MakeMap(item.Type())
item.SetMapIndex(runInfo.rv, value)
mapIndex := runInfo.rv
// assign new map
runInfo.rv = item
runInfo.expr = expr.Item
runInfo.invokeLetExpr()
runInfo.rv = item.MapIndex(mapIndex)
return
}
item.SetMapIndex(runInfo.rv, value)
// String
case reflect.String:
var index int
index, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
value, runInfo.err = convertReflectValueToType(value, item.Type())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "type "+value.Type().String()+" cannot be assigned to type "+item.Type().String())
runInfo.rv = nilValue
return
}
if index == item.Len() {
// automatic append
if item.CanSet() {
item.SetString(item.String() + value.String())
return
}
runInfo.rv = reflect.ValueOf(item.String() + value.String())
runInfo.expr = expr.Item
runInfo.invokeLetExpr()
return
}
if index < 0 || index >= item.Len() {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
if item.CanSet() {
item.SetString(item.Slice(0, index).String() + value.String() + item.Slice(index+1, item.Len()).String())
runInfo.rv = item
return
}
runInfo.rv = reflect.ValueOf(item.Slice(0, index).String() + value.String() + item.Slice(index+1, item.Len()).String())
runInfo.expr = expr.Item
runInfo.invokeLetExpr()
default:
runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support index operation")
runInfo.rv = nilValue
}
// SliceExpr
case *ast.SliceExpr:
value := runInfo.rv
runInfo.expr = expr.Item
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
item := runInfo.rv
if item.Kind() == reflect.Interface && !item.IsNil() {
item = item.Elem()
}
switch item.Kind() {
// Slice && Array
case reflect.Slice, reflect.Array:
var beginIndex int
endIndex := item.Len()
if expr.Begin != nil {
runInfo.expr = expr.Begin
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
beginIndex, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
// (0 <= low) <= high <= len(a)
if beginIndex < 0 {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
}
if expr.End != nil {
runInfo.expr = expr.End
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
endIndex, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
// 0 <= low <= (high <= len(a))
if endIndex > item.Len() {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
}
// 0 <= (low <= high) <= len(a)
if beginIndex > endIndex {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
sliceCap := item.Cap()
if expr.Cap != nil {
runInfo.expr = expr.Cap
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
sliceCap, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cap must be a number")
runInfo.rv = nilValue
return
}
// 0 <= low <= (high <= max <= cap(a))
if sliceCap < endIndex || sliceCap > item.Cap() {
runInfo.err = newStringError(expr, "cap out of range")
runInfo.rv = nilValue
return
}
}
item = item.Slice3(beginIndex, endIndex, sliceCap)
if !item.CanSet() {
runInfo.err = newStringError(expr, "slice cannot be assigned")
runInfo.rv = nilValue
return
}
item.Set(value)
// String
case reflect.String:
runInfo.err = newStringError(expr, "type string does not support slice operation for assignment")
runInfo.rv = nilValue
default:
runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support slice operation")
runInfo.rv = nilValue
}
// DerefExpr
case *ast.DerefExpr:
value := runInfo.rv
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
runInfo.rv.Elem().Set(value)
runInfo.rv = value
default:
runInfo.err = newStringError(expr, "invalid operation")
runInfo.rv = nilValue
}
if runInfo.err == nil &&
runInfo.options != nil &&
runInfo.options.MaxMemoryUsage > 0 &&
runInfo.options.MaxMemoryUsage < runInfo.memUsage {
runInfo.err = fmt.Errorf("Max memory exceeded")
}
}