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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
|
/*
** Public Lua/C API.
** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/
#define lj_api_c
#define LUA_CORE
#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_debug.h"
#include "lj_str.h"
#include "lj_tab.h"
#include "lj_func.h"
#include "lj_udata.h"
#include "lj_meta.h"
#include "lj_state.h"
#include "lj_bc.h"
#include "lj_frame.h"
#include "lj_trace.h"
#include "lj_vm.h"
#include "lj_strscan.h"
#include "lj_strfmt.h"
/* -- Common helper functions --------------------------------------------- */
#define lj_checkapi_slot(idx) \
lj_checkapi((idx) <= (L->top - L->base), "stack slot %d out of range", (idx))
static TValue *index2adr(lua_State *L, int idx)
{
if (idx > 0) {
TValue *o = L->base + (idx - 1);
return o < L->top ? o : niltv(L);
} else if (idx > LUA_REGISTRYINDEX) {
lj_checkapi(idx != 0 && -idx <= L->top - L->base,
"bad stack slot %d", idx);
return L->top + idx;
} else if (idx == LUA_GLOBALSINDEX) {
TValue *o = &G(L)->tmptv;
settabV(L, o, tabref(L->env));
return o;
} else if (idx == LUA_REGISTRYINDEX) {
return registry(L);
} else {
GCfunc *fn = curr_func(L);
lj_checkapi(fn->c.gct == ~LJ_TFUNC && !isluafunc(fn),
"calling frame is not a C function");
if (idx == LUA_ENVIRONINDEX) {
TValue *o = &G(L)->tmptv;
settabV(L, o, tabref(fn->c.env));
return o;
} else {
idx = LUA_GLOBALSINDEX - idx;
return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
}
}
}
static LJ_AINLINE TValue *index2adr_check(lua_State *L, int idx)
{
TValue *o = index2adr(L, idx);
lj_checkapi(o != niltv(L), "invalid stack slot %d", idx);
return o;
}
static TValue *index2adr_stack(lua_State *L, int idx)
{
if (idx > 0) {
TValue *o = L->base + (idx - 1);
if (o < L->top) {
return o;
} else {
lj_checkapi(0, "invalid stack slot %d", idx);
return niltv(L);
}
return o < L->top ? o : niltv(L);
} else {
lj_checkapi(idx != 0 && -idx <= L->top - L->base,
"invalid stack slot %d", idx);
return L->top + idx;
}
}
static GCtab *getcurrenv(lua_State *L)
{
GCfunc *fn = curr_func(L);
return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
}
/* -- Miscellaneous API functions ----------------------------------------- */
LUA_API int lua_status(lua_State *L)
{
return L->status;
}
LUA_API int lua_checkstack(lua_State *L, int size)
{
if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
return 0; /* Stack overflow. */
} else if (size > 0) {
lj_state_checkstack(L, (MSize)size);
}
return 1;
}
LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
{
if (!lua_checkstack(L, size))
lj_err_callerv(L, LJ_ERR_STKOVM, msg);
}
LUA_API void lua_xmove(lua_State *L, lua_State *to, int n)
{
TValue *f, *t;
if (L == to) return;
lj_checkapi_slot(n);
lj_checkapi(G(L) == G(to), "move across global states");
lj_state_checkstack(to, (MSize)n);
f = L->top;
t = to->top = to->top + n;
while (--n >= 0) copyTV(to, --t, --f);
L->top = f;
}
LUA_API const lua_Number *lua_version(lua_State *L)
{
static const lua_Number version = LUA_VERSION_NUM;
UNUSED(L);
return &version;
}
/* -- Stack manipulation -------------------------------------------------- */
LUA_API int lua_gettop(lua_State *L)
{
return (int)(L->top - L->base);
}
LUA_API void lua_settop(lua_State *L, int idx)
{
if (idx >= 0) {
lj_checkapi(idx <= tvref(L->maxstack) - L->base, "bad stack slot %d", idx);
if (L->base + idx > L->top) {
if (L->base + idx >= tvref(L->maxstack))
lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
do { setnilV(L->top++); } while (L->top < L->base + idx);
} else {
L->top = L->base + idx;
}
} else {
lj_checkapi(-(idx+1) <= (L->top - L->base), "bad stack slot %d", idx);
L->top += idx+1; /* Shrinks top (idx < 0). */
}
}
LUA_API void lua_remove(lua_State *L, int idx)
{
TValue *p = index2adr_stack(L, idx);
while (++p < L->top) copyTV(L, p-1, p);
L->top--;
}
LUA_API void lua_insert(lua_State *L, int idx)
{
TValue *q, *p = index2adr_stack(L, idx);
for (q = L->top; q > p; q--) copyTV(L, q, q-1);
copyTV(L, p, L->top);
}
static void copy_slot(lua_State *L, TValue *f, int idx)
{
if (idx == LUA_GLOBALSINDEX) {
lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
/* NOBARRIER: A thread (i.e. L) is never black. */
setgcref(L->env, obj2gco(tabV(f)));
} else if (idx == LUA_ENVIRONINDEX) {
GCfunc *fn = curr_func(L);
if (fn->c.gct != ~LJ_TFUNC)
lj_err_msg(L, LJ_ERR_NOENV);
lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
setgcref(fn->c.env, obj2gco(tabV(f)));
lj_gc_barrier(L, fn, f);
} else {
TValue *o = index2adr_check(L, idx);
copyTV(L, o, f);
if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
lj_gc_barrier(L, curr_func(L), f);
}
}
LUA_API void lua_replace(lua_State *L, int idx)
{
lj_checkapi_slot(1);
copy_slot(L, L->top - 1, idx);
L->top--;
}
LUA_API void lua_copy(lua_State *L, int fromidx, int toidx)
{
copy_slot(L, index2adr(L, fromidx), toidx);
}
LUA_API void lua_pushvalue(lua_State *L, int idx)
{
copyTV(L, L->top, index2adr(L, idx));
incr_top(L);
}
/* -- Stack getters ------------------------------------------------------- */
LUA_API int lua_type(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
if (tvisnumber(o)) {
return LUA_TNUMBER;
#if LJ_64 && !LJ_GC64
} else if (tvislightud(o)) {
return LUA_TLIGHTUSERDATA;
#endif
} else if (o == niltv(L)) {
return LUA_TNONE;
} else { /* Magic internal/external tag conversion. ORDER LJ_T */
uint32_t t = ~itype(o);
#if LJ_64
int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
#else
int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
#endif
lj_assertL(tt != LUA_TNIL || tvisnil(o), "bad tag conversion");
return tt;
}
}
LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
{
if (lua_type(L, idx) != tt)
lj_err_argt(L, idx, tt);
}
LUALIB_API void luaL_checkany(lua_State *L, int idx)
{
if (index2adr(L, idx) == niltv(L))
lj_err_arg(L, idx, LJ_ERR_NOVAL);
}
LUA_API const char *lua_typename(lua_State *L, int t)
{
UNUSED(L);
return lj_obj_typename[t+1];
}
LUA_API int lua_iscfunction(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
return tvisfunc(o) && !isluafunc(funcV(o));
}
LUA_API int lua_isnumber(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
}
LUA_API int lua_isstring(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
return (tvisstr(o) || tvisnumber(o));
}
LUA_API int lua_isuserdata(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
return (tvisudata(o) || tvislightud(o));
}
LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
{
cTValue *o1 = index2adr(L, idx1);
cTValue *o2 = index2adr(L, idx2);
return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
}
LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
{
cTValue *o1 = index2adr(L, idx1);
cTValue *o2 = index2adr(L, idx2);
if (tvisint(o1) && tvisint(o2)) {
return intV(o1) == intV(o2);
} else if (tvisnumber(o1) && tvisnumber(o2)) {
return numberVnum(o1) == numberVnum(o2);
} else if (itype(o1) != itype(o2)) {
return 0;
} else if (tvispri(o1)) {
return o1 != niltv(L) && o2 != niltv(L);
#if LJ_64 && !LJ_GC64
} else if (tvislightud(o1)) {
return o1->u64 == o2->u64;
#endif
} else if (gcrefeq(o1->gcr, o2->gcr)) {
return 1;
} else if (!tvistabud(o1)) {
return 0;
} else {
TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
if ((uintptr_t)base <= 1) {
return (int)(uintptr_t)base;
} else {
L->top = base+2;
lj_vm_call(L, base, 1+1);
L->top -= 2+LJ_FR2;
return tvistruecond(L->top+1+LJ_FR2);
}
}
}
LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
{
cTValue *o1 = index2adr(L, idx1);
cTValue *o2 = index2adr(L, idx2);
if (o1 == niltv(L) || o2 == niltv(L)) {
return 0;
} else if (tvisint(o1) && tvisint(o2)) {
return intV(o1) < intV(o2);
} else if (tvisnumber(o1) && tvisnumber(o2)) {
return numberVnum(o1) < numberVnum(o2);
} else {
TValue *base = lj_meta_comp(L, o1, o2, 0);
if ((uintptr_t)base <= 1) {
return (int)(uintptr_t)base;
} else {
L->top = base+2;
lj_vm_call(L, base, 1+1);
L->top -= 2+LJ_FR2;
return tvistruecond(L->top+1+LJ_FR2);
}
}
}
LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
if (LJ_LIKELY(tvisnumber(o)))
return numberVnum(o);
else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
return numV(&tmp);
else
return 0;
}
LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
if (LJ_LIKELY(tvisnumber(o))) {
if (ok) *ok = 1;
return numberVnum(o);
} else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) {
if (ok) *ok = 1;
return numV(&tmp);
} else {
if (ok) *ok = 0;
return 0;
}
}
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
if (LJ_LIKELY(tvisnumber(o)))
return numberVnum(o);
else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
lj_err_argt(L, idx, LUA_TNUMBER);
return numV(&tmp);
}
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
if (LJ_LIKELY(tvisnumber(o)))
return numberVnum(o);
else if (tvisnil(o))
return def;
else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
lj_err_argt(L, idx, LUA_TNUMBER);
return numV(&tmp);
}
LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
lua_Number n;
if (LJ_LIKELY(tvisint(o))) {
return intV(o);
} else if (LJ_LIKELY(tvisnum(o))) {
n = numV(o);
} else {
if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
return 0;
if (tvisint(&tmp))
return intV(&tmp);
n = numV(&tmp);
}
#if LJ_64
return (lua_Integer)n;
#else
return lj_num2int(n);
#endif
}
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
lua_Number n;
if (LJ_LIKELY(tvisint(o))) {
if (ok) *ok = 1;
return intV(o);
} else if (LJ_LIKELY(tvisnum(o))) {
n = numV(o);
} else {
if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) {
if (ok) *ok = 0;
return 0;
}
if (tvisint(&tmp)) {
if (ok) *ok = 1;
return intV(&tmp);
}
n = numV(&tmp);
}
if (ok) *ok = 1;
#if LJ_64
return (lua_Integer)n;
#else
return lj_num2int(n);
#endif
}
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
lua_Number n;
if (LJ_LIKELY(tvisint(o))) {
return intV(o);
} else if (LJ_LIKELY(tvisnum(o))) {
n = numV(o);
} else {
if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
lj_err_argt(L, idx, LUA_TNUMBER);
if (tvisint(&tmp))
return (lua_Integer)intV(&tmp);
n = numV(&tmp);
}
#if LJ_64
return (lua_Integer)n;
#else
return lj_num2int(n);
#endif
}
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
{
cTValue *o = index2adr(L, idx);
TValue tmp;
lua_Number n;
if (LJ_LIKELY(tvisint(o))) {
return intV(o);
} else if (LJ_LIKELY(tvisnum(o))) {
n = numV(o);
} else if (tvisnil(o)) {
return def;
} else {
if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
lj_err_argt(L, idx, LUA_TNUMBER);
if (tvisint(&tmp))
return (lua_Integer)intV(&tmp);
n = numV(&tmp);
}
#if LJ_64
return (lua_Integer)n;
#else
return lj_num2int(n);
#endif
}
LUA_API int lua_toboolean(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
return tvistruecond(o);
}
LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
{
TValue *o = index2adr(L, idx);
GCstr *s;
if (LJ_LIKELY(tvisstr(o))) {
s = strV(o);
} else if (tvisnumber(o)) {
lj_gc_check(L);
o = index2adr(L, idx); /* GC may move the stack. */
s = lj_strfmt_number(L, o);
setstrV(L, o, s);
} else {
if (len != NULL) *len = 0;
return NULL;
}
if (len != NULL) *len = s->len;
return strdata(s);
}
LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
{
TValue *o = index2adr(L, idx);
GCstr *s;
if (LJ_LIKELY(tvisstr(o))) {
s = strV(o);
} else if (tvisnumber(o)) {
lj_gc_check(L);
o = index2adr(L, idx); /* GC may move the stack. */
s = lj_strfmt_number(L, o);
setstrV(L, o, s);
} else {
lj_err_argt(L, idx, LUA_TSTRING);
}
if (len != NULL) *len = s->len;
return strdata(s);
}
LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
const char *def, size_t *len)
{
TValue *o = index2adr(L, idx);
GCstr *s;
if (LJ_LIKELY(tvisstr(o))) {
s = strV(o);
} else if (tvisnil(o)) {
if (len != NULL) *len = def ? strlen(def) : 0;
return def;
} else if (tvisnumber(o)) {
lj_gc_check(L);
o = index2adr(L, idx); /* GC may move the stack. */
s = lj_strfmt_number(L, o);
setstrV(L, o, s);
} else {
lj_err_argt(L, idx, LUA_TSTRING);
}
if (len != NULL) *len = s->len;
return strdata(s);
}
LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
const char *const lst[])
{
ptrdiff_t i;
const char *s = lua_tolstring(L, idx, NULL);
if (s == NULL && (s = def) == NULL)
lj_err_argt(L, idx, LUA_TSTRING);
for (i = 0; lst[i]; i++)
if (strcmp(lst[i], s) == 0)
return (int)i;
lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
}
LUA_API size_t lua_objlen(lua_State *L, int idx)
{
TValue *o = index2adr(L, idx);
if (tvisstr(o)) {
return strV(o)->len;
} else if (tvistab(o)) {
return (size_t)lj_tab_len(tabV(o));
} else if (tvisudata(o)) {
return udataV(o)->len;
} else if (tvisnumber(o)) {
GCstr *s = lj_strfmt_number(L, o);
setstrV(L, o, s);
return s->len;
} else {
return 0;
}
}
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
if (tvisfunc(o)) {
BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
if (op == BC_FUNCC || op == BC_FUNCCW)
return funcV(o)->c.f;
}
return NULL;
}
LUA_API void *lua_touserdata(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o))
return uddata(udataV(o));
else if (tvislightud(o))
return lightudV(G(L), o);
else
return NULL;
}
LUA_API lua_State *lua_tothread(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
return (!tvisthread(o)) ? NULL : threadV(o);
}
LUA_API const void *lua_topointer(lua_State *L, int idx)
{
return lj_obj_ptr(G(L), index2adr(L, idx));
}
/* -- Stack setters (object creation) ------------------------------------- */
LUA_API void lua_pushnil(lua_State *L)
{
setnilV(L->top);
incr_top(L);
}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
{
setnumV(L->top, n);
if (LJ_UNLIKELY(tvisnan(L->top)))
setnanV(L->top); /* Canonicalize injected NaNs. */
incr_top(L);
}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
{
setintptrV(L->top, n);
incr_top(L);
}
LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
{
GCstr *s;
lj_gc_check(L);
s = lj_str_new(L, str, len);
setstrV(L, L->top, s);
incr_top(L);
}
LUA_API void lua_pushstring(lua_State *L, const char *str)
{
if (str == NULL) {
setnilV(L->top);
} else {
GCstr *s;
lj_gc_check(L);
s = lj_str_newz(L, str);
setstrV(L, L->top, s);
}
incr_top(L);
}
LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
va_list argp)
{
lj_gc_check(L);
return lj_strfmt_pushvf(L, fmt, argp);
}
LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
{
const char *ret;
va_list argp;
lj_gc_check(L);
va_start(argp, fmt);
ret = lj_strfmt_pushvf(L, fmt, argp);
va_end(argp);
return ret;
}
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
{
GCfunc *fn;
lj_gc_check(L);
lj_checkapi_slot(n);
fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
fn->c.f = f;
L->top -= n;
while (n--)
copyTV(L, &fn->c.upvalue[n], L->top+n);
setfuncV(L, L->top, fn);
lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white");
incr_top(L);
}
LUA_API void lua_pushboolean(lua_State *L, int b)
{
setboolV(L->top, (b != 0));
incr_top(L);
}
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
{
#if LJ_64
p = lj_lightud_intern(L, p);
#endif
setrawlightudV(L->top, p);
incr_top(L);
}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
{
lj_gc_check(L);
settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
incr_top(L);
}
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
{
GCtab *regt = tabV(registry(L));
TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
if (tvisnil(tv)) {
GCtab *mt = lj_tab_new(L, 0, 1);
settabV(L, tv, mt);
settabV(L, L->top++, mt);
lj_gc_anybarriert(L, regt);
return 1;
} else {
copyTV(L, L->top++, tv);
return 0;
}
}
LUA_API int lua_pushthread(lua_State *L)
{
setthreadV(L, L->top, L);
incr_top(L);
return (mainthread(G(L)) == L);
}
LUA_API lua_State *lua_newthread(lua_State *L)
{
lua_State *L1;
lj_gc_check(L);
L1 = lj_state_new(L);
setthreadV(L, L->top, L1);
incr_top(L);
return L1;
}
LUA_API void *lua_newuserdata(lua_State *L, size_t size)
{
GCudata *ud;
lj_gc_check(L);
if (size > LJ_MAX_UDATA)
lj_err_msg(L, LJ_ERR_UDATAOV);
ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
setudataV(L, L->top, ud);
incr_top(L);
return uddata(ud);
}
LUA_API void lua_concat(lua_State *L, int n)
{
lj_checkapi_slot(n);
if (n >= 2) {
n--;
do {
TValue *top = lj_meta_cat(L, L->top-1, -n);
if (top == NULL) {
L->top -= n;
break;
}
n -= (int)(L->top - (top - 2*LJ_FR2));
L->top = top+2;
lj_vm_call(L, top, 1+1);
L->top -= 1+LJ_FR2;
copyTV(L, L->top-1, L->top+LJ_FR2);
} while (--n > 0);
} else if (n == 0) { /* Push empty string. */
setstrV(L, L->top, &G(L)->strempty);
incr_top(L);
}
/* else n == 1: nothing to do. */
}
/* -- Object getters ------------------------------------------------------ */
LUA_API void lua_gettable(lua_State *L, int idx)
{
cTValue *t = index2adr_check(L, idx);
cTValue *v = lj_meta_tget(L, t, L->top-1);
if (v == NULL) {
L->top += 2;
lj_vm_call(L, L->top-2, 1+1);
L->top -= 2+LJ_FR2;
v = L->top+1+LJ_FR2;
}
copyTV(L, L->top-1, v);
}
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
{
cTValue *v, *t = index2adr_check(L, idx);
TValue key;
setstrV(L, &key, lj_str_newz(L, k));
v = lj_meta_tget(L, t, &key);
if (v == NULL) {
L->top += 2;
lj_vm_call(L, L->top-2, 1+1);
L->top -= 2+LJ_FR2;
v = L->top+1+LJ_FR2;
}
copyTV(L, L->top, v);
incr_top(L);
}
LUA_API void lua_rawget(lua_State *L, int idx)
{
cTValue *t = index2adr(L, idx);
lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
}
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
{
cTValue *v, *t = index2adr(L, idx);
lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
v = lj_tab_getint(tabV(t), n);
if (v) {
copyTV(L, L->top, v);
} else {
setnilV(L->top);
}
incr_top(L);
}
LUA_API int lua_getmetatable(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
GCtab *mt = NULL;
if (tvistab(o))
mt = tabref(tabV(o)->metatable);
else if (tvisudata(o))
mt = tabref(udataV(o)->metatable);
else
mt = tabref(basemt_obj(G(L), o));
if (mt == NULL)
return 0;
settabV(L, L->top, mt);
incr_top(L);
return 1;
}
LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
{
if (lua_getmetatable(L, idx)) {
cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
if (tv && !tvisnil(tv)) {
copyTV(L, L->top-1, tv);
return 1;
}
L->top--;
}
return 0;
}
LUA_API void lua_getfenv(lua_State *L, int idx)
{
cTValue *o = index2adr_check(L, idx);
if (tvisfunc(o)) {
settabV(L, L->top, tabref(funcV(o)->c.env));
} else if (tvisudata(o)) {
settabV(L, L->top, tabref(udataV(o)->env));
} else if (tvisthread(o)) {
settabV(L, L->top, tabref(threadV(o)->env));
} else {
setnilV(L->top);
}
incr_top(L);
}
LUA_API int lua_next(lua_State *L, int idx)
{
cTValue *t = index2adr(L, idx);
int more;
lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
more = lj_tab_next(tabV(t), L->top-1, L->top-1);
if (more > 0) {
incr_top(L); /* Return new key and value slot. */
} else if (!more) { /* End of traversal. */
L->top--; /* Remove key slot. */
} else {
lj_err_msg(L, LJ_ERR_NEXTIDX);
}
return more;
}
LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
{
TValue *val;
GCobj *o;
const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val, &o);
if (name) {
copyTV(L, L->top, val);
incr_top(L);
}
return name;
}
LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
{
GCfunc *fn = funcV(index2adr(L, idx));
n--;
lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n);
return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
(void *)&fn->c.upvalue[n];
}
LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
{
GCfunc *fn1 = funcV(index2adr(L, idx1));
GCfunc *fn2 = funcV(index2adr(L, idx2));
n1--; n2--;
lj_checkapi(isluafunc(fn1), "stack slot %d is not a Lua function", idx1);
lj_checkapi(isluafunc(fn2), "stack slot %d is not a Lua function", idx2);
lj_checkapi((uint32_t)n1 < fn1->l.nupvalues, "bad upvalue %d", n1+1);
lj_checkapi((uint32_t)n2 < fn2->l.nupvalues, "bad upvalue %d", n2+1);
setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
}
LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o)) {
GCudata *ud = udataV(o);
cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
return uddata(ud);
}
return NULL; /* value is not a userdata with a metatable */
}
LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
{
void *p = luaL_testudata(L, idx, tname);
if (!p) lj_err_argtype(L, idx, tname);
return p;
}
/* -- Object setters ------------------------------------------------------ */
LUA_API void lua_settable(lua_State *L, int idx)
{
TValue *o;
cTValue *t = index2adr_check(L, idx);
lj_checkapi_slot(2);
o = lj_meta_tset(L, t, L->top-2);
if (o) {
/* NOBARRIER: lj_meta_tset ensures the table is not black. */
L->top -= 2;
copyTV(L, o, L->top+1);
} else {
TValue *base = L->top;
copyTV(L, base+2, base-3-2*LJ_FR2);
L->top = base+3;
lj_vm_call(L, base, 0+1);
L->top -= 3+LJ_FR2;
}
}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
{
TValue *o;
TValue key;
cTValue *t = index2adr_check(L, idx);
lj_checkapi_slot(1);
setstrV(L, &key, lj_str_newz(L, k));
o = lj_meta_tset(L, t, &key);
if (o) {
/* NOBARRIER: lj_meta_tset ensures the table is not black. */
copyTV(L, o, --L->top);
} else {
TValue *base = L->top;
copyTV(L, base+2, base-3-2*LJ_FR2);
L->top = base+3;
lj_vm_call(L, base, 0+1);
L->top -= 2+LJ_FR2;
}
}
LUA_API void lua_rawset(lua_State *L, int idx)
{
GCtab *t = tabV(index2adr(L, idx));
TValue *dst, *key;
lj_checkapi_slot(2);
key = L->top-2;
dst = lj_tab_set(L, t, key);
copyTV(L, dst, key+1);
lj_gc_anybarriert(L, t);
L->top = key;
}
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
{
GCtab *t = tabV(index2adr(L, idx));
TValue *dst, *src;
lj_checkapi_slot(1);
dst = lj_tab_setint(L, t, n);
src = L->top-1;
copyTV(L, dst, src);
lj_gc_barriert(L, t, dst);
L->top = src;
}
LUA_API int lua_setmetatable(lua_State *L, int idx)
{
global_State *g;
GCtab *mt;
cTValue *o = index2adr_check(L, idx);
lj_checkapi_slot(1);
if (tvisnil(L->top-1)) {
mt = NULL;
} else {
lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
mt = tabV(L->top-1);
}
g = G(L);
if (tvistab(o)) {
setgcref(tabV(o)->metatable, obj2gco(mt));
if (mt)
lj_gc_objbarriert(L, tabV(o), mt);
} else if (tvisudata(o)) {
setgcref(udataV(o)->metatable, obj2gco(mt));
if (mt)
lj_gc_objbarrier(L, udataV(o), mt);
} else {
/* Flush cache, since traces specialize to basemt. But not during __gc. */
if (lj_trace_flushall(L))
lj_err_caller(L, LJ_ERR_NOGCMM);
if (tvisbool(o)) {
/* NOBARRIER: basemt is a GC root. */
setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
} else {
/* NOBARRIER: basemt is a GC root. */
setgcref(basemt_obj(g, o), obj2gco(mt));
}
}
L->top--;
return 1;
}
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
{
lua_getfield(L, LUA_REGISTRYINDEX, tname);
lua_setmetatable(L, -2);
}
LUA_API int lua_setfenv(lua_State *L, int idx)
{
cTValue *o = index2adr_check(L, idx);
GCtab *t;
lj_checkapi_slot(1);
lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
t = tabV(L->top-1);
if (tvisfunc(o)) {
setgcref(funcV(o)->c.env, obj2gco(t));
} else if (tvisudata(o)) {
setgcref(udataV(o)->env, obj2gco(t));
} else if (tvisthread(o)) {
setgcref(threadV(o)->env, obj2gco(t));
} else {
L->top--;
return 0;
}
lj_gc_objbarrier(L, gcV(o), t);
L->top--;
return 1;
}
LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
{
cTValue *f = index2adr(L, idx);
TValue *val;
GCobj *o;
const char *name;
lj_checkapi_slot(1);
name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o);
if (name) {
L->top--;
copyTV(L, val, L->top);
lj_gc_barrier(L, o, L->top);
}
return name;
}
/* -- Calls --------------------------------------------------------------- */
#if LJ_FR2
static TValue *api_call_base(lua_State *L, int nargs)
{
TValue *o = L->top, *base = o - nargs;
L->top = o+1;
for (; o > base; o--) copyTV(L, o, o-1);
setnilV(o);
return o+1;
}
#else
#define api_call_base(L, nargs) (L->top - (nargs))
#endif
LUA_API void lua_call(lua_State *L, int nargs, int nresults)
{
lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
"thread called in wrong state %d", L->status);
lj_checkapi_slot(nargs+1);
lj_vm_call(L, api_call_base(L, nargs), nresults+1);
}
LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
{
global_State *g = G(L);
uint8_t oldh = hook_save(g);
ptrdiff_t ef;
int status;
lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
"thread called in wrong state %d", L->status);
lj_checkapi_slot(nargs+1);
if (errfunc == 0) {
ef = 0;
} else {
cTValue *o = index2adr_stack(L, errfunc);
ef = savestack(L, o);
}
status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
if (status) hook_restore(g, oldh);
return status;
}
static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
{
GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
TValue *top = L->top;
fn->c.f = func;
setfuncV(L, top++, fn);
if (LJ_FR2) setnilV(top++);
#if LJ_64
ud = lj_lightud_intern(L, ud);
#endif
setrawlightudV(top++, ud);
cframe_nres(L->cframe) = 1+0; /* Zero results. */
L->top = top;
return top-1; /* Now call the newly allocated C function. */
}
LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
{
global_State *g = G(L);
uint8_t oldh = hook_save(g);
int status;
lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
"thread called in wrong state %d", L->status);
status = lj_vm_cpcall(L, func, ud, cpcall);
if (status) hook_restore(g, oldh);
return status;
}
LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
{
if (luaL_getmetafield(L, idx, field)) {
TValue *top = L->top--;
if (LJ_FR2) setnilV(top++);
copyTV(L, top++, index2adr(L, idx));
L->top = top;
lj_vm_call(L, top-1, 1+1);
return 1;
}
return 0;
}
/* -- Coroutine yield and resume ------------------------------------------ */
LUA_API int lua_isyieldable(lua_State *L)
{
return cframe_canyield(L->cframe);
}
LUA_API int lua_yield(lua_State *L, int nresults)
{
void *cf = L->cframe;
global_State *g = G(L);
if (cframe_canyield(cf)) {
cf = cframe_raw(cf);
if (!hook_active(g)) { /* Regular yield: move results down if needed. */
cTValue *f = L->top - nresults;
if (f > L->base) {
TValue *t = L->base;
while (--nresults >= 0) copyTV(L, t++, f++);
L->top = t;
}
L->cframe = NULL;
L->status = LUA_YIELD;
return -1;
} else { /* Yield from hook: add a pseudo-frame. */
TValue *top = L->top;
hook_leave(g);
(top++)->u64 = cframe_multres(cf);
setcont(top, lj_cont_hook);
if (LJ_FR2) top++;
setframe_pc(top, cframe_pc(cf)-1);
top++;
setframe_gc(top, obj2gco(L), LJ_TTHREAD);
if (LJ_FR2) top++;
setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
L->top = L->base = top+1;
#if ((defined(__GNUC__) || defined(__clang__)) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND) || LJ_TARGET_WINDOWS
lj_err_throw(L, LUA_YIELD);
#else
L->cframe = NULL;
L->status = LUA_YIELD;
lj_vm_unwind_c(cf, LUA_YIELD);
#endif
}
}
lj_err_msg(L, LJ_ERR_CYIELD);
return 0; /* unreachable */
}
LUA_API int lua_resume(lua_State *L, int nargs)
{
if (L->cframe == NULL && L->status <= LUA_YIELD)
return lj_vm_resume(L,
L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs,
0, 0);
L->top = L->base;
setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
incr_top(L);
return LUA_ERRRUN;
}
/* -- GC and memory management -------------------------------------------- */
LUA_API int lua_gc(lua_State *L, int what, int data)
{
global_State *g = G(L);
int res = 0;
switch (what) {
case LUA_GCSTOP:
g->gc.threshold = LJ_MAX_MEM;
break;
case LUA_GCRESTART:
g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
break;
case LUA_GCCOLLECT:
lj_gc_fullgc(L);
break;
case LUA_GCCOUNT:
res = (int)(g->gc.total >> 10);
break;
case LUA_GCCOUNTB:
res = (int)(g->gc.total & 0x3ff);
break;
case LUA_GCSTEP: {
GCSize a = (GCSize)data << 10;
g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
while (g->gc.total >= g->gc.threshold)
if (lj_gc_step(L) > 0) {
res = 1;
break;
}
break;
}
case LUA_GCSETPAUSE:
res = (int)(g->gc.pause);
g->gc.pause = (MSize)data;
break;
case LUA_GCSETSTEPMUL:
res = (int)(g->gc.stepmul);
g->gc.stepmul = (MSize)data;
break;
case LUA_GCISRUNNING:
res = (g->gc.threshold != LJ_MAX_MEM);
break;
default:
res = -1; /* Invalid option. */
}
return res;
}
LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
{
global_State *g = G(L);
if (ud) *ud = g->allocd;
return g->allocf;
}
LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
{
global_State *g = G(L);
g->allocd = ud;
g->allocf = f;
}
|