3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
27 #include "lj_bcdump.h"
30 /* -- Common helper functions --------------------------------------------- */
32 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
33 #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
35 static TValue
*index2adr(lua_State
*L
, int idx
)
38 TValue
*o
= L
->base
+ (idx
- 1);
39 return o
< L
->top
? o
: niltv(L
);
40 } else if (idx
> LUA_REGISTRYINDEX
) {
41 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
43 } else if (idx
== LUA_GLOBALSINDEX
) {
44 TValue
*o
= &G(L
)->tmptv
;
45 settabV(L
, o
, tabref(L
->env
));
47 } else if (idx
== LUA_REGISTRYINDEX
) {
50 GCfunc
*fn
= curr_func(L
);
51 api_check(L
, fn
->c
.gct
== ~LJ_TFUNC
&& !isluafunc(fn
));
52 if (idx
== LUA_ENVIRONINDEX
) {
53 TValue
*o
= &G(L
)->tmptv
;
54 settabV(L
, o
, tabref(fn
->c
.env
));
57 idx
= LUA_GLOBALSINDEX
- idx
;
58 return idx
<= fn
->c
.nupvalues
? &fn
->c
.upvalue
[idx
-1] : niltv(L
);
63 static TValue
*stkindex2adr(lua_State
*L
, int idx
)
66 TValue
*o
= L
->base
+ (idx
- 1);
67 return o
< L
->top
? o
: niltv(L
);
69 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
74 static GCtab
*getcurrenv(lua_State
*L
)
76 GCfunc
*fn
= curr_func(L
);
77 return fn
->c
.gct
== ~LJ_TFUNC
? tabref(fn
->c
.env
) : tabref(L
->env
);
80 /* -- Miscellaneous API functions ----------------------------------------- */
82 LUA_API
int lua_status(lua_State
*L
)
87 LUA_API
int lua_checkstack(lua_State
*L
, int size
)
89 if (size
> LUAI_MAXCSTACK
|| (L
->top
- L
->base
+ size
) > LUAI_MAXCSTACK
) {
90 return 0; /* Stack overflow. */
91 } else if (size
> 0) {
92 lj_state_checkstack(L
, (MSize
)size
);
97 LUALIB_API
void luaL_checkstack(lua_State
*L
, int size
, const char *msg
)
99 if (!lua_checkstack(L
, size
))
100 lj_err_callerv(L
, LJ_ERR_STKOVM
, msg
);
103 LUA_API
void lua_xmove(lua_State
*from
, lua_State
*to
, int n
)
106 if (from
== to
) return;
107 api_checknelems(from
, n
);
108 api_check(from
, G(from
) == G(to
));
109 lj_state_checkstack(to
, (MSize
)n
);
111 t
= to
->top
= to
->top
+ n
;
112 while (--n
>= 0) copyTV(to
, --t
, --f
);
116 /* -- Stack manipulation -------------------------------------------------- */
118 LUA_API
int lua_gettop(lua_State
*L
)
120 return (int)(L
->top
- L
->base
);
123 LUA_API
void lua_settop(lua_State
*L
, int idx
)
126 api_check(L
, idx
<= tvref(L
->maxstack
) - L
->base
);
127 if (L
->base
+ idx
> L
->top
) {
128 if (L
->base
+ idx
>= tvref(L
->maxstack
))
129 lj_state_growstack(L
, (MSize
)idx
- (MSize
)(L
->top
- L
->base
));
130 do { setnilV(L
->top
++); } while (L
->top
< L
->base
+ idx
);
132 L
->top
= L
->base
+ idx
;
135 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
136 L
->top
+= idx
+1; /* Shrinks top (idx < 0). */
140 LUA_API
void lua_remove(lua_State
*L
, int idx
)
142 TValue
*p
= stkindex2adr(L
, idx
);
143 api_checkvalidindex(L
, p
);
144 while (++p
< L
->top
) copyTV(L
, p
-1, p
);
148 LUA_API
void lua_insert(lua_State
*L
, int idx
)
150 TValue
*q
, *p
= stkindex2adr(L
, idx
);
151 api_checkvalidindex(L
, p
);
152 for (q
= L
->top
; q
> p
; q
--) copyTV(L
, q
, q
-1);
153 copyTV(L
, p
, L
->top
);
156 LUA_API
void lua_replace(lua_State
*L
, int idx
)
158 api_checknelems(L
, 1);
159 if (idx
== LUA_GLOBALSINDEX
) {
160 api_check(L
, tvistab(L
->top
-1));
161 /* NOBARRIER: A thread (i.e. L) is never black. */
162 setgcref(L
->env
, obj2gco(tabV(L
->top
-1)));
163 } else if (idx
== LUA_ENVIRONINDEX
) {
164 GCfunc
*fn
= curr_func(L
);
165 if (fn
->c
.gct
!= ~LJ_TFUNC
)
166 lj_err_msg(L
, LJ_ERR_NOENV
);
167 api_check(L
, tvistab(L
->top
-1));
168 setgcref(fn
->c
.env
, obj2gco(tabV(L
->top
-1)));
169 lj_gc_barrier(L
, fn
, L
->top
-1);
171 TValue
*o
= index2adr(L
, idx
);
172 api_checkvalidindex(L
, o
);
173 copyTV(L
, o
, L
->top
-1);
174 if (idx
< LUA_GLOBALSINDEX
) /* Need a barrier for upvalues. */
175 lj_gc_barrier(L
, curr_func(L
), L
->top
-1);
180 LUA_API
void lua_pushvalue(lua_State
*L
, int idx
)
182 copyTV(L
, L
->top
, index2adr(L
, idx
));
186 /* -- Stack getters ------------------------------------------------------- */
188 LUA_API
int lua_type(lua_State
*L
, int idx
)
190 cTValue
*o
= index2adr(L
, idx
);
194 } else if (tvislightud(o
)) {
195 return LUA_TLIGHTUSERDATA
;
197 } else if (o
== niltv(L
)) {
199 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
200 uint32_t t
= ~itype(o
);
202 int tt
= (int)((U64x(75a06
,98042110) >> 4*t
) & 15u);
204 int tt
= (int)(((t
< 8 ? 0x98042110u
: 0x75a06u
) >> 4*(t
&7)) & 15u);
206 lua_assert(tt
!= LUA_TNIL
|| tvisnil(o
));
211 LUALIB_API
void luaL_checktype(lua_State
*L
, int idx
, int tt
)
213 if (lua_type(L
, idx
) != tt
)
214 lj_err_argt(L
, idx
, tt
);
217 LUALIB_API
void luaL_checkany(lua_State
*L
, int idx
)
219 if (index2adr(L
, idx
) == niltv(L
))
220 lj_err_arg(L
, idx
, LJ_ERR_NOVAL
);
223 LUA_API
const char *lua_typename(lua_State
*L
, int t
)
226 return lj_obj_typename
[t
+1];
229 LUA_API
int lua_iscfunction(lua_State
*L
, int idx
)
231 cTValue
*o
= index2adr(L
, idx
);
232 return tvisfunc(o
) && !isluafunc(funcV(o
));
235 LUA_API
int lua_isnumber(lua_State
*L
, int idx
)
237 cTValue
*o
= index2adr(L
, idx
);
239 return (tvisnumber(o
) || (tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)));
242 LUA_API
int lua_isstring(lua_State
*L
, int idx
)
244 cTValue
*o
= index2adr(L
, idx
);
245 return (tvisstr(o
) || tvisnumber(o
));
248 LUA_API
int lua_isuserdata(lua_State
*L
, int idx
)
250 cTValue
*o
= index2adr(L
, idx
);
251 return (tvisudata(o
) || tvislightud(o
));
254 LUA_API
int lua_rawequal(lua_State
*L
, int idx1
, int idx2
)
256 cTValue
*o1
= index2adr(L
, idx1
);
257 cTValue
*o2
= index2adr(L
, idx2
);
258 return (o1
== niltv(L
) || o2
== niltv(L
)) ? 0 : lj_obj_equal(o1
, o2
);
261 LUA_API
int lua_equal(lua_State
*L
, int idx1
, int idx2
)
263 cTValue
*o1
= index2adr(L
, idx1
);
264 cTValue
*o2
= index2adr(L
, idx2
);
265 if (tvisint(o1
) && tvisint(o2
)) {
266 return intV(o1
) == intV(o2
);
267 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
268 return numberVnum(o1
) == numberVnum(o2
);
269 } else if (itype(o1
) != itype(o2
)) {
271 } else if (tvispri(o1
)) {
272 return o1
!= niltv(L
) && o2
!= niltv(L
);
274 } else if (tvislightud(o1
)) {
275 return o1
->u64
== o2
->u64
;
277 } else if (gcrefeq(o1
->gcr
, o2
->gcr
)) {
279 } else if (!tvistabud(o1
)) {
282 TValue
*base
= lj_meta_equal(L
, gcV(o1
), gcV(o2
), 0);
283 if ((uintptr_t)base
<= 1) {
284 return (int)(uintptr_t)base
;
287 lj_vm_call(L
, base
, 1+1);
289 return tvistruecond(L
->top
+1);
294 LUA_API
int lua_lessthan(lua_State
*L
, int idx1
, int idx2
)
296 cTValue
*o1
= index2adr(L
, idx1
);
297 cTValue
*o2
= index2adr(L
, idx2
);
298 if (o1
== niltv(L
) || o2
== niltv(L
)) {
300 } else if (tvisint(o1
) && tvisint(o2
)) {
301 return intV(o1
) < intV(o2
);
302 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
303 return numberVnum(o1
) < numberVnum(o2
);
305 TValue
*base
= lj_meta_comp(L
, o1
, o2
, 0);
306 if ((uintptr_t)base
<= 1) {
307 return (int)(uintptr_t)base
;
310 lj_vm_call(L
, base
, 1+1);
312 return tvistruecond(L
->top
+1);
317 LUA_API lua_Number
lua_tonumber(lua_State
*L
, int idx
)
319 cTValue
*o
= index2adr(L
, idx
);
321 if (LJ_LIKELY(tvisnumber(o
)))
322 return numberVnum(o
);
323 else if (tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
))
329 LUALIB_API lua_Number
luaL_checknumber(lua_State
*L
, int idx
)
331 cTValue
*o
= index2adr(L
, idx
);
333 if (LJ_LIKELY(tvisnumber(o
)))
334 return numberVnum(o
);
335 else if (!(tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
)))
336 lj_err_argt(L
, idx
, LUA_TNUMBER
);
340 LUALIB_API lua_Number
luaL_optnumber(lua_State
*L
, int idx
, lua_Number def
)
342 cTValue
*o
= index2adr(L
, idx
);
344 if (LJ_LIKELY(tvisnumber(o
)))
345 return numberVnum(o
);
348 else if (!(tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
)))
349 lj_err_argt(L
, idx
, LUA_TNUMBER
);
353 LUA_API lua_Integer
lua_tointeger(lua_State
*L
, int idx
)
355 cTValue
*o
= index2adr(L
, idx
);
358 if (LJ_LIKELY(tvisint(o
))) {
360 } else if (LJ_LIKELY(tvisnum(o
))) {
363 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
366 return (lua_Integer
)intV(&tmp
);
370 return (lua_Integer
)n
;
372 return lj_num2int(n
);
376 LUALIB_API lua_Integer
luaL_checkinteger(lua_State
*L
, int idx
)
378 cTValue
*o
= index2adr(L
, idx
);
381 if (LJ_LIKELY(tvisint(o
))) {
383 } else if (LJ_LIKELY(tvisnum(o
))) {
386 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
387 lj_err_argt(L
, idx
, LUA_TNUMBER
);
389 return (lua_Integer
)intV(&tmp
);
393 return (lua_Integer
)n
;
395 return lj_num2int(n
);
399 LUALIB_API lua_Integer
luaL_optinteger(lua_State
*L
, int idx
, lua_Integer def
)
401 cTValue
*o
= index2adr(L
, idx
);
404 if (LJ_LIKELY(tvisint(o
))) {
406 } else if (LJ_LIKELY(tvisnum(o
))) {
408 } else if (tvisnil(o
)) {
411 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
412 lj_err_argt(L
, idx
, LUA_TNUMBER
);
414 return (lua_Integer
)intV(&tmp
);
418 return (lua_Integer
)n
;
420 return lj_num2int(n
);
424 LUA_API
int lua_toboolean(lua_State
*L
, int idx
)
426 cTValue
*o
= index2adr(L
, idx
);
427 return tvistruecond(o
);
430 LUA_API
const char *lua_tolstring(lua_State
*L
, int idx
, size_t *len
)
432 TValue
*o
= index2adr(L
, idx
);
434 if (LJ_LIKELY(tvisstr(o
))) {
436 } else if (tvisnumber(o
)) {
438 o
= index2adr(L
, idx
); /* GC may move the stack. */
439 s
= lj_str_fromnumber(L
, o
);
441 if (len
!= NULL
) *len
= 0;
444 if (len
!= NULL
) *len
= s
->len
;
448 LUALIB_API
const char *luaL_checklstring(lua_State
*L
, int idx
, size_t *len
)
450 TValue
*o
= index2adr(L
, idx
);
452 if (LJ_LIKELY(tvisstr(o
))) {
454 } else if (tvisnumber(o
)) {
456 o
= index2adr(L
, idx
); /* GC may move the stack. */
457 s
= lj_str_fromnumber(L
, o
);
459 lj_err_argt(L
, idx
, LUA_TSTRING
);
461 if (len
!= NULL
) *len
= s
->len
;
465 LUALIB_API
const char *luaL_optlstring(lua_State
*L
, int idx
,
466 const char *def
, size_t *len
)
468 TValue
*o
= index2adr(L
, idx
);
470 if (LJ_LIKELY(tvisstr(o
))) {
472 } else if (tvisnil(o
)) {
473 if (len
!= NULL
) *len
= def
? strlen(def
) : 0;
475 } else if (tvisnumber(o
)) {
477 o
= index2adr(L
, idx
); /* GC may move the stack. */
478 s
= lj_str_fromnumber(L
, o
);
480 lj_err_argt(L
, idx
, LUA_TSTRING
);
482 if (len
!= NULL
) *len
= s
->len
;
486 LUALIB_API
int luaL_checkoption(lua_State
*L
, int idx
, const char *def
,
487 const char *const lst
[])
490 const char *s
= lua_tolstring(L
, idx
, NULL
);
491 if (s
== NULL
&& (s
= def
) == NULL
)
492 lj_err_argt(L
, idx
, LUA_TSTRING
);
493 for (i
= 0; lst
[i
]; i
++)
494 if (strcmp(lst
[i
], s
) == 0)
496 lj_err_argv(L
, idx
, LJ_ERR_INVOPTM
, s
);
499 LUA_API
size_t lua_objlen(lua_State
*L
, int idx
)
501 TValue
*o
= index2adr(L
, idx
);
505 return (size_t)lj_tab_len(tabV(o
));
506 else if (tvisudata(o
))
507 return udataV(o
)->len
;
508 else if (tvisnumber(o
))
509 return lj_str_fromnumber(L
, o
)->len
;
514 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
)
516 cTValue
*o
= index2adr(L
, idx
);
518 BCOp op
= bc_op(*mref(funcV(o
)->c
.pc
, BCIns
));
519 if (op
== BC_FUNCC
|| op
== BC_FUNCCW
)
520 return funcV(o
)->c
.f
;
525 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
)
527 cTValue
*o
= index2adr(L
, idx
);
529 return uddata(udataV(o
));
530 else if (tvislightud(o
))
536 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
)
538 cTValue
*o
= index2adr(L
, idx
);
539 return (!tvisthread(o
)) ? NULL
: threadV(o
);
542 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
)
544 cTValue
*o
= index2adr(L
, idx
);
546 return uddata(udataV(o
));
547 else if (tvislightud(o
))
549 else if (tviscdata(o
))
550 return cdataptr(cdataV(o
));
557 /* -- Stack setters (object creation) ------------------------------------- */
559 LUA_API
void lua_pushnil(lua_State
*L
)
565 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
)
568 if (LJ_UNLIKELY(tvisnan(L
->top
)))
569 setnanV(L
->top
); /* Canonicalize injected NaNs. */
573 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
)
575 setintptrV(L
->top
, n
);
579 LUA_API
void lua_pushlstring(lua_State
*L
, const char *str
, size_t len
)
583 s
= lj_str_new(L
, str
, len
);
584 setstrV(L
, L
->top
, s
);
588 LUA_API
void lua_pushstring(lua_State
*L
, const char *str
)
595 s
= lj_str_newz(L
, str
);
596 setstrV(L
, L
->top
, s
);
601 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
605 return lj_str_pushvf(L
, fmt
, argp
);
608 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...)
614 ret
= lj_str_pushvf(L
, fmt
, argp
);
619 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction f
, int n
)
623 api_checknelems(L
, n
);
624 fn
= lj_func_newC(L
, (MSize
)n
, getcurrenv(L
));
628 copyTV(L
, &fn
->c
.upvalue
[n
], L
->top
+n
);
629 setfuncV(L
, L
->top
, fn
);
630 lua_assert(iswhite(obj2gco(fn
)));
634 LUA_API
void lua_pushboolean(lua_State
*L
, int b
)
636 setboolV(L
->top
, (b
!= 0));
640 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
)
642 setlightudV(L
->top
, checklightudptr(L
, p
));
646 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
)
650 t
= lj_tab_new(L
, (uint32_t)(narray
> 0 ? narray
+1 : 0), hsize2hbits(nrec
));
651 settabV(L
, L
->top
, t
);
655 LUALIB_API
int luaL_newmetatable(lua_State
*L
, const char *tname
)
657 GCtab
*regt
= tabV(registry(L
));
658 TValue
*tv
= lj_tab_setstr(L
, regt
, lj_str_newz(L
, tname
));
660 GCtab
*mt
= lj_tab_new(L
, 0, 1);
662 settabV(L
, L
->top
++, mt
);
663 lj_gc_anybarriert(L
, regt
);
666 copyTV(L
, L
->top
++, tv
);
671 LUA_API
int lua_pushthread(lua_State
*L
)
673 setthreadV(L
, L
->top
, L
);
675 return (mainthread(G(L
)) == L
);
678 LUA_API lua_State
*lua_newthread(lua_State
*L
)
682 L1
= lj_state_new(L
);
683 setthreadV(L
, L
->top
, L1
);
688 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
)
692 if (size
> LJ_MAX_UDATA
)
693 lj_err_msg(L
, LJ_ERR_UDATAOV
);
694 ud
= lj_udata_new(L
, (MSize
)size
, getcurrenv(L
));
695 setudataV(L
, L
->top
, ud
);
700 LUA_API
void lua_concat(lua_State
*L
, int n
)
702 api_checknelems(L
, n
);
706 TValue
*top
= lj_meta_cat(L
, L
->top
-1, n
);
711 n
-= (int)(L
->top
- top
);
713 lj_vm_call(L
, top
, 1+1);
715 copyTV(L
, L
->top
-1, L
->top
);
717 } else if (n
== 0) { /* Push empty string. */
718 setstrV(L
, L
->top
, &G(L
)->strempty
);
721 /* else n == 1: nothing to do. */
724 /* -- Object getters ------------------------------------------------------ */
726 LUA_API
void lua_gettable(lua_State
*L
, int idx
)
728 cTValue
*v
, *t
= index2adr(L
, idx
);
729 api_checkvalidindex(L
, t
);
730 v
= lj_meta_tget(L
, t
, L
->top
-1);
733 lj_vm_call(L
, L
->top
-2, 1+1);
737 copyTV(L
, L
->top
-1, v
);
740 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
)
742 cTValue
*v
, *t
= index2adr(L
, idx
);
744 api_checkvalidindex(L
, t
);
745 setstrV(L
, &key
, lj_str_newz(L
, k
));
746 v
= lj_meta_tget(L
, t
, &key
);
749 lj_vm_call(L
, L
->top
-2, 1+1);
753 copyTV(L
, L
->top
, v
);
757 LUA_API
void lua_rawget(lua_State
*L
, int idx
)
759 cTValue
*t
= index2adr(L
, idx
);
760 api_check(L
, tvistab(t
));
761 copyTV(L
, L
->top
-1, lj_tab_get(L
, tabV(t
), L
->top
-1));
764 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
)
766 cTValue
*v
, *t
= index2adr(L
, idx
);
767 api_check(L
, tvistab(t
));
768 v
= lj_tab_getint(tabV(t
), n
);
770 copyTV(L
, L
->top
, v
);
777 LUA_API
int lua_getmetatable(lua_State
*L
, int idx
)
779 cTValue
*o
= index2adr(L
, idx
);
782 mt
= tabref(tabV(o
)->metatable
);
783 else if (tvisudata(o
))
784 mt
= tabref(udataV(o
)->metatable
);
786 mt
= tabref(basemt_obj(G(L
), o
));
789 settabV(L
, L
->top
, mt
);
794 LUALIB_API
int luaL_getmetafield(lua_State
*L
, int idx
, const char *field
)
796 if (lua_getmetatable(L
, idx
)) {
797 cTValue
*tv
= lj_tab_getstr(tabV(L
->top
-1), lj_str_newz(L
, field
));
798 if (tv
&& !tvisnil(tv
)) {
799 copyTV(L
, L
->top
-1, tv
);
807 LUA_API
void lua_getfenv(lua_State
*L
, int idx
)
809 cTValue
*o
= index2adr(L
, idx
);
810 api_checkvalidindex(L
, o
);
812 settabV(L
, L
->top
, tabref(funcV(o
)->c
.env
));
813 } else if (tvisudata(o
)) {
814 settabV(L
, L
->top
, tabref(udataV(o
)->env
));
815 } else if (tvisthread(o
)) {
816 settabV(L
, L
->top
, tabref(threadV(o
)->env
));
823 LUA_API
int lua_next(lua_State
*L
, int idx
)
825 cTValue
*t
= index2adr(L
, idx
);
827 api_check(L
, tvistab(t
));
828 more
= lj_tab_next(L
, tabV(t
), L
->top
-1);
830 incr_top(L
); /* Return new key and value slot. */
831 } else { /* End of traversal. */
832 L
->top
--; /* Remove key slot. */
837 LUA_API
const char *lua_getupvalue(lua_State
*L
, int idx
, int n
)
840 const char *name
= lj_debug_uvnamev(index2adr(L
, idx
), (uint32_t)(n
-1), &val
);
842 copyTV(L
, L
->top
, val
);
848 LUALIB_API
void *luaL_checkudata(lua_State
*L
, int idx
, const char *tname
)
850 cTValue
*o
= index2adr(L
, idx
);
852 GCudata
*ud
= udataV(o
);
853 cTValue
*tv
= lj_tab_getstr(tabV(registry(L
)), lj_str_newz(L
, tname
));
854 if (tv
&& tvistab(tv
) && tabV(tv
) == tabref(ud
->metatable
))
857 lj_err_argtype(L
, idx
, tname
);
858 return NULL
; /* unreachable */
861 /* -- Object setters ------------------------------------------------------ */
863 LUA_API
void lua_settable(lua_State
*L
, int idx
)
866 cTValue
*t
= index2adr(L
, idx
);
867 api_checknelems(L
, 2);
868 api_checkvalidindex(L
, t
);
869 o
= lj_meta_tset(L
, t
, L
->top
-2);
871 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
872 copyTV(L
, o
, L
->top
-1);
876 copyTV(L
, L
->top
-1, L
->top
-6);
877 lj_vm_call(L
, L
->top
-3, 0+1);
882 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
)
886 cTValue
*t
= index2adr(L
, idx
);
887 api_checknelems(L
, 1);
888 api_checkvalidindex(L
, t
);
889 setstrV(L
, &key
, lj_str_newz(L
, k
));
890 o
= lj_meta_tset(L
, t
, &key
);
893 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
894 copyTV(L
, o
, L
->top
);
897 copyTV(L
, L
->top
-1, L
->top
-6);
898 lj_vm_call(L
, L
->top
-3, 0+1);
903 LUA_API
void lua_rawset(lua_State
*L
, int idx
)
905 GCtab
*t
= tabV(index2adr(L
, idx
));
907 api_checknelems(L
, 2);
909 dst
= lj_tab_set(L
, t
, key
);
910 copyTV(L
, dst
, key
+1);
911 lj_gc_anybarriert(L
, t
);
915 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
)
917 GCtab
*t
= tabV(index2adr(L
, idx
));
919 api_checknelems(L
, 1);
920 dst
= lj_tab_setint(L
, t
, n
);
923 lj_gc_barriert(L
, t
, dst
);
927 LUA_API
int lua_setmetatable(lua_State
*L
, int idx
)
931 cTValue
*o
= index2adr(L
, idx
);
932 api_checknelems(L
, 1);
933 api_checkvalidindex(L
, o
);
934 if (tvisnil(L
->top
-1)) {
937 api_check(L
, tvistab(L
->top
-1));
942 setgcref(tabV(o
)->metatable
, obj2gco(mt
));
944 lj_gc_objbarriert(L
, tabV(o
), mt
);
945 } else if (tvisudata(o
)) {
946 setgcref(udataV(o
)->metatable
, obj2gco(mt
));
948 lj_gc_objbarrier(L
, udataV(o
), mt
);
950 /* Flush cache, since traces specialize to basemt. But not during __gc. */
951 if (lj_trace_flushall(L
))
952 lj_err_caller(L
, LJ_ERR_NOGCMM
);
954 /* NOBARRIER: basemt is a GC root. */
955 setgcref(basemt_it(g
, LJ_TTRUE
), obj2gco(mt
));
956 setgcref(basemt_it(g
, LJ_TFALSE
), obj2gco(mt
));
958 /* NOBARRIER: basemt is a GC root. */
959 setgcref(basemt_obj(g
, o
), obj2gco(mt
));
966 LUA_API
int lua_setfenv(lua_State
*L
, int idx
)
968 cTValue
*o
= index2adr(L
, idx
);
970 api_checknelems(L
, 1);
971 api_checkvalidindex(L
, o
);
972 api_check(L
, tvistab(L
->top
-1));
975 setgcref(funcV(o
)->c
.env
, obj2gco(t
));
976 } else if (tvisudata(o
)) {
977 setgcref(udataV(o
)->env
, obj2gco(t
));
978 } else if (tvisthread(o
)) {
979 setgcref(threadV(o
)->env
, obj2gco(t
));
984 lj_gc_objbarrier(L
, gcV(o
), t
);
989 LUA_API
const char *lua_setupvalue(lua_State
*L
, int idx
, int n
)
991 cTValue
*f
= index2adr(L
, idx
);
994 api_checknelems(L
, 1);
995 name
= lj_debug_uvnamev(f
, (uint32_t)(n
-1), &val
);
998 copyTV(L
, val
, L
->top
);
999 lj_gc_barrier(L
, funcV(f
), L
->top
);
1004 /* -- Calls --------------------------------------------------------------- */
1006 LUA_API
void lua_call(lua_State
*L
, int nargs
, int nresults
)
1008 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1009 api_checknelems(L
, nargs
+1);
1010 lj_vm_call(L
, L
->top
- nargs
, nresults
+1);
1013 LUA_API
int lua_pcall(lua_State
*L
, int nargs
, int nresults
, int errfunc
)
1015 global_State
*g
= G(L
);
1016 uint8_t oldh
= hook_save(g
);
1019 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1020 api_checknelems(L
, nargs
+1);
1024 cTValue
*o
= stkindex2adr(L
, errfunc
);
1025 api_checkvalidindex(L
, o
);
1026 ef
= savestack(L
, o
);
1028 status
= lj_vm_pcall(L
, L
->top
- nargs
, nresults
+1, ef
);
1029 if (status
) hook_restore(g
, oldh
);
1033 static TValue
*cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1035 GCfunc
*fn
= lj_func_newC(L
, 0, getcurrenv(L
));
1037 setfuncV(L
, L
->top
, fn
);
1038 setlightudV(L
->top
+1, checklightudptr(L
, ud
));
1039 cframe_nres(L
->cframe
) = 1+0; /* Zero results. */
1041 return L
->top
-1; /* Now call the newly allocated C function. */
1044 LUA_API
int lua_cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1046 global_State
*g
= G(L
);
1047 uint8_t oldh
= hook_save(g
);
1049 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1050 status
= lj_vm_cpcall(L
, func
, ud
, cpcall
);
1051 if (status
) hook_restore(g
, oldh
);
1055 LUALIB_API
int luaL_callmeta(lua_State
*L
, int idx
, const char *field
)
1057 if (luaL_getmetafield(L
, idx
, field
)) {
1058 TValue
*base
= L
->top
--;
1059 copyTV(L
, base
, index2adr(L
, idx
));
1061 lj_vm_call(L
, base
, 1+1);
1067 /* -- Coroutine yield and resume ------------------------------------------ */
1069 LUA_API
int lua_yield(lua_State
*L
, int nresults
)
1071 void *cf
= L
->cframe
;
1072 global_State
*g
= G(L
);
1073 if (cframe_canyield(cf
)) {
1074 cf
= cframe_raw(cf
);
1075 if (!hook_active(g
)) { /* Regular yield: move results down if needed. */
1076 cTValue
*f
= L
->top
- nresults
;
1078 TValue
*t
= L
->base
;
1079 while (--nresults
>= 0) copyTV(L
, t
++, f
++);
1082 } else { /* Yield from hook: add a pseudo-frame. */
1083 TValue
*top
= L
->top
;
1085 top
->u64
= cframe_multres(cf
);
1086 setcont(top
+1, lj_cont_hook
);
1087 setframe_pc(top
+1, cframe_pc(cf
)-1);
1088 setframe_gc(top
+2, obj2gco(L
));
1089 setframe_ftsz(top
+2, (int)((char *)(top
+3)-(char *)L
->base
)+FRAME_CONT
);
1090 L
->top
= L
->base
= top
+3;
1093 lj_err_throw(L
, LUA_YIELD
);
1096 L
->status
= LUA_YIELD
;
1097 lj_vm_unwind_c(cf
, LUA_YIELD
);
1100 lj_err_msg(L
, LJ_ERR_CYIELD
);
1101 return 0; /* unreachable */
1104 LUA_API
int lua_resume(lua_State
*L
, int nargs
)
1106 if (L
->cframe
== NULL
&& L
->status
<= LUA_YIELD
)
1107 return lj_vm_resume(L
, L
->top
- nargs
, 0, 0);
1109 setstrV(L
, L
->top
, lj_err_str(L
, LJ_ERR_COSUSP
));
1114 /* -- Load and dump Lua code ---------------------------------------------- */
1116 static TValue
*cpparser(lua_State
*L
, lua_CFunction dummy
, void *ud
)
1118 LexState
*ls
= (LexState
*)ud
;
1122 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
1123 pt
= lj_lex_setup(L
, ls
) ? lj_bcread(ls
) : lj_parse(ls
);
1124 fn
= lj_func_newL_empty(L
, pt
, tabref(L
->env
));
1125 /* Don't combine above/below into one statement. */
1126 setfuncV(L
, L
->top
++, fn
);
1130 LUA_API
int lua_load(lua_State
*L
, lua_Reader reader
, void *data
,
1131 const char *chunkname
)
1137 ls
.chunkarg
= chunkname
? chunkname
: "?";
1138 lj_str_initbuf(&ls
.sb
);
1139 status
= lj_vm_cpcall(L
, NULL
, &ls
, cpparser
);
1140 lj_lex_cleanup(L
, &ls
);
1145 LUA_API
int lua_dump(lua_State
*L
, lua_Writer writer
, void *data
)
1147 cTValue
*o
= L
->top
-1;
1148 api_checknelems(L
, 1);
1149 if (tvisfunc(o
) && isluafunc(funcV(o
)))
1150 return lj_bcwrite(L
, funcproto(funcV(o
)), writer
, data
, 0);
1155 /* -- GC and memory management -------------------------------------------- */
1157 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
)
1159 global_State
*g
= G(L
);
1163 g
->gc
.threshold
= LJ_MAX_MEM
;
1166 g
->gc
.threshold
= data
== -1 ? (g
->gc
.total
/100)*g
->gc
.pause
: g
->gc
.total
;
1172 res
= (int)(g
->gc
.total
>> 10);
1175 res
= (int)(g
->gc
.total
& 0x3ff);
1178 MSize a
= (MSize
)data
<< 10;
1179 g
->gc
.threshold
= (a
<= g
->gc
.total
) ? (g
->gc
.total
- a
) : 0;
1180 while (g
->gc
.total
>= g
->gc
.threshold
)
1181 if (lj_gc_step(L
)) {
1187 case LUA_GCSETPAUSE
:
1188 res
= (int)(g
->gc
.pause
);
1189 g
->gc
.pause
= (MSize
)data
;
1191 case LUA_GCSETSTEPMUL
:
1192 res
= (int)(g
->gc
.stepmul
);
1193 g
->gc
.stepmul
= (MSize
)data
;
1196 res
= -1; /* Invalid option. */
1201 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
)
1203 global_State
*g
= G(L
);
1204 if (ud
) *ud
= g
->allocd
;
1208 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
)
1210 global_State
*g
= G(L
);