forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
1390 lines (1344 loc) · 34.4 KB
/
vm.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lua
////////////////////////////////////////////////////////
// This file was generated by go-inline. DO NOT EDIT. //
////////////////////////////////////////////////////////
import (
"fmt"
"math"
"strings"
)
func mainLoop(L *LState, baseframe *callFrame) {
var inst uint32
var cf *callFrame
if L.stack.IsEmpty() {
return
}
L.currentFrame = L.stack.Last()
if L.currentFrame.Fn.IsG {
callGFunction(L, false)
return
}
for {
cf = L.currentFrame
inst = cf.Fn.Proto.Code[cf.Pc]
cf.Pc++
if jumpTable[int(inst>>26)](L, inst, baseframe) == 1 {
return
}
}
}
func mainLoopWithContext(L *LState, baseframe *callFrame) {
var inst uint32
var cf *callFrame
if L.stack.IsEmpty() {
return
}
L.currentFrame = L.stack.Last()
if L.currentFrame.Fn.IsG {
callGFunction(L, false)
return
}
for {
cf = L.currentFrame
inst = cf.Fn.Proto.Code[cf.Pc]
cf.Pc++
select {
case <-L.ctx.Done():
L.RaiseError(L.ctx.Err().Error())
return
default:
if jumpTable[int(inst>>26)](L, inst, baseframe) == 1 {
return
}
}
}
}
func copyReturnValues(L *LState, regv, start, n, b int) { // +inline-start
if b == 1 {
// this section is inlined by go-inline
// source function is 'func (rg *registry) FillNil(regm, n int) ' in '_state.go'
{
rg := L.reg
regm := regv
for i := 0; i < n; i++ {
rg.array[regm+i] = LNil
}
rg.top = regm + n
}
} else {
// this section is inlined by go-inline
// source function is 'func (rg *registry) CopyRange(regv, start, limit, n int) ' in '_state.go'
{
rg := L.reg
limit := -1
for i := 0; i < n; i++ {
if tidx := start + i; tidx >= rg.top || limit > -1 && tidx >= limit || tidx < 0 {
rg.array[regv+i] = LNil
} else {
rg.array[regv+i] = rg.array[tidx]
}
}
rg.top = regv + n
}
}
} // +inline-end
func switchToParentThread(L *LState, nargs int, haserror bool, kill bool) {
parent := L.Parent
if parent == nil {
L.RaiseError("can not yield from outside of a coroutine")
}
L.G.CurrentThread = parent
L.Parent = nil
if !L.wrapped {
if haserror {
parent.Push(LFalse)
} else {
parent.Push(LTrue)
}
}
L.XMoveTo(parent, nargs)
L.stack.Pop()
offset := L.currentFrame.LocalBase - L.currentFrame.ReturnBase
L.currentFrame = L.stack.Last()
L.reg.SetTop(L.reg.Top() - offset) // remove 'yield' function(including tailcalled functions)
if kill {
L.kill()
}
}
func callGFunction(L *LState, tailcall bool) bool {
frame := L.currentFrame
gfnret := frame.Fn.GFunction(L)
if tailcall {
L.stack.Remove(L.stack.Sp() - 2) // remove caller lua function frame
L.currentFrame = L.stack.Last()
}
if gfnret < 0 {
switchToParentThread(L, L.GetTop(), false, false)
return true
}
wantret := frame.NRet
if wantret == MultRet {
wantret = gfnret
}
if tailcall && L.Parent != nil && L.stack.Sp() == 1 {
switchToParentThread(L, wantret, false, true)
return true
}
// this section is inlined by go-inline
// source function is 'func (rg *registry) CopyRange(regv, start, limit, n int) ' in '_state.go'
{
rg := L.reg
regv := frame.ReturnBase
start := L.reg.Top() - gfnret
limit := -1
n := wantret
for i := 0; i < n; i++ {
if tidx := start + i; tidx >= rg.top || limit > -1 && tidx >= limit || tidx < 0 {
rg.array[regv+i] = LNil
} else {
rg.array[regv+i] = rg.array[tidx]
}
}
rg.top = regv + n
}
L.stack.Pop()
L.currentFrame = L.stack.Last()
return false
}
func threadRun(L *LState) {
if L.stack.IsEmpty() {
return
}
defer func() {
if rcv := recover(); rcv != nil {
var lv LValue
if v, ok := rcv.(*ApiError); ok {
lv = v.Object
} else {
lv = LString(fmt.Sprint(rcv))
}
if parent := L.Parent; parent != nil {
if L.wrapped {
L.Push(lv)
parent.Panic(L)
} else {
L.SetTop(0)
L.Push(lv)
switchToParentThread(L, 1, true, true)
}
} else {
panic(rcv)
}
}
}()
L.mainLoop(L, nil)
}
type instFunc func(*LState, uint32, *callFrame) int
var jumpTable [opCodeMax + 1]instFunc
func init() {
jumpTable = [opCodeMax + 1]instFunc{
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_MOVE
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
reg.Set(RA, reg.Get(lbase+B))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_MOVEN
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(lbase+A, reg.Get(lbase+B))
code := cf.Fn.Proto.Code
pc := cf.Pc
for i := 0; i < C; i++ {
inst = code[pc]
pc++
A = int(inst>>18) & 0xff //GETA
B = int(inst & 0x1ff) //GETB
reg.Set(lbase+A, reg.Get(lbase+B))
}
cf.Pc = pc
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LOADK
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
Bx := int(inst & 0x3ffff) //GETBX
reg.Set(RA, cf.Fn.Proto.Constants[Bx])
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LOADBOOL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
if B != 0 {
reg.Set(RA, LTrue)
} else {
reg.Set(RA, LFalse)
}
if C != 0 {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LOADNIL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
for i := RA; i <= lbase+B; i++ {
reg.Set(i, LNil)
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETUPVAL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
reg.Set(RA, cf.Fn.Upvalues[B].Value())
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETGLOBAL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
Bx := int(inst & 0x3ffff) //GETBX
//reg.Set(RA, L.getField(cf.Fn.Env, cf.Fn.Proto.Constants[Bx]))
reg.Set(RA, L.getFieldString(cf.Fn.Env, cf.Fn.Proto.stringConstants[Bx]))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETTABLE
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, L.getField(reg.Get(lbase+B), L.rkValue(C)))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETTABLEKS
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, L.getFieldString(reg.Get(lbase+B), L.rkString(C)))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SETGLOBAL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
Bx := int(inst & 0x3ffff) //GETBX
//L.setField(cf.Fn.Env, cf.Fn.Proto.Constants[Bx], reg.Get(RA))
L.setFieldString(cf.Fn.Env, cf.Fn.Proto.stringConstants[Bx], reg.Get(RA))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SETUPVAL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
cf.Fn.Upvalues[B].SetValue(reg.Get(RA))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SETTABLE
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
L.setField(reg.Get(RA), L.rkValue(B), L.rkValue(C))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SETTABLEKS
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
L.setFieldString(reg.Get(RA), L.rkString(B), L.rkValue(C))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_NEWTABLE
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, newLTable(B, C))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SELF
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
selfobj := reg.Get(lbase + B)
reg.Set(RA, L.getFieldString(selfobj, L.rkString(C)))
reg.Set(RA+1, selfobj)
return 0
},
opArith, // OP_ADD
opArith, // OP_SUB
opArith, // OP_MUL
opArith, // OP_DIV
opArith, // OP_MOD
opArith, // OP_POW
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_UNM
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
unaryv := L.rkValue(B)
if nm, ok := unaryv.(LNumber); ok {
reg.SetNumber(RA, -nm)
} else {
op := L.metaOp1(unaryv, "__unm")
if op.Type() == LTFunction {
reg.Push(op)
reg.Push(unaryv)
L.Call(1, 1)
reg.Set(RA, reg.Pop())
} else if str, ok1 := unaryv.(LString); ok1 {
if num, err := parseNumber(string(str)); err == nil {
reg.Set(RA, -num)
} else {
L.RaiseError("__unm undefined")
}
} else {
L.RaiseError("__unm undefined")
}
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_NOT
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
if LVIsFalse(reg.Get(lbase + B)) {
reg.Set(RA, LTrue)
} else {
reg.Set(RA, LFalse)
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LEN
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
switch lv := L.rkValue(B).(type) {
case LString:
reg.SetNumber(RA, LNumber(len(lv)))
default:
op := L.metaOp1(lv, "__len")
if op.Type() == LTFunction {
reg.Push(op)
reg.Push(lv)
L.Call(1, 1)
reg.Set(RA, reg.Pop())
} else if lv.Type() == LTTable {
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
} else {
L.RaiseError("__len undefined")
}
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_CONCAT
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
RC := lbase + C
RB := lbase + B
reg.Set(RA, stringConcat(L, RC-RB+1, RC))
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_JMP
cf := L.currentFrame
Sbx := int(inst&0x3ffff) - opMaxArgSbx //GETSBX
cf.Pc += Sbx
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_EQ
cf := L.currentFrame
A := int(inst>>18) & 0xff //GETA
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
ret := equals(L, L.rkValue(B), L.rkValue(C), false)
v := 1
if ret {
v = 0
}
if v == A {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LT
cf := L.currentFrame
A := int(inst>>18) & 0xff //GETA
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
ret := lessThan(L, L.rkValue(B), L.rkValue(C))
v := 1
if ret {
v = 0
}
if v == A {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LE
cf := L.currentFrame
A := int(inst>>18) & 0xff //GETA
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
lhs := L.rkValue(B)
rhs := L.rkValue(C)
ret := false
if v1, ok1 := lhs.assertFloat64(); ok1 {
if v2, ok2 := rhs.assertFloat64(); ok2 {
ret = v1 <= v2
} else {
L.RaiseError("attempt to compare %v with %v", lhs.Type().String(), rhs.Type().String())
}
} else {
if lhs.Type() != rhs.Type() {
L.RaiseError("attempt to compare %v with %v", lhs.Type().String(), rhs.Type().String())
}
switch lhs.Type() {
case LTString:
ret = strCmp(string(lhs.(LString)), string(rhs.(LString))) <= 0
default:
switch objectRational(L, lhs, rhs, "__le") {
case 1:
ret = true
case 0:
ret = false
default:
ret = !objectRationalWithError(L, rhs, lhs, "__lt")
}
}
}
v := 1
if ret {
v = 0
}
if v == A {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_TEST
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
C := int(inst>>9) & 0x1ff //GETC
if LVAsBool(reg.Get(RA)) == (C == 0) {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_TESTSET
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
if value := reg.Get(lbase + B); LVAsBool(value) != (C == 0) {
reg.Set(RA, value)
} else {
cf.Pc++
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_CALL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
nargs := B - 1
if B == 0 {
nargs = reg.Top() - (RA + 1)
}
lv := reg.Get(RA)
nret := C - 1
var callable *LFunction
var meta bool
if fn, ok := lv.assertFunction(); ok {
callable = fn
meta = false
} else {
callable, meta = L.metaCall(lv)
}
// this section is inlined by go-inline
// source function is 'func (ls *LState) pushCallFrame(cf callFrame, fn LValue, meta bool) ' in '_state.go'
{
ls := L
cf := callFrame{Fn: callable, Pc: 0, Base: RA, LocalBase: RA + 1, ReturnBase: RA, NArgs: nargs, NRet: nret, Parent: cf, TailCall: 0}
fn := lv
if meta {
cf.NArgs++
ls.reg.Insert(fn, cf.LocalBase)
}
if cf.Fn == nil {
ls.RaiseError("attempt to call a non-function object")
}
if ls.stack.sp == ls.Options.CallStackSize {
ls.RaiseError("stack overflow")
}
// this section is inlined by go-inline
// source function is 'func (cs *callFrameStack) Push(v callFrame) ' in '_state.go'
{
cs := ls.stack
v := cf
cs.array[cs.sp] = v
cs.array[cs.sp].Idx = cs.sp
cs.sp++
}
newcf := ls.stack.Last()
// this section is inlined by go-inline
// source function is 'func (ls *LState) initCallFrame(cf *callFrame) ' in '_state.go'
{
cf := newcf
if cf.Fn.IsG {
ls.reg.SetTop(cf.LocalBase + cf.NArgs)
} else {
proto := cf.Fn.Proto
nargs := cf.NArgs
np := int(proto.NumParameters)
for i := nargs; i < np; i++ {
ls.reg.array[cf.LocalBase+i] = LNil
nargs = np
}
if (proto.IsVarArg & VarArgIsVarArg) == 0 {
if nargs < int(proto.NumUsedRegisters) {
nargs = int(proto.NumUsedRegisters)
}
for i := np; i < nargs; i++ {
ls.reg.array[cf.LocalBase+i] = LNil
}
ls.reg.top = cf.LocalBase + int(proto.NumUsedRegisters)
} else {
/* swap vararg positions:
closure
namedparam1 <- lbase
namedparam2
vararg1
vararg2
TO
closure
nil
nil
vararg1
vararg2
namedparam1 <- lbase
namedparam2
*/
nvarargs := nargs - np
if nvarargs < 0 {
nvarargs = 0
}
ls.reg.SetTop(cf.LocalBase + nargs + np)
for i := 0; i < np; i++ {
//ls.reg.Set(cf.LocalBase+nargs+i, ls.reg.Get(cf.LocalBase+i))
ls.reg.array[cf.LocalBase+nargs+i] = ls.reg.array[cf.LocalBase+i]
//ls.reg.Set(cf.LocalBase+i, LNil)
ls.reg.array[cf.LocalBase+i] = LNil
}
if CompatVarArg {
ls.reg.SetTop(cf.LocalBase + nargs + np + 1)
if (proto.IsVarArg & VarArgNeedsArg) != 0 {
argtb := newLTable(nvarargs, 0)
for i := 0; i < nvarargs; i++ {
argtb.RawSetInt(i+1, ls.reg.Get(cf.LocalBase+np+i))
}
argtb.RawSetString("n", LNumber(nvarargs))
//ls.reg.Set(cf.LocalBase+nargs+np, argtb)
ls.reg.array[cf.LocalBase+nargs+np] = argtb
} else {
ls.reg.array[cf.LocalBase+nargs+np] = LNil
}
}
cf.LocalBase += nargs
maxreg := cf.LocalBase + int(proto.NumUsedRegisters)
ls.reg.SetTop(maxreg)
}
}
}
ls.currentFrame = newcf
}
if callable.IsG && callGFunction(L, false) {
return 1
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_TAILCALL
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
nargs := B - 1
if B == 0 {
nargs = reg.Top() - (RA + 1)
}
lv := reg.Get(RA)
var callable *LFunction
var meta bool
if fn, ok := lv.assertFunction(); ok {
callable = fn
meta = false
} else {
callable, meta = L.metaCall(lv)
}
if callable == nil {
L.RaiseError("attempt to call a non-function object")
}
// this section is inlined by go-inline
// source function is 'func (ls *LState) closeUpvalues(idx int) ' in '_state.go'
{
ls := L
idx := lbase
if ls.uvcache != nil {
var prev *Upvalue
for uv := ls.uvcache; uv != nil; uv = uv.next {
if uv.index >= idx {
if prev != nil {
prev.next = nil
} else {
ls.uvcache = nil
}
uv.Close()
}
prev = uv
}
}
}
if callable.IsG {
luaframe := cf
L.pushCallFrame(callFrame{
Fn: callable,
Pc: 0,
Base: RA,
LocalBase: RA + 1,
ReturnBase: cf.ReturnBase,
NArgs: nargs,
NRet: cf.NRet,
Parent: cf,
TailCall: 0,
}, lv, meta)
if callGFunction(L, true) {
return 1
}
if L.currentFrame == nil || L.currentFrame.Fn.IsG || luaframe == baseframe {
return 1
}
} else {
base := cf.Base
cf.Fn = callable
cf.Pc = 0
cf.Base = RA
cf.LocalBase = RA + 1
cf.ReturnBase = cf.ReturnBase
cf.NArgs = nargs
cf.NRet = cf.NRet
cf.TailCall++
lbase := cf.LocalBase
if meta {
cf.NArgs++
L.reg.Insert(lv, cf.LocalBase)
}
// this section is inlined by go-inline
// source function is 'func (ls *LState) initCallFrame(cf *callFrame) ' in '_state.go'
{
ls := L
if cf.Fn.IsG {
ls.reg.SetTop(cf.LocalBase + cf.NArgs)
} else {
proto := cf.Fn.Proto
nargs := cf.NArgs
np := int(proto.NumParameters)
for i := nargs; i < np; i++ {
ls.reg.array[cf.LocalBase+i] = LNil
nargs = np
}
if (proto.IsVarArg & VarArgIsVarArg) == 0 {
if nargs < int(proto.NumUsedRegisters) {
nargs = int(proto.NumUsedRegisters)
}
for i := np; i < nargs; i++ {
ls.reg.array[cf.LocalBase+i] = LNil
}
ls.reg.top = cf.LocalBase + int(proto.NumUsedRegisters)
} else {
/* swap vararg positions:
closure
namedparam1 <- lbase
namedparam2
vararg1
vararg2
TO
closure
nil
nil
vararg1
vararg2
namedparam1 <- lbase
namedparam2
*/
nvarargs := nargs - np
if nvarargs < 0 {
nvarargs = 0
}
ls.reg.SetTop(cf.LocalBase + nargs + np)
for i := 0; i < np; i++ {
//ls.reg.Set(cf.LocalBase+nargs+i, ls.reg.Get(cf.LocalBase+i))
ls.reg.array[cf.LocalBase+nargs+i] = ls.reg.array[cf.LocalBase+i]
//ls.reg.Set(cf.LocalBase+i, LNil)
ls.reg.array[cf.LocalBase+i] = LNil
}
if CompatVarArg {
ls.reg.SetTop(cf.LocalBase + nargs + np + 1)
if (proto.IsVarArg & VarArgNeedsArg) != 0 {
argtb := newLTable(nvarargs, 0)
for i := 0; i < nvarargs; i++ {
argtb.RawSetInt(i+1, ls.reg.Get(cf.LocalBase+np+i))
}
argtb.RawSetString("n", LNumber(nvarargs))
//ls.reg.Set(cf.LocalBase+nargs+np, argtb)
ls.reg.array[cf.LocalBase+nargs+np] = argtb
} else {
ls.reg.array[cf.LocalBase+nargs+np] = LNil
}
}
cf.LocalBase += nargs
maxreg := cf.LocalBase + int(proto.NumUsedRegisters)
ls.reg.SetTop(maxreg)
}
}
}
// this section is inlined by go-inline
// source function is 'func (rg *registry) CopyRange(regv, start, limit, n int) ' in '_state.go'
{
rg := L.reg
regv := base
start := RA
limit := -1
n := reg.Top() - RA - 1
for i := 0; i < n; i++ {
if tidx := start + i; tidx >= rg.top || limit > -1 && tidx >= limit || tidx < 0 {
rg.array[regv+i] = LNil
} else {
rg.array[regv+i] = rg.array[tidx]
}
}
rg.top = regv + n
}
cf.Base = base
cf.LocalBase = base + (cf.LocalBase - lbase + 1)
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_RETURN
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
// this section is inlined by go-inline
// source function is 'func (ls *LState) closeUpvalues(idx int) ' in '_state.go'
{
ls := L
idx := lbase
if ls.uvcache != nil {
var prev *Upvalue
for uv := ls.uvcache; uv != nil; uv = uv.next {
if uv.index >= idx {
if prev != nil {
prev.next = nil
} else {
ls.uvcache = nil
}
uv.Close()
}
prev = uv
}
}
}
nret := B - 1
if B == 0 {
nret = reg.Top() - RA
}
n := cf.NRet
if cf.NRet == MultRet {
n = nret
}
if L.Parent != nil && L.stack.Sp() == 1 {
// this section is inlined by go-inline
// source function is 'func copyReturnValues(L *LState, regv, start, n, b int) ' in '_vm.go'
{
regv := reg.Top()
start := RA
b := B
if b == 1 {
// this section is inlined by go-inline
// source function is 'func (rg *registry) FillNil(regm, n int) ' in '_state.go'
{
rg := L.reg
regm := regv
for i := 0; i < n; i++ {
rg.array[regm+i] = LNil
}
rg.top = regm + n
}
} else {
// this section is inlined by go-inline
// source function is 'func (rg *registry) CopyRange(regv, start, limit, n int) ' in '_state.go'
{
rg := L.reg
limit := -1
for i := 0; i < n; i++ {
if tidx := start + i; tidx >= rg.top || limit > -1 && tidx >= limit || tidx < 0 {
rg.array[regv+i] = LNil
} else {
rg.array[regv+i] = rg.array[tidx]
}
}
rg.top = regv + n
}
}
}
switchToParentThread(L, n, false, true)
return 1
}
islast := baseframe == L.stack.Pop() || L.stack.IsEmpty()
// this section is inlined by go-inline
// source function is 'func copyReturnValues(L *LState, regv, start, n, b int) ' in '_vm.go'
{
regv := cf.ReturnBase
start := RA
b := B
if b == 1 {
// this section is inlined by go-inline
// source function is 'func (rg *registry) FillNil(regm, n int) ' in '_state.go'
{
rg := L.reg
regm := regv
for i := 0; i < n; i++ {
rg.array[regm+i] = LNil
}
rg.top = regm + n
}
} else {
// this section is inlined by go-inline
// source function is 'func (rg *registry) CopyRange(regv, start, limit, n int) ' in '_state.go'
{
rg := L.reg
limit := -1
for i := 0; i < n; i++ {
if tidx := start + i; tidx >= rg.top || limit > -1 && tidx >= limit || tidx < 0 {
rg.array[regv+i] = LNil
} else {
rg.array[regv+i] = rg.array[tidx]
}
}
rg.top = regv + n
}
}
}
L.currentFrame = L.stack.Last()
if islast || L.currentFrame == nil || L.currentFrame.Fn.IsG {
return 1
}
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_FORLOOP
reg := L.reg
cf := L.currentFrame
lbase := cf.LocalBase
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
if init, ok1 := reg.Get(RA).assertFloat64(); ok1 {
if limit, ok2 := reg.Get(RA + 1).assertFloat64(); ok2 {
if step, ok3 := reg.Get(RA + 2).assertFloat64(); ok3 {