-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvalue.go
727 lines (639 loc) · 13.4 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
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 "math"
import "fmt"
import "github.com/milochristiansen/lua/luautil"
type TypeID int
type STypeID int
const (
TypNil TypeID = iota
TypNumber // Both int and float
TypString
TypBool
TypTable
TypFunction
TypUserData
typeCount int = iota
)
const (
STypNone STypeID = iota
STypInt
STypFloat
)
var typeNames = [...]string{"nil", "number", "string", "boolean", "table", "function", "userdata"}
func (typ TypeID) String() string {
return typeNames[typ]
}
var stypeNames = [...]string{"none", "int", "float"}
func (typ STypeID) String() string {
return stypeNames[typ]
}
type value interface{}
// See table.go for Table
// See function.go for Function
type userData struct {
meta *table
data interface{}
}
// Utility functions
func typeOf(v value) TypeID {
switch v.(type) {
case nil:
return TypNil
case float64:
return TypNumber
case int64:
return TypNumber
case string:
return TypString
case bool:
return TypBool
case *table:
return TypTable
case *function:
return TypFunction
case *userData:
return TypUserData
default:
return TypUserData // Should be an error?
}
}
func sTypeOf(v value) STypeID {
switch v.(type) {
case nil:
return STypNone
case float64:
return STypFloat
case int64:
return STypInt
case string:
return STypNone
case bool:
return STypNone
case *table:
return STypNone
case *function:
return STypNone
case *userData:
return STypNone
default:
return STypNone // Should be an error?
}
}
func (l *State) getMetaTable(v value) *table {
switch v2 := v.(type) {
case nil:
return l.metaTbls[TypNil]
case float64:
return l.metaTbls[TypNumber]
case int64:
return l.metaTbls[TypNumber]
case string:
return l.metaTbls[TypString]
case bool:
return l.metaTbls[TypBool]
case *table:
return v2.meta
case *function:
return l.metaTbls[TypFunction]
case *userData:
return v2.meta
default:
luautil.Raise("Invalid type passed to getMetaTable.", luautil.ErrTypMajorInternal)
panic("UNREACHABLE")
}
}
func (l *State) hasMetaMethod(v value, name string) value {
tbl := l.getMetaTable(v)
if tbl == nil {
return nil
}
return tbl.GetRaw(name)
}
func toStringConcat(v value) string {
switch v2 := v.(type) {
case nil:
luautil.Raise("Attempt to concatenate a nil value.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
case float64:
return fmt.Sprintf("%g", v2)
case int64:
return fmt.Sprintf("%d", v2)
case string:
return v2
case bool:
luautil.Raise("Attempt to concatenate a bool value.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
case *table:
luautil.Raise("Attempt to concatenate a table value.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
case *function:
luautil.Raise("Attempt to concatenate a function value.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
case *userData:
luautil.Raise("Attempt to concatenate a userdata value.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
default:
luautil.Raise("Invalid type passed to toStringConcat.", luautil.ErrTypMajorInternal)
panic("UNREACHABLE")
}
}
func toString(v value) string {
switch v2 := v.(type) {
case nil:
return "nil"
case float64:
return fmt.Sprintf("%g", v2)
case int64:
return fmt.Sprintf("%d", v2)
case string:
return v2
case bool:
if v2 {
return "true"
}
return "false"
case *table:
return fmt.Sprintf("table %p", v2)
case *function:
return fmt.Sprintf("function %p", v2)
case *userData:
return fmt.Sprintf("userdata %p", v2)
default:
return fmt.Sprintf("unknown %p", v2)
}
}
func toBool(v value) bool {
switch v2 := v.(type) {
case nil:
return false
case bool:
return v2
default:
return true
}
}
func toFloat(v value) float64 {
msg := ""
switch v2 := v.(type) {
case string:
valid, _, _, f := luautil.ConvNumber(v2, false, true)
if valid {
return f
}
msg = ": String not numeric"
case int64:
return float64(v2)
case float64:
return v2
default:
msg = ": Not string or number"
}
luautil.Raise("Invalid conversion to float"+msg, luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
}
func tryFloat(v value) (float64, bool) {
switch v2 := v.(type) {
case string:
valid, _, _, f := luautil.ConvNumber(v2, false, true)
if valid {
return f, true
}
return 0, false
case int64:
return float64(v2), true
case float64:
return v2, true
default:
return 0, false
}
}
// Used in the compare function for int->float comparisons.
func forceFloat(v value) float64 {
switch v2 := v.(type) {
case int64:
return float64(v2)
case float64:
return v2
}
luautil.Raise("Invalid conversion to integer: Not number", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
}
func toInt(v value) int64 {
msg := ""
switch v2 := v.(type) {
case string:
valid, _, i, _ := luautil.ConvNumber(v2, true, false)
if valid {
return i
}
msg = ": String not numeric"
case int64:
return v2
case float64:
if i := int64(v2); float64(i) == v2 {
return i
}
msg = ": Non-integral float"
default:
msg = ": Not string or number"
}
luautil.Raise("Invalid conversion to integer"+msg, luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
}
func tryInt(v value) (int64, bool) {
switch v2 := v.(type) {
case string:
valid, _, i, _ := luautil.ConvNumber(v2, true, false)
if valid {
return i, true
}
return 0, false
case int64:
return v2, true
case float64:
if i := int64(v2); float64(i) == v2 {
return i, true
}
return 0, false
default:
return 0, false
}
}
func (l *State) getTable(t, k value) value {
tbl, ok := t.(*table)
if ok && tbl.Exists(k) {
return tbl.GetRaw(k)
}
meth := l.hasMetaMethod(t, "__index")
if meth != nil {
if tbl, ok := meth.(*table); ok {
return l.getTable(tbl, k)
}
f, ok := meth.(*function)
if !ok {
luautil.Raise("Meta method __index is not a table or function.", luautil.ErrTypGenRuntime)
}
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.GetRaw(k)
}
luautil.Raise("Value is not a table and has no __index meta method.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
}
func (l *State) setTable(t, k, v value) {
tbl, ok := t.(*table)
if ok && tbl.Exists(k) {
tbl.SetRaw(k, v)
return
}
meth := l.hasMetaMethod(t, "__newindex")
if meth != nil {
if t, ok := meth.(*table); ok {
l.setTable(t, k, v)
return
}
f, ok := meth.(*function)
if !ok {
luautil.Raise("Meta method __newindex is not a table or function.", luautil.ErrTypGenRuntime)
}
l.Push(f)
l.Push(t)
l.Push(k)
l.Push(v)
l.Call(3, 0)
return
}
if ok {
tbl.SetRaw(k, v)
return
}
luautil.Raise("Value is not a table and has no __newindex meta method.", luautil.ErrTypGenRuntime)
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 {
luautil.Raise("Operator passed to tryMathMeta out of range.", luautil.ErrTypMajorInternal)
}
name := mathMeta[op-OpAdd]
meta := l.hasMetaMethod(a, name)
if meta == nil {
meta = l.hasMetaMethod(b, name)
if meta == nil {
luautil.Raise("Neither operand has a "+name+" meta method.", luautil.ErrTypGenRuntime)
}
}
f, ok := meta.(*function)
if !ok {
luautil.Raise("Meta method "+name+" is not a function.", luautil.ErrTypGenRuntime)
}
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, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
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, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
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, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
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, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
return math.Mod(fa, fb)
}
return l.tryMathMeta(op, a, b)
case OpPow:
fa, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
return math.Pow(fa, fb)
}
return l.tryMathMeta(op, a, b)
case OpDiv:
fa, oka := tryFloat(a)
fb, okb := tryFloat(b)
if oka && okb {
return fa / fb
}
return l.tryMathMeta(op, a, b)
case OpIDiv:
ia, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
return ia / ib
}
return l.tryMathMeta(op, a, b)
case OpBinAND:
ia, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
return ia & ib
}
return l.tryMathMeta(op, a, b)
case OpBinOR:
ia, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
return ia | ib
}
return l.tryMathMeta(op, a, b)
case OpBinXOR:
ia, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
return ia ^ ib
}
return l.tryMathMeta(op, a, b)
case OpBinShiftL:
ia, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
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, oka := tryInt(a)
ib, okb := tryInt(b)
if oka && okb {
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, oka := tryFloat(a)
if oka {
return -fa
}
return l.tryMathMeta(op, a, b)
case OpBinNot:
ia, oka := tryInt(a)
if oka {
return ^ia
}
return l.tryMathMeta(op, a, b)
default:
luautil.Raise("Invalid opCode passed to arith", luautil.ErrTypMajorInternal)
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 {
luautil.Raise("Operator passed to tryCmpMeta out of range.", luautil.ErrTypMajorInternal)
}
name := cmpMeta[op-OpEqual]
var meta value
tryLEHack := false
try:
meta = l.hasMetaMethod(a, name)
if meta == nil {
meta = l.hasMetaMethod(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 := toBool(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 TypNil:
return true // Obviously.
case TypNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia == ib
}
fa, oka := a.(float64)
fb, okb := b.(float64)
if oka && okb {
return fa == fb
}
return forceFloat(a) == forceFloat(b)
case TypString:
return a.(string) == b.(string)
case TypBool:
return a.(bool) == b.(bool)
}
}
if raw {
return a == b
}
return l.tryCmpMeta(op, a, b)
case OpLessThan:
if tm {
switch t {
case TypNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia < ib
}
fa, oka := a.(float64)
fb, okb := b.(float64)
if oka && okb {
return fa < fb
}
return forceFloat(a) < forceFloat(b)
case TypString:
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 TypNumber:
ia, oka := a.(int64)
ib, okb := b.(int64)
if oka && okb {
return ia <= ib
}
fa, oka := a.(float64)
fb, okb := b.(float64)
if oka && okb {
return fa <= fb
}
return forceFloat(a) <= forceFloat(b)
case TypString:
return a.(string) <= b.(string) // Fix me, should be locale sensitive, not lexical
}
}
if raw {
return false
}
return l.tryCmpMeta(op, a, b)
default:
luautil.Raise("Invalid comparison operator.", luautil.ErrTypGenRuntime)
panic("UNREACHABLE")
}
}