3 ** Copyright (C) 2005-2012 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
);
442 if (len
!= NULL
) *len
= 0;
445 if (len
!= NULL
) *len
= s
->len
;
449 LUALIB_API
const char *luaL_checklstring(lua_State
*L
, int idx
, size_t *len
)
451 TValue
*o
= index2adr(L
, idx
);
453 if (LJ_LIKELY(tvisstr(o
))) {
455 } else if (tvisnumber(o
)) {
457 o
= index2adr(L
, idx
); /* GC may move the stack. */
458 s
= lj_str_fromnumber(L
, o
);
461 lj_err_argt(L
, idx
, LUA_TSTRING
);
463 if (len
!= NULL
) *len
= s
->len
;
467 LUALIB_API
const char *luaL_optlstring(lua_State
*L
, int idx
,
468 const char *def
, size_t *len
)
470 TValue
*o
= index2adr(L
, idx
);
472 if (LJ_LIKELY(tvisstr(o
))) {
474 } else if (tvisnil(o
)) {
475 if (len
!= NULL
) *len
= def
? strlen(def
) : 0;
477 } else if (tvisnumber(o
)) {
479 o
= index2adr(L
, idx
); /* GC may move the stack. */
480 s
= lj_str_fromnumber(L
, o
);
483 lj_err_argt(L
, idx
, LUA_TSTRING
);
485 if (len
!= NULL
) *len
= s
->len
;
489 LUALIB_API
int luaL_checkoption(lua_State
*L
, int idx
, const char *def
,
490 const char *const lst
[])
493 const char *s
= lua_tolstring(L
, idx
, NULL
);
494 if (s
== NULL
&& (s
= def
) == NULL
)
495 lj_err_argt(L
, idx
, LUA_TSTRING
);
496 for (i
= 0; lst
[i
]; i
++)
497 if (strcmp(lst
[i
], s
) == 0)
499 lj_err_argv(L
, idx
, LJ_ERR_INVOPTM
, s
);
502 LUA_API
size_t lua_objlen(lua_State
*L
, int idx
)
504 TValue
*o
= index2adr(L
, idx
);
507 } else if (tvistab(o
)) {
508 return (size_t)lj_tab_len(tabV(o
));
509 } else if (tvisudata(o
)) {
510 return udataV(o
)->len
;
511 } else if (tvisnumber(o
)) {
512 GCstr
*s
= lj_str_fromnumber(L
, o
);
520 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
)
522 cTValue
*o
= index2adr(L
, idx
);
524 BCOp op
= bc_op(*mref(funcV(o
)->c
.pc
, BCIns
));
525 if (op
== BC_FUNCC
|| op
== BC_FUNCCW
)
526 return funcV(o
)->c
.f
;
531 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
)
533 cTValue
*o
= index2adr(L
, idx
);
535 return uddata(udataV(o
));
536 else if (tvislightud(o
))
542 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
)
544 cTValue
*o
= index2adr(L
, idx
);
545 return (!tvisthread(o
)) ? NULL
: threadV(o
);
548 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
)
550 cTValue
*o
= index2adr(L
, idx
);
552 return uddata(udataV(o
));
553 else if (tvislightud(o
))
555 else if (tviscdata(o
))
556 return cdataptr(cdataV(o
));
563 /* -- Stack setters (object creation) ------------------------------------- */
565 LUA_API
void lua_pushnil(lua_State
*L
)
571 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
)
574 if (LJ_UNLIKELY(tvisnan(L
->top
)))
575 setnanV(L
->top
); /* Canonicalize injected NaNs. */
579 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
)
581 setintptrV(L
->top
, n
);
585 LUA_API
void lua_pushlstring(lua_State
*L
, const char *str
, size_t len
)
589 s
= lj_str_new(L
, str
, len
);
590 setstrV(L
, L
->top
, s
);
594 LUA_API
void lua_pushstring(lua_State
*L
, const char *str
)
601 s
= lj_str_newz(L
, str
);
602 setstrV(L
, L
->top
, s
);
607 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
611 return lj_str_pushvf(L
, fmt
, argp
);
614 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...)
620 ret
= lj_str_pushvf(L
, fmt
, argp
);
625 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction f
, int n
)
629 api_checknelems(L
, n
);
630 fn
= lj_func_newC(L
, (MSize
)n
, getcurrenv(L
));
634 copyTV(L
, &fn
->c
.upvalue
[n
], L
->top
+n
);
635 setfuncV(L
, L
->top
, fn
);
636 lua_assert(iswhite(obj2gco(fn
)));
640 LUA_API
void lua_pushboolean(lua_State
*L
, int b
)
642 setboolV(L
->top
, (b
!= 0));
646 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
)
648 setlightudV(L
->top
, checklightudptr(L
, p
));
652 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
)
656 t
= lj_tab_new(L
, (uint32_t)(narray
> 0 ? narray
+1 : 0), hsize2hbits(nrec
));
657 settabV(L
, L
->top
, t
);
661 LUALIB_API
int luaL_newmetatable(lua_State
*L
, const char *tname
)
663 GCtab
*regt
= tabV(registry(L
));
664 TValue
*tv
= lj_tab_setstr(L
, regt
, lj_str_newz(L
, tname
));
666 GCtab
*mt
= lj_tab_new(L
, 0, 1);
668 settabV(L
, L
->top
++, mt
);
669 lj_gc_anybarriert(L
, regt
);
672 copyTV(L
, L
->top
++, tv
);
677 LUA_API
int lua_pushthread(lua_State
*L
)
679 setthreadV(L
, L
->top
, L
);
681 return (mainthread(G(L
)) == L
);
684 LUA_API lua_State
*lua_newthread(lua_State
*L
)
688 L1
= lj_state_new(L
);
689 setthreadV(L
, L
->top
, L1
);
694 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
)
698 if (size
> LJ_MAX_UDATA
)
699 lj_err_msg(L
, LJ_ERR_UDATAOV
);
700 ud
= lj_udata_new(L
, (MSize
)size
, getcurrenv(L
));
701 setudataV(L
, L
->top
, ud
);
706 LUA_API
void lua_concat(lua_State
*L
, int n
)
708 api_checknelems(L
, n
);
712 TValue
*top
= lj_meta_cat(L
, L
->top
-1, -n
);
717 n
-= (int)(L
->top
- top
);
719 lj_vm_call(L
, top
, 1+1);
721 copyTV(L
, L
->top
-1, L
->top
);
723 } else if (n
== 0) { /* Push empty string. */
724 setstrV(L
, L
->top
, &G(L
)->strempty
);
727 /* else n == 1: nothing to do. */
730 /* -- Object getters ------------------------------------------------------ */
732 LUA_API
void lua_gettable(lua_State
*L
, int idx
)
734 cTValue
*v
, *t
= index2adr(L
, idx
);
735 api_checkvalidindex(L
, t
);
736 v
= lj_meta_tget(L
, t
, L
->top
-1);
739 lj_vm_call(L
, L
->top
-2, 1+1);
743 copyTV(L
, L
->top
-1, v
);
746 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
)
748 cTValue
*v
, *t
= index2adr(L
, idx
);
750 api_checkvalidindex(L
, t
);
751 setstrV(L
, &key
, lj_str_newz(L
, k
));
752 v
= lj_meta_tget(L
, t
, &key
);
755 lj_vm_call(L
, L
->top
-2, 1+1);
759 copyTV(L
, L
->top
, v
);
763 LUA_API
void lua_rawget(lua_State
*L
, int idx
)
765 cTValue
*t
= index2adr(L
, idx
);
766 api_check(L
, tvistab(t
));
767 copyTV(L
, L
->top
-1, lj_tab_get(L
, tabV(t
), L
->top
-1));
770 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
)
772 cTValue
*v
, *t
= index2adr(L
, idx
);
773 api_check(L
, tvistab(t
));
774 v
= lj_tab_getint(tabV(t
), n
);
776 copyTV(L
, L
->top
, v
);
783 LUA_API
int lua_getmetatable(lua_State
*L
, int idx
)
785 cTValue
*o
= index2adr(L
, idx
);
788 mt
= tabref(tabV(o
)->metatable
);
789 else if (tvisudata(o
))
790 mt
= tabref(udataV(o
)->metatable
);
792 mt
= tabref(basemt_obj(G(L
), o
));
795 settabV(L
, L
->top
, mt
);
800 LUALIB_API
int luaL_getmetafield(lua_State
*L
, int idx
, const char *field
)
802 if (lua_getmetatable(L
, idx
)) {
803 cTValue
*tv
= lj_tab_getstr(tabV(L
->top
-1), lj_str_newz(L
, field
));
804 if (tv
&& !tvisnil(tv
)) {
805 copyTV(L
, L
->top
-1, tv
);
813 LUA_API
void lua_getfenv(lua_State
*L
, int idx
)
815 cTValue
*o
= index2adr(L
, idx
);
816 api_checkvalidindex(L
, o
);
818 settabV(L
, L
->top
, tabref(funcV(o
)->c
.env
));
819 } else if (tvisudata(o
)) {
820 settabV(L
, L
->top
, tabref(udataV(o
)->env
));
821 } else if (tvisthread(o
)) {
822 settabV(L
, L
->top
, tabref(threadV(o
)->env
));
829 LUA_API
int lua_next(lua_State
*L
, int idx
)
831 cTValue
*t
= index2adr(L
, idx
);
833 api_check(L
, tvistab(t
));
834 more
= lj_tab_next(L
, tabV(t
), L
->top
-1);
836 incr_top(L
); /* Return new key and value slot. */
837 } else { /* End of traversal. */
838 L
->top
--; /* Remove key slot. */
843 LUA_API
const char *lua_getupvalue(lua_State
*L
, int idx
, int n
)
846 const char *name
= lj_debug_uvnamev(index2adr(L
, idx
), (uint32_t)(n
-1), &val
);
848 copyTV(L
, L
->top
, val
);
854 LUALIB_API
void *luaL_checkudata(lua_State
*L
, int idx
, const char *tname
)
856 cTValue
*o
= index2adr(L
, idx
);
858 GCudata
*ud
= udataV(o
);
859 cTValue
*tv
= lj_tab_getstr(tabV(registry(L
)), lj_str_newz(L
, tname
));
860 if (tv
&& tvistab(tv
) && tabV(tv
) == tabref(ud
->metatable
))
863 lj_err_argtype(L
, idx
, tname
);
864 return NULL
; /* unreachable */
867 /* -- Object setters ------------------------------------------------------ */
869 LUA_API
void lua_settable(lua_State
*L
, int idx
)
872 cTValue
*t
= index2adr(L
, idx
);
873 api_checknelems(L
, 2);
874 api_checkvalidindex(L
, t
);
875 o
= lj_meta_tset(L
, t
, L
->top
-2);
877 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
878 copyTV(L
, o
, L
->top
-1);
882 copyTV(L
, L
->top
-1, L
->top
-6);
883 lj_vm_call(L
, L
->top
-3, 0+1);
888 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
)
892 cTValue
*t
= index2adr(L
, idx
);
893 api_checknelems(L
, 1);
894 api_checkvalidindex(L
, t
);
895 setstrV(L
, &key
, lj_str_newz(L
, k
));
896 o
= lj_meta_tset(L
, t
, &key
);
899 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
900 copyTV(L
, o
, L
->top
);
903 copyTV(L
, L
->top
-1, L
->top
-6);
904 lj_vm_call(L
, L
->top
-3, 0+1);
909 LUA_API
void lua_rawset(lua_State
*L
, int idx
)
911 GCtab
*t
= tabV(index2adr(L
, idx
));
913 api_checknelems(L
, 2);
915 dst
= lj_tab_set(L
, t
, key
);
916 copyTV(L
, dst
, key
+1);
917 lj_gc_anybarriert(L
, t
);
921 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
)
923 GCtab
*t
= tabV(index2adr(L
, idx
));
925 api_checknelems(L
, 1);
926 dst
= lj_tab_setint(L
, t
, n
);
929 lj_gc_barriert(L
, t
, dst
);
933 LUA_API
int lua_setmetatable(lua_State
*L
, int idx
)
937 cTValue
*o
= index2adr(L
, idx
);
938 api_checknelems(L
, 1);
939 api_checkvalidindex(L
, o
);
940 if (tvisnil(L
->top
-1)) {
943 api_check(L
, tvistab(L
->top
-1));
948 setgcref(tabV(o
)->metatable
, obj2gco(mt
));
950 lj_gc_objbarriert(L
, tabV(o
), mt
);
951 } else if (tvisudata(o
)) {
952 setgcref(udataV(o
)->metatable
, obj2gco(mt
));
954 lj_gc_objbarrier(L
, udataV(o
), mt
);
956 /* Flush cache, since traces specialize to basemt. But not during __gc. */
957 if (lj_trace_flushall(L
))
958 lj_err_caller(L
, LJ_ERR_NOGCMM
);
960 /* NOBARRIER: basemt is a GC root. */
961 setgcref(basemt_it(g
, LJ_TTRUE
), obj2gco(mt
));
962 setgcref(basemt_it(g
, LJ_TFALSE
), obj2gco(mt
));
964 /* NOBARRIER: basemt is a GC root. */
965 setgcref(basemt_obj(g
, o
), obj2gco(mt
));
972 LUA_API
int lua_setfenv(lua_State
*L
, int idx
)
974 cTValue
*o
= index2adr(L
, idx
);
976 api_checknelems(L
, 1);
977 api_checkvalidindex(L
, o
);
978 api_check(L
, tvistab(L
->top
-1));
981 setgcref(funcV(o
)->c
.env
, obj2gco(t
));
982 } else if (tvisudata(o
)) {
983 setgcref(udataV(o
)->env
, obj2gco(t
));
984 } else if (tvisthread(o
)) {
985 setgcref(threadV(o
)->env
, obj2gco(t
));
990 lj_gc_objbarrier(L
, gcV(o
), t
);
995 LUA_API
const char *lua_setupvalue(lua_State
*L
, int idx
, int n
)
997 cTValue
*f
= index2adr(L
, idx
);
1000 api_checknelems(L
, 1);
1001 name
= lj_debug_uvnamev(f
, (uint32_t)(n
-1), &val
);
1004 copyTV(L
, val
, L
->top
);
1005 lj_gc_barrier(L
, funcV(f
), L
->top
);
1010 /* -- Calls --------------------------------------------------------------- */
1012 LUA_API
void lua_call(lua_State
*L
, int nargs
, int nresults
)
1014 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1015 api_checknelems(L
, nargs
+1);
1016 lj_vm_call(L
, L
->top
- nargs
, nresults
+1);
1019 LUA_API
int lua_pcall(lua_State
*L
, int nargs
, int nresults
, int errfunc
)
1021 global_State
*g
= G(L
);
1022 uint8_t oldh
= hook_save(g
);
1025 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1026 api_checknelems(L
, nargs
+1);
1030 cTValue
*o
= stkindex2adr(L
, errfunc
);
1031 api_checkvalidindex(L
, o
);
1032 ef
= savestack(L
, o
);
1034 status
= lj_vm_pcall(L
, L
->top
- nargs
, nresults
+1, ef
);
1035 if (status
) hook_restore(g
, oldh
);
1039 static TValue
*cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1041 GCfunc
*fn
= lj_func_newC(L
, 0, getcurrenv(L
));
1043 setfuncV(L
, L
->top
, fn
);
1044 setlightudV(L
->top
+1, checklightudptr(L
, ud
));
1045 cframe_nres(L
->cframe
) = 1+0; /* Zero results. */
1047 return L
->top
-1; /* Now call the newly allocated C function. */
1050 LUA_API
int lua_cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1052 global_State
*g
= G(L
);
1053 uint8_t oldh
= hook_save(g
);
1055 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1056 status
= lj_vm_cpcall(L
, func
, ud
, cpcall
);
1057 if (status
) hook_restore(g
, oldh
);
1061 LUALIB_API
int luaL_callmeta(lua_State
*L
, int idx
, const char *field
)
1063 if (luaL_getmetafield(L
, idx
, field
)) {
1064 TValue
*base
= L
->top
--;
1065 copyTV(L
, base
, index2adr(L
, idx
));
1067 lj_vm_call(L
, base
, 1+1);
1073 /* -- Coroutine yield and resume ------------------------------------------ */
1075 LUA_API
int lua_yield(lua_State
*L
, int nresults
)
1077 void *cf
= L
->cframe
;
1078 global_State
*g
= G(L
);
1079 if (cframe_canyield(cf
)) {
1080 cf
= cframe_raw(cf
);
1081 if (!hook_active(g
)) { /* Regular yield: move results down if needed. */
1082 cTValue
*f
= L
->top
- nresults
;
1084 TValue
*t
= L
->base
;
1085 while (--nresults
>= 0) copyTV(L
, t
++, f
++);
1088 } else { /* Yield from hook: add a pseudo-frame. */
1089 TValue
*top
= L
->top
;
1091 top
->u64
= cframe_multres(cf
);
1092 setcont(top
+1, lj_cont_hook
);
1093 setframe_pc(top
+1, cframe_pc(cf
)-1);
1094 setframe_gc(top
+2, obj2gco(L
));
1095 setframe_ftsz(top
+2, (int)((char *)(top
+3)-(char *)L
->base
)+FRAME_CONT
);
1096 L
->top
= L
->base
= top
+3;
1099 lj_err_throw(L
, LUA_YIELD
);
1102 L
->status
= LUA_YIELD
;
1103 lj_vm_unwind_c(cf
, LUA_YIELD
);
1106 lj_err_msg(L
, LJ_ERR_CYIELD
);
1107 return 0; /* unreachable */
1110 LUA_API
int lua_resume(lua_State
*L
, int nargs
)
1112 if (L
->cframe
== NULL
&& L
->status
<= LUA_YIELD
)
1113 return lj_vm_resume(L
, L
->top
- nargs
, 0, 0);
1115 setstrV(L
, L
->top
, lj_err_str(L
, LJ_ERR_COSUSP
));
1120 /* -- Load and dump Lua code ---------------------------------------------- */
1122 static TValue
*cpparser(lua_State
*L
, lua_CFunction dummy
, void *ud
)
1124 LexState
*ls
= (LexState
*)ud
;
1128 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
1129 pt
= lj_lex_setup(L
, ls
) ? lj_bcread(ls
) : lj_parse(ls
);
1130 fn
= lj_func_newL_empty(L
, pt
, tabref(L
->env
));
1131 /* Don't combine above/below into one statement. */
1132 setfuncV(L
, L
->top
++, fn
);
1136 LUA_API
int lua_load(lua_State
*L
, lua_Reader reader
, void *data
,
1137 const char *chunkname
)
1143 ls
.chunkarg
= chunkname
? chunkname
: "?";
1144 lj_str_initbuf(&ls
.sb
);
1145 status
= lj_vm_cpcall(L
, NULL
, &ls
, cpparser
);
1146 lj_lex_cleanup(L
, &ls
);
1151 LUA_API
int lua_dump(lua_State
*L
, lua_Writer writer
, void *data
)
1153 cTValue
*o
= L
->top
-1;
1154 api_checknelems(L
, 1);
1155 if (tvisfunc(o
) && isluafunc(funcV(o
)))
1156 return lj_bcwrite(L
, funcproto(funcV(o
)), writer
, data
, 0);
1161 /* -- GC and memory management -------------------------------------------- */
1163 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
)
1165 global_State
*g
= G(L
);
1169 g
->gc
.threshold
= LJ_MAX_MEM
;
1172 g
->gc
.threshold
= data
== -1 ? (g
->gc
.total
/100)*g
->gc
.pause
: g
->gc
.total
;
1178 res
= (int)(g
->gc
.total
>> 10);
1181 res
= (int)(g
->gc
.total
& 0x3ff);
1184 MSize a
= (MSize
)data
<< 10;
1185 g
->gc
.threshold
= (a
<= g
->gc
.total
) ? (g
->gc
.total
- a
) : 0;
1186 while (g
->gc
.total
>= g
->gc
.threshold
)
1187 if (lj_gc_step(L
)) {
1193 case LUA_GCSETPAUSE
:
1194 res
= (int)(g
->gc
.pause
);
1195 g
->gc
.pause
= (MSize
)data
;
1197 case LUA_GCSETSTEPMUL
:
1198 res
= (int)(g
->gc
.stepmul
);
1199 g
->gc
.stepmul
= (MSize
)data
;
1202 res
= -1; /* Invalid option. */
1207 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
)
1209 global_State
*g
= G(L
);
1210 if (ud
) *ud
= g
->allocd
;
1214 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
)
1216 global_State
*g
= G(L
);