-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_state.cpp
1417 lines (1268 loc) · 33.9 KB
/
lua_state.cpp
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
#include "state/lua_value.h"
#include "state/lua_table.h"
#include "state/api_arith.h"
#include "vm/inst_misc.h"
#include "vm/inst_operators.h"
#include "vm/inst_table.h"
#include "vm/inst_call.h"
#include "vm/inst_upvalue.h"
#include "vm/opcodes.h"
String g_panic_message;
const OpCode opcodes[47] =
{
#define MAKE_OP_CODE(T, A, B, C, mode, name, action) OpCode{T, A, B, C, mode, #name, action}
MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iABC, MOVE, __inst_misc__::move)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgN, iABx, LOADK, __inst_misc__::loadK)
,MAKE_OP_CODE(0, 1, OpArgN, OpArgN, iABx, LOADKX, __inst_misc__::loadKx)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgU, iABC, LOADBOOL, __inst_misc__::loadBool)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgN, iABC, LOADNIL, __inst_misc__::loadNil)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgN, iABC, GETUPVAL, __upvalue_inst__::getUpVal)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgK, iABC, GETTABUP, __upvalue_inst__::getTabUp)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgK, iABC, GETTABLE, __table_insts__::getTable)
,MAKE_OP_CODE(0, 0, OpArgK, OpArgK, iABC, SETTABUP, __upvalue_inst__::setTabUp)
,MAKE_OP_CODE(0, 0, OpArgU, OpArgN, iABC, SETUPVAL, __upvalue_inst__::setUpVal)
,MAKE_OP_CODE(0, 0, OpArgK, OpArgK, iABC, SETTABLE, __table_insts__::setTable)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgU, iABC, NEWTABLE, __table_insts__::newTable)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgK, iABC, SELF, __call_insts__::self)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, ADD, __binary_insts__::add)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, SUB, __binary_insts__::sub)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, MUL, __binary_insts__::mul)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, MOD, __binary_insts__::mod)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, POW, __binary_insts__::pow)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, DIV, __binary_insts__::div)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, IDIV, __binary_insts__::idiv)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, BAND, __binary_insts__::band)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, BOR, __binary_insts__::bor)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, BXOR, __binary_insts__::bxor)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, SHL, __binary_insts__::shl)
,MAKE_OP_CODE(0, 1, OpArgK, OpArgK, iABC, SHR, __binary_insts__::shr)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iABC, UNM, __binary_insts__::unm)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iABC, BNOT, __binary_insts__::bnot)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iABC, NOT, __other_insts__::_not)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iABC, LEN, __str_insts__::length)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgR, iABC, CONCAT, __str_insts__::cat)
,MAKE_OP_CODE(0, 0, OpArgR, OpArgN, iAsBx, JMP, __inst_misc__::jmp)
,MAKE_OP_CODE(1, 0, OpArgK, OpArgK, iABC, EQ, __compare_insts__::eq)
,MAKE_OP_CODE(1, 0, OpArgK, OpArgK, iABC, LT, __compare_insts__::lt)
,MAKE_OP_CODE(1, 0, OpArgK, OpArgK, iABC, LE, __compare_insts__::le)
,MAKE_OP_CODE(1, 0, OpArgN, OpArgU, iABC, TEST, __other_insts__::test)
,MAKE_OP_CODE(1, 1, OpArgR, OpArgU, iABC, TESTSET, __other_insts__::testSet)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgU, iABC, CALL, __call_insts__::call)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgU, iABC, TAILCALL, __call_insts__::tailCall)
,MAKE_OP_CODE(0, 0, OpArgU, OpArgN, iABC, RETURN, __call_insts__::_return)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iAsBx, FORLOOP, __other_insts__::forLoop)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iAsBx, FORPREP, __other_insts__::forPrep)
,MAKE_OP_CODE(0, 0, OpArgN, OpArgU, iABC, TFORCALL, __call_insts__::tForCall)
,MAKE_OP_CODE(0, 1, OpArgR, OpArgN, iAsBx, TFORLOOP, __call_insts__::tForLoop)
,MAKE_OP_CODE(0, 0, OpArgU, OpArgU, iABC, SETLIST, __table_insts__::setList)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgN, iABx, CLOSURE, __call_insts__::closure)
,MAKE_OP_CODE(0, 1, OpArgU, OpArgN, iABC, VARARG, __call_insts__::vararg)
,MAKE_OP_CODE(0, 0, OpArgU, OpArgU, iAx, EXTRAARG, nullptr)
#undef MAKE_OP_CODE
};
struct __funcs__
{
static Int64 iadd(Int64 a, Int64 b) { return a + b; }
static Float64 fadd(Float64 a, Float64 b) { return a + b; }
static Int64 isub(Int64 a, Int64 b) { return a - b; }
static Float64 fsub(Float64 a, Float64 b) { return a - b; }
static Int64 imul(Int64 a, Int64 b) { return a * b; }
static Float64 fmul(Float64 a, Float64 b) { return a * b; }
static Int64 imod(Int64 a, Int64 b) { return IMod(a, b); }
static Float64 fmod(Float64 a, Float64 b) { return FMod(a, b); }
static Float64 pow(Float64 a, Float64 b) { return ::pow(a, b); }
static Float64 div(Float64 a, Float64 b) { return a / b; }
static Int64 iidiv(Int64 a, Int64 b) { return IFloorDiv(a, b); }
static Float64 fidiv(Float64 a, Float64 b) { return FFloorDiv(a, b); }
static Int64 band(Int64 a, Int64 b) { return a & b; }
static Int64 bor(Int64 a, Int64 b) { return a | b; }
static Int64 bxor(Int64 a, Int64 b) { return a ^ b; }
static Int64 shl(Int64 a, Int64 b) { return ShiftLeft(a, b); }
static Int64 shr(Int64 a, Int64 b) { return ShiftRight(a, b); }
static Int64 iunm(Int64 a, Int64) { return -a; }
static Float64 funm(Float64 a, Float64) { return -a; }
static Int64 bnot(Int64 a, Int64) { return ~a; }
};
const Operator operators[14] =
{
Operator{"__add", __funcs__::iadd, __funcs__::fadd},
Operator{"__sub", __funcs__::isub, __funcs__::fsub},
Operator{"__mul",__funcs__::imul, __funcs__::fmul},
Operator{"__mod", __funcs__::imod, __funcs__::fmod},
Operator{"__pow", nullptr, __funcs__::pow},
Operator{"__div", nullptr, __funcs__::div},
Operator{"__idiv", __funcs__::iidiv, __funcs__::fidiv},
Operator{"__band", __funcs__::band, nullptr},
Operator{"__bor", __funcs__::bor, nullptr},
Operator{"__bxor", __funcs__::bxor, nullptr},
Operator{"__shl", __funcs__::shl, nullptr},
Operator{"__shr", __funcs__::shr, nullptr},
Operator{"__unm", __funcs__::iunm, __funcs__::funm},
Operator{"__bnot", __funcs__::bnot, nullptr},
};
void Instruction::Execute(LuaVM *vm)
{
OpCode::Func action = opcodes[Opcode()].action;
if(action)
action(*this, vm);
else
warning(OpName().c_str());
}
void SetMetatable(LuaValue& val, LuaTablePtr mt, LuaState* ls)
{
if(val.IsTable())
{
DEBUG_PRINT("SetMetatable: 0x%x 0x%x\n", (size_t)val.table.get(), (size_t)mt.get());
val.table->metatable = mt;
return;
}
String key = Format::FormatString("_MT%d", val.tag);
ls->registry->Put(LuaValue(key), LuaValue(mt));
}
LuaTablePtr GetMetatable(const LuaValue& val, const LuaState* ls)
{
if(val.IsTable())
{
DEBUG_PRINT("GetMetatable: 0x%x\n", (size_t)val.table.get());
return val.table->metatable;
}
String key = Format::FormatString("_MT%d", val.tag);
LuaValuePtr mt = ls->registry->Get(LuaValue(key));
if(mt && mt->IsTable())
{
return mt->table;
}
return nullptr;
}
std::tuple<LuaValue, bool> CallMetamethod(const LuaValue& a, const LuaValue& b,
const String& mmName, LuaState* ls)
{
LuaValue mm;
mm = ::GetMetafield(a, mmName, ls);
if(mm == LuaValue::Nil)
mm = ::GetMetafield(b, mmName, ls);
if(mm == LuaValue::Nil)
return std::make_tuple(LuaValue::Nil, false);
ls->stack->Check(4);
ls->stack->Push(mm);
ls->stack->Push(a);
ls->stack->Push(b);
ls->Call(2, 1);
return std::make_tuple(*ls->stack->Pop(), true);
}
LuaValue GetMetafield(const LuaValue& val, const String& fieldName, const LuaState* ls)
{
LuaTablePtr mt = GetMetatable(val, ls);
if(mt)
{
return *mt->Get(LuaValue(fieldName));
}
return LuaValue::Nil;
}
LuaState::LuaState()
{
stack = nullptr;
registry = nullptr;
coStatus = 0;
}
int LuaState::GetTop() const
{
return (int)stack->top;
}
int LuaState::AbsIndex(int idx) const
{
return stack->AbsIndex(idx);
}
bool LuaState::CheckStack(int n)
{
stack->Check(n);
return true;
}
void LuaState::Pop(int n)
{
for(int i = 0; i < n; ++i)
stack->Pop();
}
void LuaState::Copy(int fromIdx, int toIdx)
{
LuaValue val = stack->Get(fromIdx);
stack->Set(toIdx, val);
}
void LuaState::PushValue(int idx)
{
LuaValue val = stack->Get(idx);
stack->Push(val);
}
// Pop the value from the top, and set it back the stack by the index(so the index should be > 0)
void LuaState::Replace(int idx)
{
LuaValue val = *stack->Pop();
stack->Set(idx, val);
}
/*
Push the element of [idx, top] to the right by n units (if n >= 0)
otherwise push it to the left by n units
For array [1,2,3,4,5,6,7] move 4 units to the right
[1,2,3,4,5,6,7] -> [3,2,1,4,5,6,7] -> [3,2,1,7,6,5,4] -> [4,5,6,7,1,2,3]
It can be seen that p is the starting index,
t is the ending index, and shifted to the right by n units ==> r(p, t - n), r(t - n + 1, t), r(p, t)
On the contrary, if [1,2,3,4,5,6,7] move 4 units to the left
[1,2,3,4,5,6,7] -> [4,3,2,1,5,6,7] -> [4,3,2,1,7,6,5] -> [5,6,7,1,2,3,4]
It can be seen that p is the starting index,
t is the ending index, and shifted to the left by n units ==> r(p, p + n - 1), r(p + n, t), r(p, t)
*/
void LuaState::Rotate(int idx, int n)
{
size_t t = stack->top - 1;
size_t p = stack->AbsIndex(idx) - 1;
size_t m = (n >= 0) ? (t - n) : (p - n - 1);
stack->_Reverse(p, m);
stack->_Reverse(m + 1, t);
stack->_Reverse(p, t);
}
// Set capacity of the stack(idx means the capacity)
void LuaState::SetTop(int idx)
{
size_t newTop = stack->AbsIndex(idx);
if(newTop < 0)
{
panic("stack underflow");
}
int n = (int)stack->top - (int)newTop;
if(n > 0)
{
for(int i = 0; i < n; ++i)
stack->Pop();
}
else if(n < 0)
{
for(int i = 0; i > n; --i)
stack->Push(LuaValue::Nil);
}
}
// Pop the top of the stack and insert it into the specified position
void LuaState::Insert(int idx)
{
Rotate(idx, 1);
}
void LuaState::Remove(int idx)
{
Rotate(idx, -1);
Pop(1);
}
void LuaState::PushNil() { stack->Push(LuaValue::Nil); }
void LuaState::PushBoolean(bool b) { stack->Push(LuaValue(b)); }
void LuaState::PushInteger(Int64 n) { stack->Push(LuaValue(n)); }
void LuaState::PushNumber(Float64 n) { stack->Push(LuaValue(n)); }
void LuaState::PushString(const String& str){ stack->Push(LuaValue(str)); }
LuaType LuaState::Type(int idx) const
{
if(stack->IsValid(idx))
{
LuaValue value = stack->Get(idx);
return value.tag;
}
return LUA_TNONE;
}
bool LuaState::IsNone(int idx) const { return Type(idx) == LUA_TNONE; }
bool LuaState::IsNil(int idx) const { return Type(idx) == LUA_TNIL; }
bool LuaState::IsNoneOrNil(int idx) const { return Type(idx) <= LUA_TNIL; }
bool LuaState::IsInteger(int idx) const
{
LuaValue val = stack->Get(idx);
if(val.tag == LUA_TNUMBER && !val.isfloat)
return true;
return false;
}
bool LuaState::IsNumber(int idx) const
{
auto pair = ToNumberX(idx);
return std::get<1>(pair);
}
bool LuaState::IsBoolean(int idx) const { return Type(idx) == LUA_TBOOLEAN; }
bool LuaState::IsTable(int idx) const { return Type(idx) == LUA_TTABLE; }
bool LuaState::IsThread(int idx) const { return Type(idx) == LUA_TTHREAD; }
bool LuaState::IsFunction(int idx) const { return Type(idx) == LUA_TFUNCTION; }
bool LuaState::IsString(int idx) const
{
LuaType t = Type(idx);
return t == LUA_TSTRING || t == LUA_TNUMBER;
}
void* LuaState::ToPointer(int idx) const
{
if(idx < (int)stack->slots.size())
return stack->slots[idx].get();
return NULL;
}
bool LuaState::ToBoolean(int idx) const
{
LuaValue value = stack->Get(idx);
return ConvertToBoolean(value);
}
std::tuple<Float64, bool> LuaState::ToNumberX(int idx) const
{
LuaValue value = stack->Get(idx);
return ConvertToFloat(value);
}
Float64 LuaState::ToNumber(int idx) const
{
auto pair = ToNumberX(idx);
return std::get<0>(pair);
}
std::tuple<Int64, bool> LuaState::ToIntegerX(int idx) const
{
LuaValue value = stack->Get(idx);
return ConvertToInteger(value);
}
Int64 LuaState::ToInteger(int idx) const
{
auto pair = ToIntegerX(idx);
return std::get<0>(pair);
}
std::tuple<String, bool> LuaState::ToStringX(int idx)
{
LuaValue val = stack->Get(idx);
switch(val.tag)
{
case LUA_TSTRING: return std::make_tuple(val.str, true);
case LUA_TNUMBER:
{
std::tuple<String, bool> ret;
if(val.isfloat)
ret = std::make_tuple(std::to_string(val.number), true);
else
ret = std::make_tuple(std::to_string(val.integer), true);
stack->Set(idx, LuaValue(std::get<0>(ret)));
return ret;
}
default: return std::make_tuple("", false);
}
}
String LuaState::ToString(int idx)
{
auto pair = ToStringX(idx);
return std::get<0>(pair);
}
void LuaState::Arith(ArithOp op)
{
LuaValue b = *stack->Pop();
LuaValue a;
if(op != LUA_OPUNM && op != LUA_OPBNOT)
a = *stack->Pop();
else
a = b;
Operator operaotr = operators[op];
LuaValue res = _Arith(a, b, operaotr);
if(res != LuaValue::Nil)
{
stack->Push(res);
return;
}
// Call method only if value can not be converted into number
const String& mm = operaotr.metamethod;
auto metaRes = CallMetamethod(a, b, mm, this);
if(std::get<1>(metaRes))
{
stack->Push(std::get<0>(metaRes));
return;
}
panic("arithmetic error!");
}
bool LuaState::Compare(int idx1, int idx2, CompareOp op)
{
LuaValue a = stack->Get(idx1);
LuaValue b = stack->Get(idx2);
switch (op)
{
case LUA_OPEQ: return _eq(a, b, this, false);
case LUA_OPLT: return _lt(a, b, this, false);
case LUA_OPLE: return _le(a, b, this, false);
default: panic("invalid compare op!"); return false;
}
}
bool LuaState::RawEqual(int idx1, int idx2)
{
LuaValue a = stack->Get(idx1);
LuaValue b = stack->Get(idx2);
return _eq(a, b, this, true);
}
void LuaState::Len(int idx)
{
LuaValue val = stack->Get(idx);
if(val.IsString())
stack->Push(LuaValue((Int64)val.str.length()));
else
{
auto metaRes = CallMetamethod(val, val, "__len", this);
if(std::get<1>(metaRes))
{
stack->Push(std::get<0>(metaRes));
}
else if(val.IsTable())
{
stack->Push(LuaValue((Int64)val.table->Len()));
}
else
{
panic("length error!");
}
}
}
int LuaState::RawLen(int idx)
{
LuaValue val = stack->Get(idx);
if(val.IsString())
return (int)val.str.length();
else if(val.IsTable())
return (int)val.table->Len();
else
return 0;
}
void LuaState::Concat(int n)
{
if(n == 0)
{
stack->Push(LuaValue(""));
}
else if(n > 1)
{
for(int i = 1; i < n; ++i)
{
if(IsString(-1) && IsString(-2))
{
String s1 = ToString(-2);
String s2 = ToString(-1);
stack->Pop();
stack->Pop();
stack->Push(LuaValue(s1 + s2));
continue;
}
LuaValue b = *stack->Pop();
LuaValue a = *stack->Pop();
auto metaRes = CallMetamethod(a, b, "__concat", this);
if(std::get<1>(metaRes))
{
stack->Push(std::get<0>(metaRes));
continue;
}
panic("concatenation error!");
}
}
// n == 1, do nothing
}
void LuaState::PushFString(const char* fmt, ...)
{
va_list list;
va_start(list, fmt);
String msg = Format::FormatString(fmt, list);
va_end(list);
stack->Push(LuaValue(msg));
}
String LuaState::TypeName(LuaType tp)
{
switch (tp)
{
case LUA_TNONE: return "no value";
case LUA_TNIL: return "nil";
case LUA_TBOOLEAN: return "boolean";
case LUA_TNUMBER: return "number";
case LUA_TSTRING: return "string";
case LUA_TTABLE: return "table";
case LUA_TFUNCTION: return "function";
case LUA_TTHREAD: return "thread";
case LUA_TLIGHTUSERDATA:
case LUA_TUSERDATA:
default: return "userdata";
}
}
/*
interfaces for table
*/
void LuaState::CreateTable(int nArr, int nRec)
{
LuaTablePtr t = NewLuaTable(nArr, nRec);
stack->Push(LuaValue(t));
}
void LuaState::NewTable()
{
CreateTable(0, 0);
}
LuaType LuaState::_GetTable(const LuaValue& t, const LuaValue& k, bool raw)
{
if(t.IsTable())
{
LuaValue v = *t.table->Get(k);
if(raw || v != LuaValue::Nil || !t.table->HasMetafield("__index"))
{
stack->Push(v);
return v.tag;
}
}
if(!raw)
{
LuaValue mf = ::GetMetafield(t, "__index", this);
if(mf != LuaValue::Nil)
{
switch (mf.tag)
{
case LUA_TTABLE:
{
return _GetTable(mf, k, false);
}
case LUA_TFUNCTION:
{
stack->Push(mf);
stack->Push(t);
stack->Push(k);
Call(2, 1);
LuaValue v = stack->Get(-1);
return v.tag;
}
default:
break;
}
}
}
panic("index error!");
return LUA_TNONE;
}
LuaType LuaState::GetTable(int idx)
{
LuaValue t = stack->Get(idx);
LuaValue k = *stack->Pop();
return _GetTable(t, k, false);
}
LuaType LuaState::GetField(int idx, const String& k)
{
LuaValue t = stack->Get(idx);
return _GetTable(t, LuaValue(k), false);
}
LuaType LuaState::RawGetField(int idx, const String& k)
{
LuaValue t = stack->Get(idx);
return _GetTable(t, LuaValue(k), true);
}
LuaType LuaState::GetI(int idx, Int64 k)
{
LuaValue t = stack->Get(idx);
return _GetTable(t, LuaValue(k), false);
}
LuaType LuaState::RawGet(int idx)
{
LuaValue t = stack->Get(idx);
LuaValue k = *stack->Pop();
return _GetTable(t, k, true);
}
LuaType LuaState::RawGetI(int idx, Int64 k)
{
LuaValue t = stack->Get(idx);
return _GetTable(t, LuaValue(k), true);
}
void LuaState::_SetTable(const LuaValue& t, const LuaValue& k, const LuaValue& v, bool raw)
{
if(t.IsTable())
{
LuaTablePtr tbl = t.table;
if(raw || *tbl->Get(k) != LuaValue::Nil || !tbl->HasMetafield("__newindex"))
{
t.table->Put(k, v);
return;
}
}
if(!raw)
{
LuaValue mf = ::GetMetafield(t, "__newindex", this);
if(mf != LuaValue::Nil)
{
switch (mf.tag)
{
case LUA_TTABLE:
{
return _SetTable(mf, k, v, false);
}
case LUA_TFUNCTION:
{
stack->Push(mf);
stack->Push(t);
stack->Push(k);
stack->Push(v);
Call(3, 0);
return;
}
default:
break;
}
}
}
panic("index error!");
}
void LuaState::SetTable(int idx)
{
LuaValue t = stack->Get(idx);
LuaValue v = *stack->Pop();
LuaValue k = *stack->Pop();
_SetTable(t, k, v, false);
}
void LuaState::SetField(int idx, const String& k)
{
LuaValue t = stack->Get(idx);
LuaValue v = *stack->Pop();
_SetTable(t, LuaValue(k), LuaValue(v), false);
}
void LuaState::SetI(int idx, Int64 i)
{
LuaValue t = stack->Get(idx);
LuaValue v = *stack->Pop();
_SetTable(t, LuaValue(i), LuaValue(v), false);
}
void LuaState::RawSet(int idx)
{
LuaValue t = stack->Get(idx);
LuaValue v = *stack->Pop();
LuaValue k = *stack->Pop();
_SetTable(t, LuaValue(k), LuaValue(v), true);
}
void LuaState::RawSetI(int idx, Int64 i)
{
LuaValue t = stack->Get(idx);
LuaValue v = *stack->Pop();
_SetTable(t, LuaValue(i), LuaValue(v), true);
}
void LuaState::PushLuaStack(LuaStackPtr s)
{
s->prev = stack;
stack = s;
}
void LuaState::PopLuaStack()
{
LuaStackPtr s = stack;
stack = stack->prev;
s->prev = nullptr;
}
bool LuaState::IsBinaryChunk(const ByteArray& chunk)
{
if(chunk.size() >= 4)
{
Byte signature[4];
static_assert(sizeof(signature) == sizeof(UInt32), "size check");
memcpy(signature, chunk.data(), sizeof(signature));
return memcmp(signature, LUA_SIGNATURE, sizeof(signature)) == 0;
}
return false;
}
int LuaState::Load(const ByteArray& chunk, const String& chunkName, const String& mode)
{
PrototypePtr proto = nullptr;
if(IsBinaryChunk(chunk))
{
proto = Undump(chunk);
}
else
{
String strChunk;
strChunk.reserve(chunk.size());
for(Byte ch : chunk)
{
strChunk += ch;
}
proto = Compile(strChunk, chunkName);
}
ClosurePtr closure = NewLuaClosure(proto);
stack->Push(LuaValue(closure));
if(proto->Upvalues.size() > 0)
{
LuaValuePtr env = registry->Get(LuaValue(LUA_RIDX_GLOBALS));
closure->upvals[0] = UpValue(env);
}
return LUA_OK;
}
void LuaState::RunLuaClosure()
{
DEBUG_PRINT("Run Lua Closure");
while(true)
{
Instruction inst = Instruction(Fetch());
#if DEBUG_PRINT_ENABLE
LuaStackPtr counter = stack;
int depth = 0;
while (counter->prev)
{
++depth;
counter = counter->prev;
}
for (int i = 0; i < depth * 5; ++i)
printf("(");
printf("thread:0x%x stack:0x%x pc:%d\n", (size_t)this, (size_t)stack.get(), stack->pc - 1);
puts("----------Stack Before Execution----------");
PrintStack(*this);
puts("----------Stack Before Execution----------");
DEBUG_PRINT("%s", inst.OpName().c_str());
Prototype::PrintOperands(inst);
puts("");
#endif
inst.Execute(this);
#if DEBUG_PRINT_ENABLE
puts("----------Stack After Execution----------");
PrintStack(*this);
puts("----------Stack After Execution----------");
printf("thread:0x%x stack:0x%x pc:%d", (size_t)this, (size_t)stack.get(), stack->pc - 1);
for (int i = 0; i < depth * 5; ++i)
printf(")");
puts("");
#endif
if(inst.Opcode() == OP_RETURN)
break;
}
}
void LuaState::CallLuaClosure(int nArgs, int nResults, ClosurePtr c)
{
int nRegs = (int)c->proto->MaxStackSize;
int nParams = (int)c->proto->NumParams;
bool isVararg = c->proto->IsVararg == 1;
LuaStackPtr newStack = NewLuaStack(nRegs + LUA_MINSTACK, stack->state);
// Assign the function
newStack->closure = c;
// a = func(1,2,3,...)
// a(1,2,3,4,5) nArgs = 5, nParams = 3
// nArgs(which is all arguments) contains nParams(which is non varargs arguments)
LuaValueArray funcAndArgs = stack->PopN(nArgs + 1);
// Push the non varargs arguments
newStack->PushN(Slice(funcAndArgs, 1, (int)funcAndArgs.size()), nParams);
panic_cond(nRegs >= nParams, "nRegs is less than nParams");
newStack->top = nRegs;
if (nArgs > nParams && isVararg)
{
// Assign the varargs arguments
newStack->varargs = Slice(funcAndArgs, nParams + 1, (int)funcAndArgs.size());
}
PushLuaStack(newStack);
RunLuaClosure();
PopLuaStack();
panic_cond(!stack->closure || stack->closure->cFunc || stack->top == stack->closure->proto->MaxStackSize,
"return values must locate on MaxStackSize of current call frame");
if (nResults != 0)
{
// Pop the return values from the newStack
LuaValueArray results = newStack->PopN(newStack->top - nRegs);
stack->Check((int)results.size());
// nResults not "(int)results.size()" for some ticky usage
stack->PushN(results, nResults);
}
}
void LuaState::CallCClosure(int nArgs, int nResults, ClosurePtr c)
{
LuaStackPtr newStack = NewLuaStack(nArgs + LUA_MINSTACK, stack->state);
newStack->closure = c;
LuaValueArray args = stack->PopN(nArgs);
newStack->PushN(args, nArgs);
// Throw away the C closure
stack->Pop();
PushLuaStack(newStack);
int r = c->cFunc(this);
PopLuaStack();
if(nResults != 0)
{
LuaValueArray results = newStack->PopN(r);
newStack->Check((int)results.size());
stack->PushN(results, nResults);
}
}
void LuaState::Call(int nArgs, int nResults)
{
// closure func
LuaValue val = stack->Get(-(nArgs + 1));
if(!val.IsClosure())
{
LuaValue mf = ::GetMetafield(val, "__call", this);
if(mf != LuaValue::Nil)
{
if(mf.IsClosure())
{
stack->Push(mf);
Insert(-(nArgs + 2));
nArgs += 1;
val = mf;
}
}
}
if(val.IsClosure())
{
ClosurePtr c = val.closure;
if(c->proto)
{
DEBUG_PRINT("call %s<%d,%d>", c->proto->Source.c_str(),
c->proto->LineDefined,
c->proto->LastLineDefined);
}
else
{
auto it = cFuncNames.find(c->cFunc);
if (it != cFuncNames.end() && it->second.length() > 0)
{
DEBUG_PRINT("%s", it->second.c_str());
}
else
{
DEBUG_PRINT("call c function");
}
}
if(c->proto != nullptr)
CallLuaClosure(nArgs, nResults, c);
else
CallCClosure(nArgs, nResults, c);
}
else
{
panic("not a function");
}
}
void LuaState::TailCall(int idx, int nArgs)
{
LuaValue val = stack->Get(idx);
LuaValueArray args;
LuaValueArray results;
if (!val.IsClosure())
{
LuaValue mf = ::GetMetafield(val, "__call", this);
if (mf != LuaValue::Nil)
{
if (mf.IsClosure())
{
args.push_back(std::move(val));
val = mf;
}
}
}
if (val.IsClosure())
{
int prevTop = GetTop();
int prevPc = stack->pc;
ClosurePtr prevClosure = stack->closure;
std::unordered_map<int, UpValue> prevOpenuvs = std::move(stack->openuvs);
LuaValueArray prevVarags = std::move(stack->varargs);
for (int i = idx + 1; i < idx + 1 + nArgs; ++i)
{
args.push_back(stack->Get(i));
}
ClosurePtr c = val.closure;
stack->top = 0;
if (c->proto)
{
int nRegs = c->proto->MaxStackSize;
CheckStack(nRegs);
for (int i = 0; i < c->proto->NumParams; ++i)
stack->Push(args[i]);
for (int i = c->proto->NumParams; i < (int)args.size(); ++i)
stack->varargs.push_back(args[i]);
SetTop(nRegs);
stack->pc = 0;
stack->closure = c;
RunLuaClosure();
results = stack->PopN(stack->top - nRegs);
}
else
{
int nRegs = c->proto->NumParams + LUA_MINSTACK;
CheckStack(nRegs);
for (int i = 0; i < c->proto->NumParams; ++i)
stack->Push(args[i]);
for (int i = c->proto->NumParams; i < (int)args.size(); ++i)
stack->varargs.push_back(args[i]);
SetTop(nRegs);
stack->pc = 0;
stack->closure = c;
int r = c->cFunc(this);
results = stack->PopN(r);
}
SetTop(prevTop);
stack->pc = prevPc;
stack->closure = prevClosure;
stack->openuvs = std::move(prevOpenuvs);
stack->varargs = std::move(prevVarags);
stack->PushN(results, (int)results.size());
}
else
{
panic("not a function");
}
}
void LuaState::PushCClosure(CFunction c, int n)
{
ClosurePtr closure = NewCClosure(c, n);
for(int i = n; i > 0; --i)
{
LuaValuePtr val = stack->Pop();
closure->upvals[i - 1] = UpValue(val);
}
stack->Push(LuaValue(closure));
}
void LuaState::PushCFunction(CFunction c)