-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalue.go
634 lines (561 loc) · 11.5 KB
/
value.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
Copyright 2016-2017 by Milo Christiansen
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use of
this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product, an
acknowledgment in the product documentation would be appreciated but is not
required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package lua
import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
"strings"
)
type TypeID int
type STypeID int
const (
TypeUnknown TypeID = iota
TypeNil
TypeNumber
TypeString
TypeBoolean
TypeTable
TypeFunction
TypeUserData
nType int = iota
)
const (
STypeUnknown STypeID = iota
STypeInteger
STypeFloat
)
var typeNames = []string{"unknown", "nil", "number", "string", "boolean", "table", "function", "userdata"}
var stypeNames = []string{"unknown", "integer", "float"}
func (typ TypeID) String() string {
return typeNames[typ]
}
func (typ STypeID) String() string {
return stypeNames[typ]
}
type value interface{}
type userdata struct {
data interface{}
meta *table
}
func typeOf(v value) TypeID {
switch v.(type) {
case nil:
return TypeNil
case float64, int64:
return TypeNumber
case string:
return TypeString
case bool:
return TypeBoolean
case *table:
return TypeTable
case *function:
return TypeFunction
case *userdata:
return TypeUserData
default:
return TypeUnknown
}
}
func stypeOf(v value) STypeID {
switch v.(type) {
case float64:
return STypeFloat
case int64:
return STypeInteger
default:
return STypeUnknown
}
}
func toBoolean(v value) bool {
switch x := v.(type) {
case bool:
return x
case nil:
return false
default:
return true
}
}
func toString(v value) string {
switch x := v.(type) {
case string:
return x
case float64:
return strconv.FormatFloat(x, 'g', -1, 64)
case int64:
return strconv.FormatInt(x, 10)
case bool:
return strconv.FormatBool(x)
case nil:
return "nil"
default:
return typeOf(x).String() + ": " + strconv.FormatUint(uint64(reflect.ValueOf(x).Pointer()), 16)
}
}
func tryFloat(v value) (float64, error) {
switch x := v.(type) {
case float64:
return x, nil
case int64:
return float64(x), nil
case string:
return strconv.ParseFloat(strings.TrimSpace(x), 64)
default:
return 0, errors.New("can't convert to float: " + toString(x))
}
}
func toFloat(v value) float64 {
if x, err := tryFloat(v); err == nil {
return x
} else {
panic(err)
}
}
func tryInteger(v value) (int64, error) {
switch x := v.(type) {
case int64:
return x, nil
case float64:
y := int64(x)
if float64(y) == x {
return y, nil
} else {
return 0, errors.New("can't convert to integer: " + toString(x))
}
case string:
x = strings.TrimSpace(x)
if y, err := strconv.ParseInt(x, 0, 64); err == nil {
return y, nil
} else if y, err := strconv.ParseFloat(x, 64); err == nil {
z := int64(y)
if float64(z) == y {
return z, nil
} else {
return 0, errors.New("can't convert to integer: " + x)
}
} else {
return 0, err
}
default:
return 0, errors.New("can't convert to integer: " + toString(x))
}
}
func toInteger(v value) int64 {
if x, err := tryInteger(v); err == nil {
return x
} else {
panic(err)
}
}
func (l *State) getTable(t, k value) value {
tbl, ok := t.(*table)
if ok {
if v := tbl.Get(k); v != nil {
return v
}
}
meth := l.getMetaField(t, "__index")
if meth != nil {
if tbl, ok := meth.(*table); ok {
return l.getTable(tbl, k)
}
f, ok := meth.(*function)
if !ok {
panic("Meta method __index is not a table or function.")
}
l.Push(f)
l.Push(t)
l.Push(k)
l.Call(2, 1)
rtn := l.stack.Get(-1)
l.Pop(1)
return rtn
}
if ok {
return tbl.Get(k)
}
panic("Value is not a table and has no __index meta method.")
panic("UNREACHABLE")
}
func (l *State) setTable(t, k, v value) {
tbl, ok := t.(*table)
if ok && tbl.Get(k) != nil {
tbl.Set(k, v)
return
}
meth := l.getMetaField(t, "__newindex")
if meth != nil {
if t, ok := meth.(*table); ok {
l.setTable(t, k, v)
return
}
f, ok := meth.(*function)
if !ok {
panic("Meta method __newindex is not a table or function.")
}
l.Push(f)
l.Push(t)
l.Push(k)
l.Push(v)
l.Call(3, 0)
return
}
if ok {
tbl.Set(k, v)
return
}
panic("Value is not a table and has no __newindex meta method.")
panic("UNREACHABLE")
}
var mathMeta = [...]string{
"__add",
"__sub",
"__mul",
"__mod",
"__pow",
"__div",
"__idiv",
"__band",
"__bor",
"__bxor",
"__shl",
"__shr",
"__unm",
"__bnot",
}
func (l *State) tryMathMeta(op opCode, a, b value) value {
if op < OpAdd || op > OpBinNot {
panic("Operator passed to tryMathMeta out of range.")
}
name := mathMeta[op-OpAdd]
meta := l.getMetaField(a, name)
if meta == nil {
meta = l.getMetaField(b, name)
if meta == nil {
panic("Neither operand has a " + name + " meta method.")
}
}
f, ok := meta.(*function)
if !ok {
panic("Meta method " + name + " is not a function.")
}
l.Push(f)
l.Push(a)
l.Push(b)
l.Call(2, 1)
rtn := l.stack.Get(-1)
l.Pop(1)
return rtn
}
func (l *State) arith(op opCode, a, b value) value {
switch op {
case OpAdd:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia + ib
}
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return fa + fb
}
return l.tryMathMeta(op, a, b)
case OpSub:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia - ib
}
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return fa - fb
}
return l.tryMathMeta(op, a, b)
case OpMul:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia * ib
}
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return fa * fb
}
return l.tryMathMeta(op, a, b)
case OpMod:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia % ib
}
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return math.Mod(fa, fb)
}
return l.tryMathMeta(op, a, b)
case OpPow:
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return math.Pow(fa, fb)
}
return l.tryMathMeta(op, a, b)
case OpDiv:
fa, erra := tryFloat(a)
fb, errb := tryFloat(b)
if erra == nil && errb == nil {
return fa / fb
}
return l.tryMathMeta(op, a, b)
case OpIDiv:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
return ia / ib
}
return l.tryMathMeta(op, a, b)
case OpBinAND:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
return ia & ib
}
return l.tryMathMeta(op, a, b)
case OpBinOR:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
return ia | ib
}
return l.tryMathMeta(op, a, b)
case OpBinXOR:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
return ia ^ ib
}
return l.tryMathMeta(op, a, b)
case OpBinShiftL:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
if ib < 0 {
return int64(uint64(ia) >> uint64(-ib))
} else {
return int64(uint64(ia) << uint64(ib))
}
}
return l.tryMathMeta(op, a, b)
case OpBinShiftR:
ia, erra := tryInteger(a)
ib, errb := tryInteger(b)
if erra == nil && errb == nil {
if ib < 0 {
return int64(uint64(ia) << uint64(-ib))
} else {
return int64(uint64(ia) >> uint64(ib))
}
}
return l.tryMathMeta(op, a, b)
case OpUMinus:
ia, oka := a.(int64)
if oka {
return -ia
}
fa, erra := tryFloat(a)
if erra == nil {
return -fa
}
return l.tryMathMeta(op, a, b)
case OpBinNot:
ia, erra := tryInteger(a)
if erra == nil {
return ^ia
}
return l.tryMathMeta(op, a, b)
default:
panic("Invalid opCode passed to arith")
panic("UNREACHABLE")
}
}
var cmpMeta = [...]string{
"__eq",
"__lt",
"__le", // if this does not exist then try !lt(b, a)
}
func (l *State) tryCmpMeta(op opCode, a, b value) bool {
if op < OpEqual || op > OpLessOrEqual {
panic("Operator passed to tryCmpMeta out of range.")
}
name := cmpMeta[op-OpEqual]
var meta value
tryLEHack := false
try:
meta = l.getMetaField(a, name)
if meta == nil {
meta = l.getMetaField(b, name)
if meta == nil {
if name == "__le" {
tryLEHack = true
name = "__lt"
goto try
}
if name == "__eq" {
return a == b // Fall back to raw equality.
}
return false
}
}
l.Push(meta)
if tryLEHack {
l.Push(b)
l.Push(a)
} else {
l.Push(a)
l.Push(b)
}
l.Call(2, 1)
rtn := toBoolean(l.stack.Get(-1))
l.Pop(1)
if tryLEHack {
return !rtn
}
return rtn
}
func (l *State) compare(op opCode, a, b value, raw bool) bool {
tm := true
t := typeOf(a)
if t != typeOf(b) {
tm = false
}
switch op {
case OpEqual:
if tm {
switch t {
case TypeNil:
return true // Obviously.
case TypeNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia == ib
}
return toFloat(a) == toFloat(b)
case TypeString:
return a.(string) == b.(string)
case TypeBoolean:
return a.(bool) == b.(bool)
}
}
if raw {
return a == b
}
return l.tryCmpMeta(op, a, b)
case OpLessThan:
if tm {
switch t {
case TypeNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia < ib
}
return toFloat(a) < toFloat(b)
case TypeString:
return a.(string) < b.(string) // Fix me, should be locale sensitive, not lexical
}
}
if raw {
return false
}
return l.tryCmpMeta(op, a, b)
case OpLessOrEqual:
if tm {
switch t {
case TypeNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia <= ib
}
return toFloat(a) <= toFloat(b)
case TypeString:
return a.(string) <= b.(string) // Fix me, should be locale sensitive, not lexical
}
}
if raw {
return false
}
return l.tryCmpMeta(op, a, b)
default:
panic("Invalid comparison operator.")
panic("UNREACHABLE")
}
}
func toStringConcat(v value) string {
switch v2 := v.(type) {
case nil:
panic("Attempt to concatenate a nil value.")
panic("UNREACHABLE")
case float64:
return fmt.Sprintf("%g", v2)
case int64:
return fmt.Sprintf("%d", v2)
case string:
return v2
case bool:
panic("Attempt to concatenate a bool value.")
panic("UNREACHABLE")
case *table:
panic("Attempt to concatenate a table value.")
panic("UNREACHABLE")
case *function:
panic("Attempt to concatenate a function value.")
panic("UNREACHABLE")
case *userdata:
panic("Attempt to concatenate a userdata value.")
panic("UNREACHABLE")
default:
panic("Invalid type passed to toStringConcat.")
panic("UNREACHABLE")
}
}
func (l *State) getMetaTable(v value) *table {
switch x := v.(type) {
case *table:
return x.meta
case *userdata:
return x.meta
default:
return l.meta[typeOf(v)]
}
}
func (l *State) getMetaField(v value, name string) value {
m := l.getMetaTable(v)
if m == nil {
return nil
}
return m.Get(name)
}