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
28 /* -- Common helper functions --------------------------------------------- */
30 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
31 #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
33 static TValue
*index2adr(lua_State
*L
, int idx
)
36 TValue
*o
= L
->base
+ (idx
- 1);
37 return o
< L
->top
? o
: niltv(L
);
38 } else if (idx
> LUA_REGISTRYINDEX
) {
39 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
41 } else if (idx
== LUA_GLOBALSINDEX
) {
42 TValue
*o
= &G(L
)->tmptv
;
43 settabV(L
, o
, tabref(L
->env
));
45 } else if (idx
== LUA_REGISTRYINDEX
) {
48 GCfunc
*fn
= curr_func(L
);
49 api_check(L
, fn
->c
.gct
== ~LJ_TFUNC
&& !isluafunc(fn
));
50 if (idx
== LUA_ENVIRONINDEX
) {
51 TValue
*o
= &G(L
)->tmptv
;
52 settabV(L
, o
, tabref(fn
->c
.env
));
55 idx
= LUA_GLOBALSINDEX
- idx
;
56 return idx
<= fn
->c
.nupvalues
? &fn
->c
.upvalue
[idx
-1] : niltv(L
);
61 static TValue
*stkindex2adr(lua_State
*L
, int idx
)
64 TValue
*o
= L
->base
+ (idx
- 1);
65 return o
< L
->top
? o
: niltv(L
);
67 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
72 static GCtab
*getcurrenv(lua_State
*L
)
74 GCfunc
*fn
= curr_func(L
);
75 return fn
->c
.gct
== ~LJ_TFUNC
? tabref(fn
->c
.env
) : tabref(L
->env
);
78 /* -- Miscellaneous API functions ----------------------------------------- */
80 LUA_API
int lua_status(lua_State
*L
)
85 LUA_API
int lua_checkstack(lua_State
*L
, int size
)
87 if (size
> LUAI_MAXCSTACK
|| (L
->top
- L
->base
+ size
) > LUAI_MAXCSTACK
) {
88 return 0; /* Stack overflow. */
89 } else if (size
> 0) {
90 lj_state_checkstack(L
, (MSize
)size
);
95 LUALIB_API
void luaL_checkstack(lua_State
*L
, int size
, const char *msg
)
97 if (!lua_checkstack(L
, size
))
98 lj_err_callerv(L
, LJ_ERR_STKOVM
, msg
);
101 LUA_API
void lua_xmove(lua_State
*from
, lua_State
*to
, int n
)
104 if (from
== to
) return;
105 api_checknelems(from
, n
);
106 api_check(from
, G(from
) == G(to
));
107 lj_state_checkstack(to
, (MSize
)n
);
109 t
= to
->top
= to
->top
+ n
;
110 while (--n
>= 0) copyTV(to
, --t
, --f
);
114 /* -- Stack manipulation -------------------------------------------------- */
116 LUA_API
int lua_gettop(lua_State
*L
)
118 return (int)(L
->top
- L
->base
);
121 LUA_API
void lua_settop(lua_State
*L
, int idx
)
124 api_check(L
, idx
<= tvref(L
->maxstack
) - L
->base
);
125 if (L
->base
+ idx
> L
->top
) {
126 if (L
->base
+ idx
>= tvref(L
->maxstack
))
127 lj_state_growstack(L
, (MSize
)idx
- (MSize
)(L
->top
- L
->base
));
128 do { setnilV(L
->top
++); } while (L
->top
< L
->base
+ idx
);
130 L
->top
= L
->base
+ idx
;
133 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
134 L
->top
+= idx
+1; /* Shrinks top (idx < 0). */
138 LUA_API
void lua_remove(lua_State
*L
, int idx
)
140 TValue
*p
= stkindex2adr(L
, idx
);
141 api_checkvalidindex(L
, p
);
142 while (++p
< L
->top
) copyTV(L
, p
-1, p
);
146 LUA_API
void lua_insert(lua_State
*L
, int idx
)
148 TValue
*q
, *p
= stkindex2adr(L
, idx
);
149 api_checkvalidindex(L
, p
);
150 for (q
= L
->top
; q
> p
; q
--) copyTV(L
, q
, q
-1);
151 copyTV(L
, p
, L
->top
);
154 LUA_API
void lua_replace(lua_State
*L
, int idx
)
156 api_checknelems(L
, 1);
157 if (idx
== LUA_GLOBALSINDEX
) {
158 api_check(L
, tvistab(L
->top
-1));
159 /* NOBARRIER: A thread (i.e. L) is never black. */
160 setgcref(L
->env
, obj2gco(tabV(L
->top
-1)));
161 } else if (idx
== LUA_ENVIRONINDEX
) {
162 GCfunc
*fn
= curr_func(L
);
163 if (fn
->c
.gct
!= ~LJ_TFUNC
)
164 lj_err_msg(L
, LJ_ERR_NOENV
);
165 api_check(L
, tvistab(L
->top
-1));
166 setgcref(fn
->c
.env
, obj2gco(tabV(L
->top
-1)));
167 lj_gc_barrier(L
, fn
, L
->top
-1);
169 TValue
*o
= index2adr(L
, idx
);
170 api_checkvalidindex(L
, o
);
171 copyTV(L
, o
, L
->top
-1);
172 if (idx
< LUA_GLOBALSINDEX
) /* Need a barrier for upvalues. */
173 lj_gc_barrier(L
, curr_func(L
), L
->top
-1);
178 LUA_API
void lua_pushvalue(lua_State
*L
, int idx
)
180 copyTV(L
, L
->top
, index2adr(L
, idx
));
184 /* -- Stack getters ------------------------------------------------------- */
186 LUA_API
int lua_type(lua_State
*L
, int idx
)
188 cTValue
*o
= index2adr(L
, idx
);
192 } else if (tvislightud(o
)) {
193 return LUA_TLIGHTUSERDATA
;
195 } else if (o
== niltv(L
)) {
197 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
198 uint32_t t
= ~itype(o
);
200 int tt
= (int)((U64x(75a06
,98042110) >> 4*t
) & 15u);
202 int tt
= (int)(((t
< 8 ? 0x98042110u
: 0x75a06u
) >> 4*(t
&7)) & 15u);
204 lua_assert(tt
!= LUA_TNIL
|| tvisnil(o
));
209 LUALIB_API
void luaL_checktype(lua_State
*L
, int idx
, int tt
)
211 if (lua_type(L
, idx
) != tt
)
212 lj_err_argt(L
, idx
, tt
);
215 LUALIB_API
void luaL_checkany(lua_State
*L
, int idx
)
217 if (index2adr(L
, idx
) == niltv(L
))
218 lj_err_arg(L
, idx
, LJ_ERR_NOVAL
);
221 LUA_API
const char *lua_typename(lua_State
*L
, int t
)
224 return lj_obj_typename
[t
+1];
227 LUA_API
int lua_iscfunction(lua_State
*L
, int idx
)
229 cTValue
*o
= index2adr(L
, idx
);
230 return tvisfunc(o
) && !isluafunc(funcV(o
));
233 LUA_API
int lua_isnumber(lua_State
*L
, int idx
)
235 cTValue
*o
= index2adr(L
, idx
);
237 return (tvisnumber(o
) || (tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)));
240 LUA_API
int lua_isstring(lua_State
*L
, int idx
)
242 cTValue
*o
= index2adr(L
, idx
);
243 return (tvisstr(o
) || tvisnumber(o
));
246 LUA_API
int lua_isuserdata(lua_State
*L
, int idx
)
248 cTValue
*o
= index2adr(L
, idx
);
249 return (tvisudata(o
) || tvislightud(o
));
252 LUA_API
int lua_rawequal(lua_State
*L
, int idx1
, int idx2
)
254 cTValue
*o1
= index2adr(L
, idx1
);
255 cTValue
*o2
= index2adr(L
, idx2
);
256 return (o1
== niltv(L
) || o2
== niltv(L
)) ? 0 : lj_obj_equal(o1
, o2
);
259 LUA_API
int lua_equal(lua_State
*L
, int idx1
, int idx2
)
261 cTValue
*o1
= index2adr(L
, idx1
);
262 cTValue
*o2
= index2adr(L
, idx2
);
263 if (tvisint(o1
) && tvisint(o2
)) {
264 return intV(o1
) == intV(o2
);
265 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
266 return numberVnum(o1
) == numberVnum(o2
);
267 } else if (itype(o1
) != itype(o2
)) {
269 } else if (tvispri(o1
)) {
270 return o1
!= niltv(L
) && o2
!= niltv(L
);
272 } else if (tvislightud(o1
)) {
273 return o1
->u64
== o2
->u64
;
275 } else if (gcrefeq(o1
->gcr
, o2
->gcr
)) {
277 } else if (!tvistabud(o1
)) {
280 TValue
*base
= lj_meta_equal(L
, gcV(o1
), gcV(o2
), 0);
281 if ((uintptr_t)base
<= 1) {
282 return (int)(uintptr_t)base
;
285 lj_vm_call(L
, base
, 1+1);
287 return tvistruecond(L
->top
+1);
292 LUA_API
int lua_lessthan(lua_State
*L
, int idx1
, int idx2
)
294 cTValue
*o1
= index2adr(L
, idx1
);
295 cTValue
*o2
= index2adr(L
, idx2
);
296 if (o1
== niltv(L
) || o2
== niltv(L
)) {
298 } else if (tvisint(o1
) && tvisint(o2
)) {
299 return intV(o1
) < intV(o2
);
300 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
301 return numberVnum(o1
) < numberVnum(o2
);
303 TValue
*base
= lj_meta_comp(L
, o1
, o2
, 0);
304 if ((uintptr_t)base
<= 1) {
305 return (int)(uintptr_t)base
;
308 lj_vm_call(L
, base
, 1+1);
310 return tvistruecond(L
->top
+1);
315 LUA_API lua_Number
lua_tonumber(lua_State
*L
, int idx
)
317 cTValue
*o
= index2adr(L
, idx
);
319 if (LJ_LIKELY(tvisnumber(o
)))
320 return numberVnum(o
);
321 else if (tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
))
327 LUALIB_API lua_Number
luaL_checknumber(lua_State
*L
, int idx
)
329 cTValue
*o
= index2adr(L
, idx
);
331 if (LJ_LIKELY(tvisnumber(o
)))
332 return numberVnum(o
);
333 else if (!(tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
)))
334 lj_err_argt(L
, idx
, LUA_TNUMBER
);
338 LUALIB_API lua_Number
luaL_optnumber(lua_State
*L
, int idx
, lua_Number def
)
340 cTValue
*o
= index2adr(L
, idx
);
342 if (LJ_LIKELY(tvisnumber(o
)))
343 return numberVnum(o
);
346 else if (!(tvisstr(o
) && lj_str_tonum(strV(o
), &tmp
)))
347 lj_err_argt(L
, idx
, LUA_TNUMBER
);
351 LUA_API lua_Integer
lua_tointeger(lua_State
*L
, int idx
)
353 cTValue
*o
= index2adr(L
, idx
);
356 if (LJ_LIKELY(tvisint(o
))) {
358 } else if (LJ_LIKELY(tvisnum(o
))) {
361 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
364 return (lua_Integer
)intV(&tmp
);
368 return (lua_Integer
)n
;
370 return lj_num2int(n
);
374 LUALIB_API lua_Integer
luaL_checkinteger(lua_State
*L
, int idx
)
376 cTValue
*o
= index2adr(L
, idx
);
379 if (LJ_LIKELY(tvisint(o
))) {
381 } else if (LJ_LIKELY(tvisnum(o
))) {
384 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
385 lj_err_argt(L
, idx
, LUA_TNUMBER
);
387 return (lua_Integer
)intV(&tmp
);
391 return (lua_Integer
)n
;
393 return lj_num2int(n
);
397 LUALIB_API lua_Integer
luaL_optinteger(lua_State
*L
, int idx
, lua_Integer def
)
399 cTValue
*o
= index2adr(L
, idx
);
402 if (LJ_LIKELY(tvisint(o
))) {
404 } else if (LJ_LIKELY(tvisnum(o
))) {
406 } else if (tvisnil(o
)) {
409 if (!(tvisstr(o
) && lj_str_tonumber(strV(o
), &tmp
)))
410 lj_err_argt(L
, idx
, LUA_TNUMBER
);
412 return (lua_Integer
)intV(&tmp
);
416 return (lua_Integer
)n
;
418 return lj_num2int(n
);
422 LUA_API
int lua_toboolean(lua_State
*L
, int idx
)
424 cTValue
*o
= index2adr(L
, idx
);
425 return tvistruecond(o
);
428 LUA_API
const char *lua_tolstring(lua_State
*L
, int idx
, size_t *len
)
430 TValue
*o
= index2adr(L
, idx
);
432 if (LJ_LIKELY(tvisstr(o
))) {
434 } else if (tvisnumber(o
)) {
436 o
= index2adr(L
, idx
); /* GC may move the stack. */
437 s
= lj_str_fromnumber(L
, o
);
439 if (len
!= NULL
) *len
= 0;
442 if (len
!= NULL
) *len
= s
->len
;
446 LUALIB_API
const char *luaL_checklstring(lua_State
*L
, int idx
, size_t *len
)
448 TValue
*o
= index2adr(L
, idx
);
450 if (LJ_LIKELY(tvisstr(o
))) {
452 } else if (tvisnumber(o
)) {
454 o
= index2adr(L
, idx
); /* GC may move the stack. */
455 s
= lj_str_fromnumber(L
, o
);
457 lj_err_argt(L
, idx
, LUA_TSTRING
);
459 if (len
!= NULL
) *len
= s
->len
;
463 LUALIB_API
const char *luaL_optlstring(lua_State
*L
, int idx
,
464 const char *def
, size_t *len
)
466 TValue
*o
= index2adr(L
, idx
);
468 if (LJ_LIKELY(tvisstr(o
))) {
470 } else if (tvisnil(o
)) {
471 if (len
!= NULL
) *len
= def
? strlen(def
) : 0;
473 } else if (tvisnumber(o
)) {
475 o
= index2adr(L
, idx
); /* GC may move the stack. */
476 s
= lj_str_fromnumber(L
, o
);
478 lj_err_argt(L
, idx
, LUA_TSTRING
);
480 if (len
!= NULL
) *len
= s
->len
;
484 LUALIB_API
int luaL_checkoption(lua_State
*L
, int idx
, const char *def
,
485 const char *const lst
[])
488 const char *s
= lua_tolstring(L
, idx
, NULL
);
489 if (s
== NULL
&& (s
= def
) == NULL
)
490 lj_err_argt(L
, idx
, LUA_TSTRING
);
491 for (i
= 0; lst
[i
]; i
++)
492 if (strcmp(lst
[i
], s
) == 0)
494 lj_err_argv(L
, idx
, LJ_ERR_INVOPTM
, s
);
497 LUA_API
size_t lua_objlen(lua_State
*L
, int idx
)
499 TValue
*o
= index2adr(L
, idx
);
503 return (size_t)lj_tab_len(tabV(o
));
504 else if (tvisudata(o
))
505 return udataV(o
)->len
;
506 else if (tvisnumber(o
))
507 return lj_str_fromnumber(L
, o
)->len
;
512 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
)
514 cTValue
*o
= index2adr(L
, idx
);
516 BCOp op
= bc_op(*mref(funcV(o
)->c
.pc
, BCIns
));
517 if (op
== BC_FUNCC
|| op
== BC_FUNCCW
)
518 return funcV(o
)->c
.f
;
523 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
)
525 cTValue
*o
= index2adr(L
, idx
);
527 return uddata(udataV(o
));
528 else if (tvislightud(o
))
534 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
)
536 cTValue
*o
= index2adr(L
, idx
);
537 return (!tvisthread(o
)) ? NULL
: threadV(o
);
540 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
)
542 cTValue
*o
= index2adr(L
, idx
);
544 return uddata(udataV(o
));
545 else if (tvislightud(o
))
547 else if (tviscdata(o
))
548 return cdataptr(cdataV(o
));
555 /* -- Stack setters (object creation) ------------------------------------- */
557 LUA_API
void lua_pushnil(lua_State
*L
)
563 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
)
566 if (LJ_UNLIKELY(tvisnan(L
->top
)))
567 setnanV(L
->top
); /* Canonicalize injected NaNs. */
571 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
)
573 setintptrV(L
->top
, n
);
577 LUA_API
void lua_pushlstring(lua_State
*L
, const char *str
, size_t len
)
581 s
= lj_str_new(L
, str
, len
);
582 setstrV(L
, L
->top
, s
);
586 LUA_API
void lua_pushstring(lua_State
*L
, const char *str
)
593 s
= lj_str_newz(L
, str
);
594 setstrV(L
, L
->top
, s
);
599 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
603 return lj_str_pushvf(L
, fmt
, argp
);
606 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...)
612 ret
= lj_str_pushvf(L
, fmt
, argp
);
617 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction f
, int n
)
621 api_checknelems(L
, n
);
622 fn
= lj_func_newC(L
, (MSize
)n
, getcurrenv(L
));
626 copyTV(L
, &fn
->c
.upvalue
[n
], L
->top
+n
);
627 setfuncV(L
, L
->top
, fn
);
628 lua_assert(iswhite(obj2gco(fn
)));
632 LUA_API
void lua_pushboolean(lua_State
*L
, int b
)
634 setboolV(L
->top
, (b
!= 0));
638 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
)
640 setlightudV(L
->top
, checklightudptr(L
, p
));
644 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
)
648 t
= lj_tab_new(L
, (uint32_t)(narray
> 0 ? narray
+1 : 0), hsize2hbits(nrec
));
649 settabV(L
, L
->top
, t
);
653 LUALIB_API
int luaL_newmetatable(lua_State
*L
, const char *tname
)
655 GCtab
*regt
= tabV(registry(L
));
656 TValue
*tv
= lj_tab_setstr(L
, regt
, lj_str_newz(L
, tname
));
658 GCtab
*mt
= lj_tab_new(L
, 0, 1);
660 settabV(L
, L
->top
++, mt
);
661 lj_gc_anybarriert(L
, regt
);
664 copyTV(L
, L
->top
++, tv
);
669 LUA_API
int lua_pushthread(lua_State
*L
)
671 setthreadV(L
, L
->top
, L
);
673 return (mainthread(G(L
)) == L
);
676 LUA_API lua_State
*lua_newthread(lua_State
*L
)
680 L1
= lj_state_new(L
);
681 setthreadV(L
, L
->top
, L1
);
686 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
)
690 if (size
> LJ_MAX_UDATA
)
691 lj_err_msg(L
, LJ_ERR_UDATAOV
);
692 ud
= lj_udata_new(L
, (MSize
)size
, getcurrenv(L
));
693 setudataV(L
, L
->top
, ud
);
698 LUA_API
void lua_concat(lua_State
*L
, int n
)
700 api_checknelems(L
, n
);
704 TValue
*top
= lj_meta_cat(L
, L
->top
-1, n
);
709 n
-= (int)(L
->top
- top
);
711 lj_vm_call(L
, top
, 1+1);
713 copyTV(L
, L
->top
-1, L
->top
);
715 } else if (n
== 0) { /* Push empty string. */
716 setstrV(L
, L
->top
, &G(L
)->strempty
);
719 /* else n == 1: nothing to do. */
722 /* -- Object getters ------------------------------------------------------ */
724 LUA_API
void lua_gettable(lua_State
*L
, int idx
)
726 cTValue
*v
, *t
= index2adr(L
, idx
);
727 api_checkvalidindex(L
, t
);
728 v
= lj_meta_tget(L
, t
, L
->top
-1);
731 lj_vm_call(L
, L
->top
-2, 1+1);
735 copyTV(L
, L
->top
-1, v
);
738 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
)
740 cTValue
*v
, *t
= index2adr(L
, idx
);
742 api_checkvalidindex(L
, t
);
743 setstrV(L
, &key
, lj_str_newz(L
, k
));
744 v
= lj_meta_tget(L
, t
, &key
);
747 lj_vm_call(L
, L
->top
-2, 1+1);
751 copyTV(L
, L
->top
, v
);
755 LUA_API
void lua_rawget(lua_State
*L
, int idx
)
757 cTValue
*t
= index2adr(L
, idx
);
758 api_check(L
, tvistab(t
));
759 copyTV(L
, L
->top
-1, lj_tab_get(L
, tabV(t
), L
->top
-1));
762 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
)
764 cTValue
*v
, *t
= index2adr(L
, idx
);
765 api_check(L
, tvistab(t
));
766 v
= lj_tab_getint(tabV(t
), n
);
768 copyTV(L
, L
->top
, v
);
775 LUA_API
int lua_getmetatable(lua_State
*L
, int idx
)
777 cTValue
*o
= index2adr(L
, idx
);
780 mt
= tabref(tabV(o
)->metatable
);
781 else if (tvisudata(o
))
782 mt
= tabref(udataV(o
)->metatable
);
784 mt
= tabref(basemt_obj(G(L
), o
));
787 settabV(L
, L
->top
, mt
);
792 LUALIB_API
int luaL_getmetafield(lua_State
*L
, int idx
, const char *field
)
794 if (lua_getmetatable(L
, idx
)) {
795 cTValue
*tv
= lj_tab_getstr(tabV(L
->top
-1), lj_str_newz(L
, field
));
796 if (tv
&& !tvisnil(tv
)) {
797 copyTV(L
, L
->top
-1, tv
);
805 LUA_API
void lua_getfenv(lua_State
*L
, int idx
)
807 cTValue
*o
= index2adr(L
, idx
);
808 api_checkvalidindex(L
, o
);
810 settabV(L
, L
->top
, tabref(funcV(o
)->c
.env
));
811 } else if (tvisudata(o
)) {
812 settabV(L
, L
->top
, tabref(udataV(o
)->env
));
813 } else if (tvisthread(o
)) {
814 settabV(L
, L
->top
, tabref(threadV(o
)->env
));
821 LUA_API
int lua_next(lua_State
*L
, int idx
)
823 cTValue
*t
= index2adr(L
, idx
);
825 api_check(L
, tvistab(t
));
826 more
= lj_tab_next(L
, tabV(t
), L
->top
-1);
828 incr_top(L
); /* Return new key and value slot. */
829 } else { /* End of traversal. */
830 L
->top
--; /* Remove key slot. */
835 static const char *aux_upvalue(cTValue
*f
, uint32_t idx
, TValue
**val
)
838 if (!tvisfunc(f
)) return NULL
;
841 GCproto
*pt
= funcproto(fn
);
842 if (idx
< pt
->sizeuv
) {
843 *val
= uvval(&gcref(fn
->l
.uvptr
[idx
])->uv
);
844 return strdata(proto_uvname(pt
, idx
));
847 if (idx
< fn
->c
.nupvalues
) {
848 *val
= &fn
->c
.upvalue
[idx
];
855 LUA_API
const char *lua_getupvalue(lua_State
*L
, int idx
, int n
)
858 const char *name
= aux_upvalue(index2adr(L
, idx
), (uint32_t)(n
-1), &val
);
860 copyTV(L
, L
->top
, val
);
866 LUALIB_API
void *luaL_checkudata(lua_State
*L
, int idx
, const char *tname
)
868 cTValue
*o
= index2adr(L
, idx
);
870 GCudata
*ud
= udataV(o
);
871 cTValue
*tv
= lj_tab_getstr(tabV(registry(L
)), lj_str_newz(L
, tname
));
872 if (tv
&& tvistab(tv
) && tabV(tv
) == tabref(ud
->metatable
))
875 lj_err_argtype(L
, idx
, tname
);
876 return NULL
; /* unreachable */
879 /* -- Object setters ------------------------------------------------------ */
881 LUA_API
void lua_settable(lua_State
*L
, int idx
)
884 cTValue
*t
= index2adr(L
, idx
);
885 api_checknelems(L
, 2);
886 api_checkvalidindex(L
, t
);
887 o
= lj_meta_tset(L
, t
, L
->top
-2);
889 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
890 copyTV(L
, o
, L
->top
-1);
894 copyTV(L
, L
->top
-1, L
->top
-6);
895 lj_vm_call(L
, L
->top
-3, 0+1);
900 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
)
904 cTValue
*t
= index2adr(L
, idx
);
905 api_checknelems(L
, 1);
906 api_checkvalidindex(L
, t
);
907 setstrV(L
, &key
, lj_str_newz(L
, k
));
908 o
= lj_meta_tset(L
, t
, &key
);
911 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
912 copyTV(L
, o
, L
->top
);
915 copyTV(L
, L
->top
-1, L
->top
-6);
916 lj_vm_call(L
, L
->top
-3, 0+1);
921 LUA_API
void lua_rawset(lua_State
*L
, int idx
)
923 GCtab
*t
= tabV(index2adr(L
, idx
));
925 api_checknelems(L
, 2);
927 dst
= lj_tab_set(L
, t
, key
);
928 copyTV(L
, dst
, key
+1);
929 lj_gc_anybarriert(L
, t
);
933 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
)
935 GCtab
*t
= tabV(index2adr(L
, idx
));
937 api_checknelems(L
, 1);
938 dst
= lj_tab_setint(L
, t
, n
);
941 lj_gc_barriert(L
, t
, dst
);
945 LUA_API
int lua_setmetatable(lua_State
*L
, int idx
)
949 cTValue
*o
= index2adr(L
, idx
);
950 api_checknelems(L
, 1);
951 api_checkvalidindex(L
, o
);
952 if (tvisnil(L
->top
-1)) {
955 api_check(L
, tvistab(L
->top
-1));
960 setgcref(tabV(o
)->metatable
, obj2gco(mt
));
962 lj_gc_objbarriert(L
, tabV(o
), mt
);
963 } else if (tvisudata(o
)) {
964 setgcref(udataV(o
)->metatable
, obj2gco(mt
));
966 lj_gc_objbarrier(L
, udataV(o
), mt
);
968 /* Flush cache, since traces specialize to basemt. But not during __gc. */
969 if (lj_trace_flushall(L
))
970 lj_err_caller(L
, LJ_ERR_NOGCMM
);
972 /* NOBARRIER: basemt is a GC root. */
973 setgcref(basemt_it(g
, LJ_TTRUE
), obj2gco(mt
));
974 setgcref(basemt_it(g
, LJ_TFALSE
), obj2gco(mt
));
976 /* NOBARRIER: basemt is a GC root. */
977 setgcref(basemt_obj(g
, o
), obj2gco(mt
));
984 LUA_API
int lua_setfenv(lua_State
*L
, int idx
)
986 cTValue
*o
= index2adr(L
, idx
);
988 api_checknelems(L
, 1);
989 api_checkvalidindex(L
, o
);
990 api_check(L
, tvistab(L
->top
-1));
993 setgcref(funcV(o
)->c
.env
, obj2gco(t
));
994 } else if (tvisudata(o
)) {
995 setgcref(udataV(o
)->env
, obj2gco(t
));
996 } else if (tvisthread(o
)) {
997 setgcref(threadV(o
)->env
, obj2gco(t
));
1002 lj_gc_objbarrier(L
, gcV(o
), t
);
1007 LUA_API
const char *lua_setupvalue(lua_State
*L
, int idx
, int n
)
1009 cTValue
*f
= index2adr(L
, idx
);
1012 api_checknelems(L
, 1);
1013 name
= aux_upvalue(f
, (uint32_t)(n
-1), &val
);
1016 copyTV(L
, val
, L
->top
);
1017 lj_gc_barrier(L
, funcV(f
), L
->top
);
1022 /* -- Calls --------------------------------------------------------------- */
1024 LUA_API
void lua_call(lua_State
*L
, int nargs
, int nresults
)
1026 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1027 api_checknelems(L
, nargs
+1);
1028 lj_vm_call(L
, L
->top
- nargs
, nresults
+1);
1031 LUA_API
int lua_pcall(lua_State
*L
, int nargs
, int nresults
, int errfunc
)
1033 global_State
*g
= G(L
);
1034 uint8_t oldh
= hook_save(g
);
1037 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1038 api_checknelems(L
, nargs
+1);
1042 cTValue
*o
= stkindex2adr(L
, errfunc
);
1043 api_checkvalidindex(L
, o
);
1044 ef
= savestack(L
, o
);
1046 status
= lj_vm_pcall(L
, L
->top
- nargs
, nresults
+1, ef
);
1047 if (status
) hook_restore(g
, oldh
);
1051 static TValue
*cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1053 GCfunc
*fn
= lj_func_newC(L
, 0, getcurrenv(L
));
1055 setfuncV(L
, L
->top
, fn
);
1056 setlightudV(L
->top
+1, checklightudptr(L
, ud
));
1057 cframe_nres(L
->cframe
) = 1+0; /* Zero results. */
1059 return L
->top
-1; /* Now call the newly allocated C function. */
1062 LUA_API
int lua_cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1064 global_State
*g
= G(L
);
1065 uint8_t oldh
= hook_save(g
);
1067 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1068 status
= lj_vm_cpcall(L
, func
, ud
, cpcall
);
1069 if (status
) hook_restore(g
, oldh
);
1073 LUALIB_API
int luaL_callmeta(lua_State
*L
, int idx
, const char *field
)
1075 if (luaL_getmetafield(L
, idx
, field
)) {
1076 TValue
*base
= L
->top
--;
1077 copyTV(L
, base
, index2adr(L
, idx
));
1079 lj_vm_call(L
, base
, 1+1);
1085 /* -- Coroutine yield and resume ------------------------------------------ */
1087 LUA_API
int lua_yield(lua_State
*L
, int nresults
)
1089 void *cf
= L
->cframe
;
1090 global_State
*g
= G(L
);
1091 if (cframe_canyield(cf
)) {
1092 cf
= cframe_raw(cf
);
1093 if (!hook_active(g
)) { /* Regular yield: move results down if needed. */
1094 cTValue
*f
= L
->top
- nresults
;
1096 TValue
*t
= L
->base
;
1097 while (--nresults
>= 0) copyTV(L
, t
++, f
++);
1100 } else { /* Yield from hook: add a pseudo-frame. */
1101 TValue
*top
= L
->top
;
1103 top
->u64
= cframe_multres(cf
);
1104 setcont(top
+1, lj_cont_hook
);
1105 setframe_pc(top
+1, cframe_pc(cf
)-1);
1106 setframe_gc(top
+2, obj2gco(L
));
1107 setframe_ftsz(top
+2, (int)((char *)(top
+3)-(char *)L
->base
)+FRAME_CONT
);
1108 L
->top
= L
->base
= top
+3;
1111 lj_err_throw(L
, LUA_YIELD
);
1114 L
->status
= LUA_YIELD
;
1115 lj_vm_unwind_c(cf
, LUA_YIELD
);
1118 lj_err_msg(L
, LJ_ERR_CYIELD
);
1119 return 0; /* unreachable */
1122 LUA_API
int lua_resume(lua_State
*L
, int nargs
)
1124 if (L
->cframe
== NULL
&& L
->status
<= LUA_YIELD
)
1125 return lj_vm_resume(L
, L
->top
- nargs
, 0, 0);
1127 setstrV(L
, L
->top
, lj_err_str(L
, LJ_ERR_COSUSP
));
1132 /* -- Load and dump Lua code ---------------------------------------------- */
1134 static TValue
*cpparser(lua_State
*L
, lua_CFunction dummy
, void *ud
)
1136 LexState
*ls
= (LexState
*)ud
;
1139 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
1140 lj_lex_setup(L
, ls
);
1141 fn
= lj_func_newL(L
, lj_parse(ls
), tabref(L
->env
));
1142 /* Parser may realloc stack. Don't combine above/below into one statement. */
1143 setfuncV(L
, L
->top
++, fn
);
1147 LUA_API
int lua_load(lua_State
*L
, lua_Reader reader
, void *data
,
1148 const char *chunkname
)
1154 ls
.chunkarg
= chunkname
? chunkname
: "?";
1155 lj_str_initbuf(L
, &ls
.sb
);
1156 status
= lj_vm_cpcall(L
, NULL
, &ls
, cpparser
);
1157 lj_lex_cleanup(L
, &ls
);
1162 LUA_API
int lua_dump(lua_State
*L
, lua_Writer writer
, void *data
)
1164 api_checknelems(L
, 1);
1165 UNUSED(L
); UNUSED(writer
); UNUSED(data
);
1166 return 1; /* Error, not supported. */
1169 /* -- GC and memory management -------------------------------------------- */
1171 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
)
1173 global_State
*g
= G(L
);
1177 g
->gc
.threshold
= LJ_MAX_MEM
;
1180 g
->gc
.threshold
= data
== -1 ? (g
->gc
.total
/100)*g
->gc
.pause
: g
->gc
.total
;
1186 res
= (int)(g
->gc
.total
>> 10);
1189 res
= (int)(g
->gc
.total
& 0x3ff);
1192 MSize a
= (MSize
)data
<< 10;
1193 g
->gc
.threshold
= (a
<= g
->gc
.total
) ? (g
->gc
.total
- a
) : 0;
1194 while (g
->gc
.total
>= g
->gc
.threshold
)
1195 if (lj_gc_step(L
)) {
1201 case LUA_GCSETPAUSE
:
1202 res
= (int)(g
->gc
.pause
);
1203 g
->gc
.pause
= (MSize
)data
;
1205 case LUA_GCSETSTEPMUL
:
1206 res
= (int)(g
->gc
.stepmul
);
1207 g
->gc
.stepmul
= (MSize
)data
;
1210 res
= -1; /* Invalid option. */
1215 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
)
1217 global_State
*g
= G(L
);
1218 if (ud
) *ud
= g
->allocd
;
1222 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
)
1224 global_State
*g
= G(L
);