-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlj_api.c
1200 lines (1101 loc) · 28.3 KB
/
lj_api.c
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
/*
** Public Lua/C API.
** Copyright (C) 2005-2013 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"
/* -- Common helper functions --------------------------------------------- */
#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
#define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
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) {
api_check(L, idx != 0 && -idx <= L->top - L->base);
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);
api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
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 TValue *stkindex2adr(lua_State *L, int idx)
{
if (idx > 0) {
TValue *o = L->base + (idx - 1);
return o < L->top ? o : niltv(L);
} else {
api_check(L, idx != 0 && -idx <= L->top - L->base);
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 *from, lua_State *to, int n)
{
TValue *f, *t;
if (from == to) return;
api_checknelems(from, n);
api_check(from, G(from) == G(to));
lj_state_checkstack(to, (MSize)n);
f = from->top;
t = to->top = to->top + n;
while (--n >= 0) copyTV(to, --t, --f);
from->top = f;
}
/* -- 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) {
api_check(L, idx <= tvref(L->maxstack) - L->base);
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 {
api_check(L, -(idx+1) <= (L->top - L->base));
L->top += idx+1; /* Shrinks top (idx < 0). */
}
}
LUA_API void lua_remove(lua_State *L, int idx)
{
TValue *p = stkindex2adr(L, idx);
api_checkvalidindex(L, p);
while (++p < L->top) copyTV(L, p-1, p);
L->top--;
}
LUA_API void lua_insert(lua_State *L, int idx)
{
TValue *q, *p = stkindex2adr(L, idx);
api_checkvalidindex(L, p);
for (q = L->top; q > p; q--) copyTV(L, q, q-1);
copyTV(L, p, L->top);
}
LUA_API void lua_replace(lua_State *L, int idx)
{
api_checknelems(L, 1);
if (idx == LUA_GLOBALSINDEX) {
api_check(L, tvistab(L->top-1));
/* NOBARRIER: A thread (i.e. L) is never black. */
setgcref(L->env, obj2gco(tabV(L->top-1)));
} else if (idx == LUA_ENVIRONINDEX) {
GCfunc *fn = curr_func(L);
if (fn->c.gct != ~LJ_TFUNC)
lj_err_msg(L, LJ_ERR_NOENV);
api_check(L, tvistab(L->top-1));
setgcref(fn->c.env, obj2gco(tabV(L->top-1)));
lj_gc_barrier(L, fn, L->top-1);
} else {
TValue *o = index2adr(L, idx);
api_checkvalidindex(L, o);
copyTV(L, o, L->top-1);
if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
lj_gc_barrier(L, curr_func(L), L->top-1);
}
L->top--;
}
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
} 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
lua_assert(tt != LUA_TNIL || tvisnil(o));
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
} 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;
return tvistruecond(L->top+1);
}
}
}
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;
return tvistruecond(L->top+1);
}
}
}
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;
}
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 (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_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_str_fromnumber(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_str_fromnumber(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_str_fromnumber(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_str_fromnumber(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(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)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o))
return uddata(udataV(o));
else if (tvislightud(o))
return lightudV(o);
else if (tviscdata(o))
return cdataptr(cdataV(o));
else if (tvisgcv(o))
return gcV(o);
else
return NULL;
}
/* -- 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_str_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_str_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);
api_checknelems(L, 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);
lua_assert(iswhite(obj2gco(fn)));
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)
{
setlightudV(L->top, checklightudptr(L, p));
incr_top(L);
}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
{
GCtab *t;
lj_gc_check(L);
t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
settabV(L, L->top, t);
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)
{
api_checknelems(L, 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);
L->top = top+2;
lj_vm_call(L, top, 1+1);
L->top--;
copyTV(L, L->top-1, L->top);
} 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 *v, *t = index2adr(L, idx);
api_checkvalidindex(L, t);
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;
v = L->top+1;
}
copyTV(L, L->top-1, v);
}
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
{
cTValue *v, *t = index2adr(L, idx);
TValue key;
api_checkvalidindex(L, t);
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;
v = L->top+1;
}
copyTV(L, L->top, v);
incr_top(L);
}
LUA_API void lua_rawget(lua_State *L, int idx)
{
cTValue *t = index2adr(L, idx);
api_check(L, tvistab(t));
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);
api_check(L, tvistab(t));
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(L, idx);
api_checkvalidindex(L, o);
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;
api_check(L, tvistab(t));
more = lj_tab_next(L, tabV(t), L->top-1);
if (more) {
incr_top(L); /* Return new key and value slot. */
} else { /* End of traversal. */
L->top--; /* Remove key slot. */
}
return more;
}
LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
{
TValue *val;
const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
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--;
api_check(L, (uint32_t)n < fn->l.nupvalues);
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--;
api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);
api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);
setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
}
LUALIB_API void *luaL_checkudata(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);
}
lj_err_argtype(L, idx, tname);
return NULL; /* unreachable */
}
/* -- Object setters ------------------------------------------------------ */
LUA_API void lua_settable(lua_State *L, int idx)
{
TValue *o;
cTValue *t = index2adr(L, idx);
api_checknelems(L, 2);
api_checkvalidindex(L, t);
o = lj_meta_tset(L, t, L->top-2);
if (o) {
/* NOBARRIER: lj_meta_tset ensures the table is not black. */
copyTV(L, o, L->top-1);
L->top -= 2;
} else {
L->top += 3;
copyTV(L, L->top-1, L->top-6);
lj_vm_call(L, L->top-3, 0+1);
L->top -= 3;
}
}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
{
TValue *o;
TValue key;
cTValue *t = index2adr(L, idx);
api_checknelems(L, 1);
api_checkvalidindex(L, t);
setstrV(L, &key, lj_str_newz(L, k));
o = lj_meta_tset(L, t, &key);
if (o) {
L->top--;
/* NOBARRIER: lj_meta_tset ensures the table is not black. */
copyTV(L, o, L->top);
} else {
L->top += 3;
copyTV(L, L->top-1, L->top-6);
lj_vm_call(L, L->top-3, 0+1);
L->top -= 2;
}
}
LUA_API void lua_rawset(lua_State *L, int idx)
{
GCtab *t = tabV(index2adr(L, idx));
TValue *dst, *key;
api_checknelems(L, 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;
api_checknelems(L, 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(L, idx);
api_checknelems(L, 1);
api_checkvalidindex(L, o);
if (tvisnil(L->top-1)) {
mt = NULL;
} else {
api_check(L, tvistab(L->top-1));
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;
}
LUA_API int lua_setfenv(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
GCtab *t;
api_checknelems(L, 1);
api_checkvalidindex(L, o);
api_check(L, tvistab(L->top-1));
t = tabV(L->top-1);
if (tvisfunc(o)) {
setgcref(funcV(o)->c.env, obj2gco(t));
} else if (tvisudata(o)) {