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
26 #include "lj_strscan.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_strscan_number(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_strscan_num(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_strscan_num(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_strscan_num(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_strscan_number(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_strscan_number(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_strscan_number(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
);
440 if (len
!= NULL
) *len
= 0;
443 if (len
!= NULL
) *len
= s
->len
;
447 LUALIB_API
const char *luaL_checklstring(lua_State
*L
, int idx
, size_t *len
)
449 TValue
*o
= index2adr(L
, idx
);
451 if (LJ_LIKELY(tvisstr(o
))) {
453 } else if (tvisnumber(o
)) {
455 o
= index2adr(L
, idx
); /* GC may move the stack. */
456 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
);
481 lj_err_argt(L
, idx
, LUA_TSTRING
);
483 if (len
!= NULL
) *len
= s
->len
;
487 LUALIB_API
int luaL_checkoption(lua_State
*L
, int idx
, const char *def
,
488 const char *const lst
[])
491 const char *s
= lua_tolstring(L
, idx
, NULL
);
492 if (s
== NULL
&& (s
= def
) == NULL
)
493 lj_err_argt(L
, idx
, LUA_TSTRING
);
494 for (i
= 0; lst
[i
]; i
++)
495 if (strcmp(lst
[i
], s
) == 0)
497 lj_err_argv(L
, idx
, LJ_ERR_INVOPTM
, s
);
500 LUA_API
size_t lua_objlen(lua_State
*L
, int idx
)
502 TValue
*o
= index2adr(L
, idx
);
505 } else if (tvistab(o
)) {
506 return (size_t)lj_tab_len(tabV(o
));
507 } else if (tvisudata(o
)) {
508 return udataV(o
)->len
;
509 } else if (tvisnumber(o
)) {
510 GCstr
*s
= lj_str_fromnumber(L
, o
);
518 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
)
520 cTValue
*o
= index2adr(L
, idx
);
522 BCOp op
= bc_op(*mref(funcV(o
)->c
.pc
, BCIns
));
523 if (op
== BC_FUNCC
|| op
== BC_FUNCCW
)
524 return funcV(o
)->c
.f
;
529 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
)
531 cTValue
*o
= index2adr(L
, idx
);
533 return uddata(udataV(o
));
534 else if (tvislightud(o
))
540 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
)
542 cTValue
*o
= index2adr(L
, idx
);
543 return (!tvisthread(o
)) ? NULL
: threadV(o
);
546 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
)
548 cTValue
*o
= index2adr(L
, idx
);
550 return uddata(udataV(o
));
551 else if (tvislightud(o
))
553 else if (tviscdata(o
))
554 return cdataptr(cdataV(o
));
561 /* -- Stack setters (object creation) ------------------------------------- */
563 LUA_API
void lua_pushnil(lua_State
*L
)
569 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
)
572 if (LJ_UNLIKELY(tvisnan(L
->top
)))
573 setnanV(L
->top
); /* Canonicalize injected NaNs. */
577 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
)
579 setintptrV(L
->top
, n
);
583 LUA_API
void lua_pushlstring(lua_State
*L
, const char *str
, size_t len
)
587 s
= lj_str_new(L
, str
, len
);
588 setstrV(L
, L
->top
, s
);
592 LUA_API
void lua_pushstring(lua_State
*L
, const char *str
)
599 s
= lj_str_newz(L
, str
);
600 setstrV(L
, L
->top
, s
);
605 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
609 return lj_str_pushvf(L
, fmt
, argp
);
612 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...)
618 ret
= lj_str_pushvf(L
, fmt
, argp
);
623 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction f
, int n
)
627 api_checknelems(L
, n
);
628 fn
= lj_func_newC(L
, (MSize
)n
, getcurrenv(L
));
632 copyTV(L
, &fn
->c
.upvalue
[n
], L
->top
+n
);
633 setfuncV(L
, L
->top
, fn
);
634 lua_assert(iswhite(obj2gco(fn
)));
638 LUA_API
void lua_pushboolean(lua_State
*L
, int b
)
640 setboolV(L
->top
, (b
!= 0));
644 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
)
646 setlightudV(L
->top
, checklightudptr(L
, p
));
650 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
)
654 t
= lj_tab_new(L
, (uint32_t)(narray
> 0 ? narray
+1 : 0), hsize2hbits(nrec
));
655 settabV(L
, L
->top
, t
);
659 LUALIB_API
int luaL_newmetatable(lua_State
*L
, const char *tname
)
661 GCtab
*regt
= tabV(registry(L
));
662 TValue
*tv
= lj_tab_setstr(L
, regt
, lj_str_newz(L
, tname
));
664 GCtab
*mt
= lj_tab_new(L
, 0, 1);
666 settabV(L
, L
->top
++, mt
);
667 lj_gc_anybarriert(L
, regt
);
670 copyTV(L
, L
->top
++, tv
);
675 LUA_API
int lua_pushthread(lua_State
*L
)
677 setthreadV(L
, L
->top
, L
);
679 return (mainthread(G(L
)) == L
);
682 LUA_API lua_State
*lua_newthread(lua_State
*L
)
686 L1
= lj_state_new(L
);
687 setthreadV(L
, L
->top
, L1
);
692 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
)
696 if (size
> LJ_MAX_UDATA
)
697 lj_err_msg(L
, LJ_ERR_UDATAOV
);
698 ud
= lj_udata_new(L
, (MSize
)size
, getcurrenv(L
));
699 setudataV(L
, L
->top
, ud
);
704 LUA_API
void lua_concat(lua_State
*L
, int n
)
706 api_checknelems(L
, n
);
710 TValue
*top
= lj_meta_cat(L
, L
->top
-1, -n
);
715 n
-= (int)(L
->top
- top
);
717 lj_vm_call(L
, top
, 1+1);
719 copyTV(L
, L
->top
-1, L
->top
);
721 } else if (n
== 0) { /* Push empty string. */
722 setstrV(L
, L
->top
, &G(L
)->strempty
);
725 /* else n == 1: nothing to do. */
728 /* -- Object getters ------------------------------------------------------ */
730 LUA_API
void lua_gettable(lua_State
*L
, int idx
)
732 cTValue
*v
, *t
= index2adr(L
, idx
);
733 api_checkvalidindex(L
, t
);
734 v
= lj_meta_tget(L
, t
, L
->top
-1);
737 lj_vm_call(L
, L
->top
-2, 1+1);
741 copyTV(L
, L
->top
-1, v
);
744 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
)
746 cTValue
*v
, *t
= index2adr(L
, idx
);
748 api_checkvalidindex(L
, t
);
749 setstrV(L
, &key
, lj_str_newz(L
, k
));
750 v
= lj_meta_tget(L
, t
, &key
);
753 lj_vm_call(L
, L
->top
-2, 1+1);
757 copyTV(L
, L
->top
, v
);
761 LUA_API
void lua_rawget(lua_State
*L
, int idx
)
763 cTValue
*t
= index2adr(L
, idx
);
764 api_check(L
, tvistab(t
));
765 copyTV(L
, L
->top
-1, lj_tab_get(L
, tabV(t
), L
->top
-1));
768 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
)
770 cTValue
*v
, *t
= index2adr(L
, idx
);
771 api_check(L
, tvistab(t
));
772 v
= lj_tab_getint(tabV(t
), n
);
774 copyTV(L
, L
->top
, v
);
781 LUA_API
int lua_getmetatable(lua_State
*L
, int idx
)
783 cTValue
*o
= index2adr(L
, idx
);
786 mt
= tabref(tabV(o
)->metatable
);
787 else if (tvisudata(o
))
788 mt
= tabref(udataV(o
)->metatable
);
790 mt
= tabref(basemt_obj(G(L
), o
));
793 settabV(L
, L
->top
, mt
);
798 LUALIB_API
int luaL_getmetafield(lua_State
*L
, int idx
, const char *field
)
800 if (lua_getmetatable(L
, idx
)) {
801 cTValue
*tv
= lj_tab_getstr(tabV(L
->top
-1), lj_str_newz(L
, field
));
802 if (tv
&& !tvisnil(tv
)) {
803 copyTV(L
, L
->top
-1, tv
);
811 LUA_API
void lua_getfenv(lua_State
*L
, int idx
)
813 cTValue
*o
= index2adr(L
, idx
);
814 api_checkvalidindex(L
, o
);
816 settabV(L
, L
->top
, tabref(funcV(o
)->c
.env
));
817 } else if (tvisudata(o
)) {
818 settabV(L
, L
->top
, tabref(udataV(o
)->env
));
819 } else if (tvisthread(o
)) {
820 settabV(L
, L
->top
, tabref(threadV(o
)->env
));
827 LUA_API
int lua_next(lua_State
*L
, int idx
)
829 cTValue
*t
= index2adr(L
, idx
);
831 api_check(L
, tvistab(t
));
832 more
= lj_tab_next(L
, tabV(t
), L
->top
-1);
834 incr_top(L
); /* Return new key and value slot. */
835 } else { /* End of traversal. */
836 L
->top
--; /* Remove key slot. */
841 LUA_API
const char *lua_getupvalue(lua_State
*L
, int idx
, int n
)
844 const char *name
= lj_debug_uvnamev(index2adr(L
, idx
), (uint32_t)(n
-1), &val
);
846 copyTV(L
, L
->top
, val
);
852 LUA_API
void *lua_upvalueid(lua_State
*L
, int idx
, int n
)
854 GCfunc
*fn
= funcV(index2adr(L
, idx
));
856 api_check(L
, (uint32_t)n
< fn
->l
.nupvalues
);
857 return isluafunc(fn
) ? (void *)gcref(fn
->l
.uvptr
[n
]) :
858 (void *)&fn
->c
.upvalue
[n
];
861 LUA_API
void lua_upvaluejoin(lua_State
*L
, int idx1
, int n1
, int idx2
, int n2
)
863 GCfunc
*fn1
= funcV(index2adr(L
, idx1
));
864 GCfunc
*fn2
= funcV(index2adr(L
, idx2
));
866 api_check(L
, isluafunc(fn1
) && (uint32_t)n1
< fn1
->l
.nupvalues
);
867 api_check(L
, isluafunc(fn2
) && (uint32_t)n2
< fn2
->l
.nupvalues
);
868 setgcrefr(fn1
->l
.uvptr
[n1
], fn2
->l
.uvptr
[n2
]);
869 lj_gc_objbarrier(L
, fn1
, gcref(fn1
->l
.uvptr
[n1
]));
872 LUALIB_API
void *luaL_checkudata(lua_State
*L
, int idx
, const char *tname
)
874 cTValue
*o
= index2adr(L
, idx
);
876 GCudata
*ud
= udataV(o
);
877 cTValue
*tv
= lj_tab_getstr(tabV(registry(L
)), lj_str_newz(L
, tname
));
878 if (tv
&& tvistab(tv
) && tabV(tv
) == tabref(ud
->metatable
))
881 lj_err_argtype(L
, idx
, tname
);
882 return NULL
; /* unreachable */
885 /* -- Object setters ------------------------------------------------------ */
887 LUA_API
void lua_settable(lua_State
*L
, int idx
)
890 cTValue
*t
= index2adr(L
, idx
);
891 api_checknelems(L
, 2);
892 api_checkvalidindex(L
, t
);
893 o
= lj_meta_tset(L
, t
, L
->top
-2);
895 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
896 copyTV(L
, o
, L
->top
-1);
900 copyTV(L
, L
->top
-1, L
->top
-6);
901 lj_vm_call(L
, L
->top
-3, 0+1);
906 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
)
910 cTValue
*t
= index2adr(L
, idx
);
911 api_checknelems(L
, 1);
912 api_checkvalidindex(L
, t
);
913 setstrV(L
, &key
, lj_str_newz(L
, k
));
914 o
= lj_meta_tset(L
, t
, &key
);
917 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
918 copyTV(L
, o
, L
->top
);
921 copyTV(L
, L
->top
-1, L
->top
-6);
922 lj_vm_call(L
, L
->top
-3, 0+1);
927 LUA_API
void lua_rawset(lua_State
*L
, int idx
)
929 GCtab
*t
= tabV(index2adr(L
, idx
));
931 api_checknelems(L
, 2);
933 dst
= lj_tab_set(L
, t
, key
);
934 copyTV(L
, dst
, key
+1);
935 lj_gc_anybarriert(L
, t
);
939 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
)
941 GCtab
*t
= tabV(index2adr(L
, idx
));
943 api_checknelems(L
, 1);
944 dst
= lj_tab_setint(L
, t
, n
);
947 lj_gc_barriert(L
, t
, dst
);
951 LUA_API
int lua_setmetatable(lua_State
*L
, int idx
)
955 cTValue
*o
= index2adr(L
, idx
);
956 api_checknelems(L
, 1);
957 api_checkvalidindex(L
, o
);
958 if (tvisnil(L
->top
-1)) {
961 api_check(L
, tvistab(L
->top
-1));
966 setgcref(tabV(o
)->metatable
, obj2gco(mt
));
968 lj_gc_objbarriert(L
, tabV(o
), mt
);
969 } else if (tvisudata(o
)) {
970 setgcref(udataV(o
)->metatable
, obj2gco(mt
));
972 lj_gc_objbarrier(L
, udataV(o
), mt
);
974 /* Flush cache, since traces specialize to basemt. But not during __gc. */
975 if (lj_trace_flushall(L
))
976 lj_err_caller(L
, LJ_ERR_NOGCMM
);
978 /* NOBARRIER: basemt is a GC root. */
979 setgcref(basemt_it(g
, LJ_TTRUE
), obj2gco(mt
));
980 setgcref(basemt_it(g
, LJ_TFALSE
), obj2gco(mt
));
982 /* NOBARRIER: basemt is a GC root. */
983 setgcref(basemt_obj(g
, o
), obj2gco(mt
));
990 LUA_API
int lua_setfenv(lua_State
*L
, int idx
)
992 cTValue
*o
= index2adr(L
, idx
);
994 api_checknelems(L
, 1);
995 api_checkvalidindex(L
, o
);
996 api_check(L
, tvistab(L
->top
-1));
999 setgcref(funcV(o
)->c
.env
, obj2gco(t
));
1000 } else if (tvisudata(o
)) {
1001 setgcref(udataV(o
)->env
, obj2gco(t
));
1002 } else if (tvisthread(o
)) {
1003 setgcref(threadV(o
)->env
, obj2gco(t
));
1008 lj_gc_objbarrier(L
, gcV(o
), t
);
1013 LUA_API
const char *lua_setupvalue(lua_State
*L
, int idx
, int n
)
1015 cTValue
*f
= index2adr(L
, idx
);
1018 api_checknelems(L
, 1);
1019 name
= lj_debug_uvnamev(f
, (uint32_t)(n
-1), &val
);
1022 copyTV(L
, val
, L
->top
);
1023 lj_gc_barrier(L
, funcV(f
), L
->top
);
1028 /* -- Calls --------------------------------------------------------------- */
1030 LUA_API
void lua_call(lua_State
*L
, int nargs
, int nresults
)
1032 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1033 api_checknelems(L
, nargs
+1);
1034 lj_vm_call(L
, L
->top
- nargs
, nresults
+1);
1037 LUA_API
int lua_pcall(lua_State
*L
, int nargs
, int nresults
, int errfunc
)
1039 global_State
*g
= G(L
);
1040 uint8_t oldh
= hook_save(g
);
1043 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1044 api_checknelems(L
, nargs
+1);
1048 cTValue
*o
= stkindex2adr(L
, errfunc
);
1049 api_checkvalidindex(L
, o
);
1050 ef
= savestack(L
, o
);
1052 status
= lj_vm_pcall(L
, L
->top
- nargs
, nresults
+1, ef
);
1053 if (status
) hook_restore(g
, oldh
);
1057 static TValue
*cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1059 GCfunc
*fn
= lj_func_newC(L
, 0, getcurrenv(L
));
1061 setfuncV(L
, L
->top
, fn
);
1062 setlightudV(L
->top
+1, checklightudptr(L
, ud
));
1063 cframe_nres(L
->cframe
) = 1+0; /* Zero results. */
1065 return L
->top
-1; /* Now call the newly allocated C function. */
1068 LUA_API
int lua_cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1070 global_State
*g
= G(L
);
1071 uint8_t oldh
= hook_save(g
);
1073 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1074 status
= lj_vm_cpcall(L
, func
, ud
, cpcall
);
1075 if (status
) hook_restore(g
, oldh
);
1079 LUALIB_API
int luaL_callmeta(lua_State
*L
, int idx
, const char *field
)
1081 if (luaL_getmetafield(L
, idx
, field
)) {
1082 TValue
*base
= L
->top
--;
1083 copyTV(L
, base
, index2adr(L
, idx
));
1085 lj_vm_call(L
, base
, 1+1);
1091 /* -- Coroutine yield and resume ------------------------------------------ */
1093 LUA_API
int lua_yield(lua_State
*L
, int nresults
)
1095 void *cf
= L
->cframe
;
1096 global_State
*g
= G(L
);
1097 if (cframe_canyield(cf
)) {
1098 cf
= cframe_raw(cf
);
1099 if (!hook_active(g
)) { /* Regular yield: move results down if needed. */
1100 cTValue
*f
= L
->top
- nresults
;
1102 TValue
*t
= L
->base
;
1103 while (--nresults
>= 0) copyTV(L
, t
++, f
++);
1106 } else { /* Yield from hook: add a pseudo-frame. */
1107 TValue
*top
= L
->top
;
1109 top
->u64
= cframe_multres(cf
);
1110 setcont(top
+1, lj_cont_hook
);
1111 setframe_pc(top
+1, cframe_pc(cf
)-1);
1112 setframe_gc(top
+2, obj2gco(L
));
1113 setframe_ftsz(top
+2, (int)((char *)(top
+3)-(char *)L
->base
)+FRAME_CONT
);
1114 L
->top
= L
->base
= top
+3;
1117 lj_err_throw(L
, LUA_YIELD
);
1120 L
->status
= LUA_YIELD
;
1121 lj_vm_unwind_c(cf
, LUA_YIELD
);
1124 lj_err_msg(L
, LJ_ERR_CYIELD
);
1125 return 0; /* unreachable */
1128 LUA_API
int lua_resume(lua_State
*L
, int nargs
)
1130 if (L
->cframe
== NULL
&& L
->status
<= LUA_YIELD
)
1131 return lj_vm_resume(L
, L
->top
- nargs
, 0, 0);
1133 setstrV(L
, L
->top
, lj_err_str(L
, LJ_ERR_COSUSP
));
1138 /* -- GC and memory management -------------------------------------------- */
1140 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
)
1142 global_State
*g
= G(L
);
1146 g
->gc
.threshold
= LJ_MAX_MEM
;
1149 g
->gc
.threshold
= data
== -1 ? (g
->gc
.total
/100)*g
->gc
.pause
: g
->gc
.total
;
1155 res
= (int)(g
->gc
.total
>> 10);
1158 res
= (int)(g
->gc
.total
& 0x3ff);
1161 MSize a
= (MSize
)data
<< 10;
1162 g
->gc
.threshold
= (a
<= g
->gc
.total
) ? (g
->gc
.total
- a
) : 0;
1163 while (g
->gc
.total
>= g
->gc
.threshold
)
1164 if (lj_gc_step(L
)) {
1170 case LUA_GCSETPAUSE
:
1171 res
= (int)(g
->gc
.pause
);
1172 g
->gc
.pause
= (MSize
)data
;
1174 case LUA_GCSETSTEPMUL
:
1175 res
= (int)(g
->gc
.stepmul
);
1176 g
->gc
.stepmul
= (MSize
)data
;
1179 res
= -1; /* Invalid option. */
1184 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
)
1186 global_State
*g
= G(L
);
1187 if (ud
) *ud
= g
->allocd
;
1191 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
)
1193 global_State
*g
= G(L
);