3 ** Copyright (C) 2005-2013 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"
27 #include "lj_strfmt.h"
29 /* -- Common helper functions --------------------------------------------- */
31 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
32 #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
34 static TValue
*index2adr(lua_State
*L
, int idx
)
37 TValue
*o
= L
->base
+ (idx
- 1);
38 return o
< L
->top
? o
: niltv(L
);
39 } else if (idx
> LUA_REGISTRYINDEX
) {
40 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
42 } else if (idx
== LUA_GLOBALSINDEX
) {
43 TValue
*o
= &G(L
)->tmptv
;
44 settabV(L
, o
, tabref(L
->env
));
46 } else if (idx
== LUA_REGISTRYINDEX
) {
49 GCfunc
*fn
= curr_func(L
);
50 api_check(L
, fn
->c
.gct
== ~LJ_TFUNC
&& !isluafunc(fn
));
51 if (idx
== LUA_ENVIRONINDEX
) {
52 TValue
*o
= &G(L
)->tmptv
;
53 settabV(L
, o
, tabref(fn
->c
.env
));
56 idx
= LUA_GLOBALSINDEX
- idx
;
57 return idx
<= fn
->c
.nupvalues
? &fn
->c
.upvalue
[idx
-1] : niltv(L
);
62 static TValue
*stkindex2adr(lua_State
*L
, int idx
)
65 TValue
*o
= L
->base
+ (idx
- 1);
66 return o
< L
->top
? o
: niltv(L
);
68 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
73 static GCtab
*getcurrenv(lua_State
*L
)
75 GCfunc
*fn
= curr_func(L
);
76 return fn
->c
.gct
== ~LJ_TFUNC
? tabref(fn
->c
.env
) : tabref(L
->env
);
79 /* -- Miscellaneous API functions ----------------------------------------- */
81 LUA_API
int lua_status(lua_State
*L
)
86 LUA_API
int lua_checkstack(lua_State
*L
, int size
)
88 if (size
> LUAI_MAXCSTACK
|| (L
->top
- L
->base
+ size
) > LUAI_MAXCSTACK
) {
89 return 0; /* Stack overflow. */
90 } else if (size
> 0) {
91 lj_state_checkstack(L
, (MSize
)size
);
96 LUALIB_API
void luaL_checkstack(lua_State
*L
, int size
, const char *msg
)
98 if (!lua_checkstack(L
, size
))
99 lj_err_callerv(L
, LJ_ERR_STKOVM
, msg
);
102 LUA_API
void lua_xmove(lua_State
*from
, lua_State
*to
, int n
)
105 if (from
== to
) return;
106 api_checknelems(from
, n
);
107 api_check(from
, G(from
) == G(to
));
108 lj_state_checkstack(to
, (MSize
)n
);
110 t
= to
->top
= to
->top
+ n
;
111 while (--n
>= 0) copyTV(to
, --t
, --f
);
115 /* -- Stack manipulation -------------------------------------------------- */
117 LUA_API
int lua_gettop(lua_State
*L
)
119 return (int)(L
->top
- L
->base
);
122 LUA_API
void lua_settop(lua_State
*L
, int idx
)
125 api_check(L
, idx
<= tvref(L
->maxstack
) - L
->base
);
126 if (L
->base
+ idx
> L
->top
) {
127 if (L
->base
+ idx
>= tvref(L
->maxstack
))
128 lj_state_growstack(L
, (MSize
)idx
- (MSize
)(L
->top
- L
->base
));
129 do { setnilV(L
->top
++); } while (L
->top
< L
->base
+ idx
);
131 L
->top
= L
->base
+ idx
;
134 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
135 L
->top
+= idx
+1; /* Shrinks top (idx < 0). */
139 LUA_API
void lua_remove(lua_State
*L
, int idx
)
141 TValue
*p
= stkindex2adr(L
, idx
);
142 api_checkvalidindex(L
, p
);
143 while (++p
< L
->top
) copyTV(L
, p
-1, p
);
147 LUA_API
void lua_insert(lua_State
*L
, int idx
)
149 TValue
*q
, *p
= stkindex2adr(L
, idx
);
150 api_checkvalidindex(L
, p
);
151 for (q
= L
->top
; q
> p
; q
--) copyTV(L
, q
, q
-1);
152 copyTV(L
, p
, L
->top
);
155 LUA_API
void lua_replace(lua_State
*L
, int idx
)
157 api_checknelems(L
, 1);
158 if (idx
== LUA_GLOBALSINDEX
) {
159 api_check(L
, tvistab(L
->top
-1));
160 /* NOBARRIER: A thread (i.e. L) is never black. */
161 setgcref(L
->env
, obj2gco(tabV(L
->top
-1)));
162 } else if (idx
== LUA_ENVIRONINDEX
) {
163 GCfunc
*fn
= curr_func(L
);
164 if (fn
->c
.gct
!= ~LJ_TFUNC
)
165 lj_err_msg(L
, LJ_ERR_NOENV
);
166 api_check(L
, tvistab(L
->top
-1));
167 setgcref(fn
->c
.env
, obj2gco(tabV(L
->top
-1)));
168 lj_gc_barrier(L
, fn
, L
->top
-1);
170 TValue
*o
= index2adr(L
, idx
);
171 api_checkvalidindex(L
, o
);
172 copyTV(L
, o
, L
->top
-1);
173 if (idx
< LUA_GLOBALSINDEX
) /* Need a barrier for upvalues. */
174 lj_gc_barrier(L
, curr_func(L
), L
->top
-1);
179 LUA_API
void lua_pushvalue(lua_State
*L
, int idx
)
181 copyTV(L
, L
->top
, index2adr(L
, idx
));
185 /* -- Stack getters ------------------------------------------------------- */
187 LUA_API
int lua_type(lua_State
*L
, int idx
)
189 cTValue
*o
= index2adr(L
, idx
);
193 } else if (tvislightud(o
)) {
194 return LUA_TLIGHTUSERDATA
;
196 } else if (o
== niltv(L
)) {
198 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
199 uint32_t t
= ~itype(o
);
201 int tt
= (int)((U64x(75a06
,98042110) >> 4*t
) & 15u);
203 int tt
= (int)(((t
< 8 ? 0x98042110u
: 0x75a06u
) >> 4*(t
&7)) & 15u);
205 lua_assert(tt
!= LUA_TNIL
|| tvisnil(o
));
210 LUALIB_API
void luaL_checktype(lua_State
*L
, int idx
, int tt
)
212 if (lua_type(L
, idx
) != tt
)
213 lj_err_argt(L
, idx
, tt
);
216 LUALIB_API
void luaL_checkany(lua_State
*L
, int idx
)
218 if (index2adr(L
, idx
) == niltv(L
))
219 lj_err_arg(L
, idx
, LJ_ERR_NOVAL
);
222 LUA_API
const char *lua_typename(lua_State
*L
, int t
)
225 return lj_obj_typename
[t
+1];
228 LUA_API
int lua_iscfunction(lua_State
*L
, int idx
)
230 cTValue
*o
= index2adr(L
, idx
);
231 return tvisfunc(o
) && !isluafunc(funcV(o
));
234 LUA_API
int lua_isnumber(lua_State
*L
, int idx
)
236 cTValue
*o
= index2adr(L
, idx
);
238 return (tvisnumber(o
) || (tvisstr(o
) && lj_strscan_number(strV(o
), &tmp
)));
241 LUA_API
int lua_isstring(lua_State
*L
, int idx
)
243 cTValue
*o
= index2adr(L
, idx
);
244 return (tvisstr(o
) || tvisnumber(o
));
247 LUA_API
int lua_isuserdata(lua_State
*L
, int idx
)
249 cTValue
*o
= index2adr(L
, idx
);
250 return (tvisudata(o
) || tvislightud(o
));
253 LUA_API
int lua_rawequal(lua_State
*L
, int idx1
, int idx2
)
255 cTValue
*o1
= index2adr(L
, idx1
);
256 cTValue
*o2
= index2adr(L
, idx2
);
257 return (o1
== niltv(L
) || o2
== niltv(L
)) ? 0 : lj_obj_equal(o1
, o2
);
260 LUA_API
int lua_equal(lua_State
*L
, int idx1
, int idx2
)
262 cTValue
*o1
= index2adr(L
, idx1
);
263 cTValue
*o2
= index2adr(L
, idx2
);
264 if (tvisint(o1
) && tvisint(o2
)) {
265 return intV(o1
) == intV(o2
);
266 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
267 return numberVnum(o1
) == numberVnum(o2
);
268 } else if (itype(o1
) != itype(o2
)) {
270 } else if (tvispri(o1
)) {
271 return o1
!= niltv(L
) && o2
!= niltv(L
);
273 } else if (tvislightud(o1
)) {
274 return o1
->u64
== o2
->u64
;
276 } else if (gcrefeq(o1
->gcr
, o2
->gcr
)) {
278 } else if (!tvistabud(o1
)) {
281 TValue
*base
= lj_meta_equal(L
, gcV(o1
), gcV(o2
), 0);
282 if ((uintptr_t)base
<= 1) {
283 return (int)(uintptr_t)base
;
286 lj_vm_call(L
, base
, 1+1);
288 return tvistruecond(L
->top
+1);
293 LUA_API
int lua_lessthan(lua_State
*L
, int idx1
, int idx2
)
295 cTValue
*o1
= index2adr(L
, idx1
);
296 cTValue
*o2
= index2adr(L
, idx2
);
297 if (o1
== niltv(L
) || o2
== niltv(L
)) {
299 } else if (tvisint(o1
) && tvisint(o2
)) {
300 return intV(o1
) < intV(o2
);
301 } else if (tvisnumber(o1
) && tvisnumber(o2
)) {
302 return numberVnum(o1
) < numberVnum(o2
);
304 TValue
*base
= lj_meta_comp(L
, o1
, o2
, 0);
305 if ((uintptr_t)base
<= 1) {
306 return (int)(uintptr_t)base
;
309 lj_vm_call(L
, base
, 1+1);
311 return tvistruecond(L
->top
+1);
316 LUA_API lua_Number
lua_tonumber(lua_State
*L
, int idx
)
318 cTValue
*o
= index2adr(L
, idx
);
320 if (LJ_LIKELY(tvisnumber(o
)))
321 return numberVnum(o
);
322 else if (tvisstr(o
) && lj_strscan_num(strV(o
), &tmp
))
328 LUALIB_API lua_Number
luaL_checknumber(lua_State
*L
, int idx
)
330 cTValue
*o
= index2adr(L
, idx
);
332 if (LJ_LIKELY(tvisnumber(o
)))
333 return numberVnum(o
);
334 else if (!(tvisstr(o
) && lj_strscan_num(strV(o
), &tmp
)))
335 lj_err_argt(L
, idx
, LUA_TNUMBER
);
339 LUALIB_API lua_Number
luaL_optnumber(lua_State
*L
, int idx
, lua_Number def
)
341 cTValue
*o
= index2adr(L
, idx
);
343 if (LJ_LIKELY(tvisnumber(o
)))
344 return numberVnum(o
);
347 else if (!(tvisstr(o
) && lj_strscan_num(strV(o
), &tmp
)))
348 lj_err_argt(L
, idx
, LUA_TNUMBER
);
352 LUA_API lua_Integer
lua_tointeger(lua_State
*L
, int idx
)
354 cTValue
*o
= index2adr(L
, idx
);
357 if (LJ_LIKELY(tvisint(o
))) {
359 } else if (LJ_LIKELY(tvisnum(o
))) {
362 if (!(tvisstr(o
) && lj_strscan_number(strV(o
), &tmp
)))
365 return (lua_Integer
)intV(&tmp
);
369 return (lua_Integer
)n
;
371 return lj_num2int(n
);
375 LUALIB_API lua_Integer
luaL_checkinteger(lua_State
*L
, int idx
)
377 cTValue
*o
= index2adr(L
, idx
);
380 if (LJ_LIKELY(tvisint(o
))) {
382 } else if (LJ_LIKELY(tvisnum(o
))) {
385 if (!(tvisstr(o
) && lj_strscan_number(strV(o
), &tmp
)))
386 lj_err_argt(L
, idx
, LUA_TNUMBER
);
388 return (lua_Integer
)intV(&tmp
);
392 return (lua_Integer
)n
;
394 return lj_num2int(n
);
398 LUALIB_API lua_Integer
luaL_optinteger(lua_State
*L
, int idx
, lua_Integer def
)
400 cTValue
*o
= index2adr(L
, idx
);
403 if (LJ_LIKELY(tvisint(o
))) {
405 } else if (LJ_LIKELY(tvisnum(o
))) {
407 } else if (tvisnil(o
)) {
410 if (!(tvisstr(o
) && lj_strscan_number(strV(o
), &tmp
)))
411 lj_err_argt(L
, idx
, LUA_TNUMBER
);
413 return (lua_Integer
)intV(&tmp
);
417 return (lua_Integer
)n
;
419 return lj_num2int(n
);
423 LUA_API
int lua_toboolean(lua_State
*L
, int idx
)
425 cTValue
*o
= index2adr(L
, idx
);
426 return tvistruecond(o
);
429 LUA_API
const char *lua_tolstring(lua_State
*L
, int idx
, size_t *len
)
431 TValue
*o
= index2adr(L
, idx
);
433 if (LJ_LIKELY(tvisstr(o
))) {
435 } else if (tvisnumber(o
)) {
437 o
= index2adr(L
, idx
); /* GC may move the stack. */
438 s
= lj_strfmt_number(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_strfmt_number(L
, o
);
460 lj_err_argt(L
, idx
, LUA_TSTRING
);
462 if (len
!= NULL
) *len
= s
->len
;
466 LUALIB_API
const char *luaL_optlstring(lua_State
*L
, int idx
,
467 const char *def
, size_t *len
)
469 TValue
*o
= index2adr(L
, idx
);
471 if (LJ_LIKELY(tvisstr(o
))) {
473 } else if (tvisnil(o
)) {
474 if (len
!= NULL
) *len
= def
? strlen(def
) : 0;
476 } else if (tvisnumber(o
)) {
478 o
= index2adr(L
, idx
); /* GC may move the stack. */
479 s
= lj_strfmt_number(L
, o
);
482 lj_err_argt(L
, idx
, LUA_TSTRING
);
484 if (len
!= NULL
) *len
= s
->len
;
488 LUALIB_API
int luaL_checkoption(lua_State
*L
, int idx
, const char *def
,
489 const char *const lst
[])
492 const char *s
= lua_tolstring(L
, idx
, NULL
);
493 if (s
== NULL
&& (s
= def
) == NULL
)
494 lj_err_argt(L
, idx
, LUA_TSTRING
);
495 for (i
= 0; lst
[i
]; i
++)
496 if (strcmp(lst
[i
], s
) == 0)
498 lj_err_argv(L
, idx
, LJ_ERR_INVOPTM
, s
);
501 LUA_API
size_t lua_objlen(lua_State
*L
, int idx
)
503 TValue
*o
= index2adr(L
, idx
);
506 } else if (tvistab(o
)) {
507 return (size_t)lj_tab_len(tabV(o
));
508 } else if (tvisudata(o
)) {
509 return udataV(o
)->len
;
510 } else if (tvisnumber(o
)) {
511 GCstr
*s
= lj_strfmt_number(L
, o
);
519 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
)
521 cTValue
*o
= index2adr(L
, idx
);
523 BCOp op
= bc_op(*mref(funcV(o
)->c
.pc
, BCIns
));
524 if (op
== BC_FUNCC
|| op
== BC_FUNCCW
)
525 return funcV(o
)->c
.f
;
530 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
)
532 cTValue
*o
= index2adr(L
, idx
);
534 return uddata(udataV(o
));
535 else if (tvislightud(o
))
541 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
)
543 cTValue
*o
= index2adr(L
, idx
);
544 return (!tvisthread(o
)) ? NULL
: threadV(o
);
547 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
)
549 return lj_obj_ptr(index2adr(L
, idx
));
552 /* -- Stack setters (object creation) ------------------------------------- */
554 LUA_API
void lua_pushnil(lua_State
*L
)
560 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
)
563 if (LJ_UNLIKELY(tvisnan(L
->top
)))
564 setnanV(L
->top
); /* Canonicalize injected NaNs. */
568 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
)
570 setintptrV(L
->top
, n
);
574 LUA_API
void lua_pushlstring(lua_State
*L
, const char *str
, size_t len
)
578 s
= lj_str_new(L
, str
, len
);
579 setstrV(L
, L
->top
, s
);
583 LUA_API
void lua_pushstring(lua_State
*L
, const char *str
)
590 s
= lj_str_newz(L
, str
);
591 setstrV(L
, L
->top
, s
);
596 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
600 return lj_strfmt_pushvf(L
, fmt
, argp
);
603 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...)
609 ret
= lj_strfmt_pushvf(L
, fmt
, argp
);
614 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction f
, int n
)
618 api_checknelems(L
, n
);
619 fn
= lj_func_newC(L
, (MSize
)n
, getcurrenv(L
));
623 copyTV(L
, &fn
->c
.upvalue
[n
], L
->top
+n
);
624 setfuncV(L
, L
->top
, fn
);
625 lua_assert(iswhite(obj2gco(fn
)));
629 LUA_API
void lua_pushboolean(lua_State
*L
, int b
)
631 setboolV(L
->top
, (b
!= 0));
635 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
)
637 setlightudV(L
->top
, checklightudptr(L
, p
));
641 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
)
644 settabV(L
, L
->top
, lj_tab_new_ah(L
, narray
, nrec
));
648 LUALIB_API
int luaL_newmetatable(lua_State
*L
, const char *tname
)
650 GCtab
*regt
= tabV(registry(L
));
651 TValue
*tv
= lj_tab_setstr(L
, regt
, lj_str_newz(L
, tname
));
653 GCtab
*mt
= lj_tab_new(L
, 0, 1);
655 settabV(L
, L
->top
++, mt
);
656 lj_gc_anybarriert(L
, regt
);
659 copyTV(L
, L
->top
++, tv
);
664 LUA_API
int lua_pushthread(lua_State
*L
)
666 setthreadV(L
, L
->top
, L
);
668 return (mainthread(G(L
)) == L
);
671 LUA_API lua_State
*lua_newthread(lua_State
*L
)
675 L1
= lj_state_new(L
);
676 setthreadV(L
, L
->top
, L1
);
681 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
)
685 if (size
> LJ_MAX_UDATA
)
686 lj_err_msg(L
, LJ_ERR_UDATAOV
);
687 ud
= lj_udata_new(L
, (MSize
)size
, getcurrenv(L
));
688 setudataV(L
, L
->top
, ud
);
693 LUA_API
void lua_concat(lua_State
*L
, int n
)
695 api_checknelems(L
, n
);
699 TValue
*top
= lj_meta_cat(L
, L
->top
-1, -n
);
704 n
-= (int)(L
->top
- top
);
706 lj_vm_call(L
, top
, 1+1);
708 copyTV(L
, L
->top
-1, L
->top
);
710 } else if (n
== 0) { /* Push empty string. */
711 setstrV(L
, L
->top
, &G(L
)->strempty
);
714 /* else n == 1: nothing to do. */
717 /* -- Object getters ------------------------------------------------------ */
719 LUA_API
void lua_gettable(lua_State
*L
, int idx
)
721 cTValue
*v
, *t
= index2adr(L
, idx
);
722 api_checkvalidindex(L
, t
);
723 v
= lj_meta_tget(L
, t
, L
->top
-1);
726 lj_vm_call(L
, L
->top
-2, 1+1);
730 copyTV(L
, L
->top
-1, v
);
733 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
)
735 cTValue
*v
, *t
= index2adr(L
, idx
);
737 api_checkvalidindex(L
, t
);
738 setstrV(L
, &key
, lj_str_newz(L
, k
));
739 v
= lj_meta_tget(L
, t
, &key
);
742 lj_vm_call(L
, L
->top
-2, 1+1);
746 copyTV(L
, L
->top
, v
);
750 LUA_API
void lua_rawget(lua_State
*L
, int idx
)
752 cTValue
*t
= index2adr(L
, idx
);
753 api_check(L
, tvistab(t
));
754 copyTV(L
, L
->top
-1, lj_tab_get(L
, tabV(t
), L
->top
-1));
757 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
)
759 cTValue
*v
, *t
= index2adr(L
, idx
);
760 api_check(L
, tvistab(t
));
761 v
= lj_tab_getint(tabV(t
), n
);
763 copyTV(L
, L
->top
, v
);
770 LUA_API
int lua_getmetatable(lua_State
*L
, int idx
)
772 cTValue
*o
= index2adr(L
, idx
);
775 mt
= tabref(tabV(o
)->metatable
);
776 else if (tvisudata(o
))
777 mt
= tabref(udataV(o
)->metatable
);
779 mt
= tabref(basemt_obj(G(L
), o
));
782 settabV(L
, L
->top
, mt
);
787 LUALIB_API
int luaL_getmetafield(lua_State
*L
, int idx
, const char *field
)
789 if (lua_getmetatable(L
, idx
)) {
790 cTValue
*tv
= lj_tab_getstr(tabV(L
->top
-1), lj_str_newz(L
, field
));
791 if (tv
&& !tvisnil(tv
)) {
792 copyTV(L
, L
->top
-1, tv
);
800 LUA_API
void lua_getfenv(lua_State
*L
, int idx
)
802 cTValue
*o
= index2adr(L
, idx
);
803 api_checkvalidindex(L
, o
);
805 settabV(L
, L
->top
, tabref(funcV(o
)->c
.env
));
806 } else if (tvisudata(o
)) {
807 settabV(L
, L
->top
, tabref(udataV(o
)->env
));
808 } else if (tvisthread(o
)) {
809 settabV(L
, L
->top
, tabref(threadV(o
)->env
));
816 LUA_API
int lua_next(lua_State
*L
, int idx
)
818 cTValue
*t
= index2adr(L
, idx
);
820 api_check(L
, tvistab(t
));
821 more
= lj_tab_next(L
, tabV(t
), L
->top
-1);
823 incr_top(L
); /* Return new key and value slot. */
824 } else { /* End of traversal. */
825 L
->top
--; /* Remove key slot. */
830 LUA_API
const char *lua_getupvalue(lua_State
*L
, int idx
, int n
)
833 const char *name
= lj_debug_uvnamev(index2adr(L
, idx
), (uint32_t)(n
-1), &val
);
835 copyTV(L
, L
->top
, val
);
841 LUA_API
void *lua_upvalueid(lua_State
*L
, int idx
, int n
)
843 GCfunc
*fn
= funcV(index2adr(L
, idx
));
845 api_check(L
, (uint32_t)n
< fn
->l
.nupvalues
);
846 return isluafunc(fn
) ? (void *)gcref(fn
->l
.uvptr
[n
]) :
847 (void *)&fn
->c
.upvalue
[n
];
850 LUA_API
void lua_upvaluejoin(lua_State
*L
, int idx1
, int n1
, int idx2
, int n2
)
852 GCfunc
*fn1
= funcV(index2adr(L
, idx1
));
853 GCfunc
*fn2
= funcV(index2adr(L
, idx2
));
855 api_check(L
, isluafunc(fn1
) && (uint32_t)n1
< fn1
->l
.nupvalues
);
856 api_check(L
, isluafunc(fn2
) && (uint32_t)n2
< fn2
->l
.nupvalues
);
857 setgcrefr(fn1
->l
.uvptr
[n1
], fn2
->l
.uvptr
[n2
]);
858 lj_gc_objbarrier(L
, fn1
, gcref(fn1
->l
.uvptr
[n1
]));
861 LUALIB_API
void *luaL_checkudata(lua_State
*L
, int idx
, const char *tname
)
863 cTValue
*o
= index2adr(L
, idx
);
865 GCudata
*ud
= udataV(o
);
866 cTValue
*tv
= lj_tab_getstr(tabV(registry(L
)), lj_str_newz(L
, tname
));
867 if (tv
&& tvistab(tv
) && tabV(tv
) == tabref(ud
->metatable
))
870 lj_err_argtype(L
, idx
, tname
);
871 return NULL
; /* unreachable */
874 /* -- Object setters ------------------------------------------------------ */
876 LUA_API
void lua_settable(lua_State
*L
, int idx
)
879 cTValue
*t
= index2adr(L
, idx
);
880 api_checknelems(L
, 2);
881 api_checkvalidindex(L
, t
);
882 o
= lj_meta_tset(L
, t
, L
->top
-2);
884 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
885 copyTV(L
, o
, L
->top
-1);
889 copyTV(L
, L
->top
-1, L
->top
-6);
890 lj_vm_call(L
, L
->top
-3, 0+1);
895 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
)
899 cTValue
*t
= index2adr(L
, idx
);
900 api_checknelems(L
, 1);
901 api_checkvalidindex(L
, t
);
902 setstrV(L
, &key
, lj_str_newz(L
, k
));
903 o
= lj_meta_tset(L
, t
, &key
);
906 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
907 copyTV(L
, o
, L
->top
);
910 copyTV(L
, L
->top
-1, L
->top
-6);
911 lj_vm_call(L
, L
->top
-3, 0+1);
916 LUA_API
void lua_rawset(lua_State
*L
, int idx
)
918 GCtab
*t
= tabV(index2adr(L
, idx
));
920 api_checknelems(L
, 2);
922 dst
= lj_tab_set(L
, t
, key
);
923 copyTV(L
, dst
, key
+1);
924 lj_gc_anybarriert(L
, t
);
928 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
)
930 GCtab
*t
= tabV(index2adr(L
, idx
));
932 api_checknelems(L
, 1);
933 dst
= lj_tab_setint(L
, t
, n
);
936 lj_gc_barriert(L
, t
, dst
);
940 LUA_API
int lua_setmetatable(lua_State
*L
, int idx
)
944 cTValue
*o
= index2adr(L
, idx
);
945 api_checknelems(L
, 1);
946 api_checkvalidindex(L
, o
);
947 if (tvisnil(L
->top
-1)) {
950 api_check(L
, tvistab(L
->top
-1));
955 setgcref(tabV(o
)->metatable
, obj2gco(mt
));
957 lj_gc_objbarriert(L
, tabV(o
), mt
);
958 } else if (tvisudata(o
)) {
959 setgcref(udataV(o
)->metatable
, obj2gco(mt
));
961 lj_gc_objbarrier(L
, udataV(o
), mt
);
963 /* Flush cache, since traces specialize to basemt. But not during __gc. */
964 if (lj_trace_flushall(L
))
965 lj_err_caller(L
, LJ_ERR_NOGCMM
);
967 /* NOBARRIER: basemt is a GC root. */
968 setgcref(basemt_it(g
, LJ_TTRUE
), obj2gco(mt
));
969 setgcref(basemt_it(g
, LJ_TFALSE
), obj2gco(mt
));
971 /* NOBARRIER: basemt is a GC root. */
972 setgcref(basemt_obj(g
, o
), obj2gco(mt
));
979 LUA_API
int lua_setfenv(lua_State
*L
, int idx
)
981 cTValue
*o
= index2adr(L
, idx
);
983 api_checknelems(L
, 1);
984 api_checkvalidindex(L
, o
);
985 api_check(L
, tvistab(L
->top
-1));
988 setgcref(funcV(o
)->c
.env
, obj2gco(t
));
989 } else if (tvisudata(o
)) {
990 setgcref(udataV(o
)->env
, obj2gco(t
));
991 } else if (tvisthread(o
)) {
992 setgcref(threadV(o
)->env
, obj2gco(t
));
997 lj_gc_objbarrier(L
, gcV(o
), t
);
1002 LUA_API
const char *lua_setupvalue(lua_State
*L
, int idx
, int n
)
1004 cTValue
*f
= index2adr(L
, idx
);
1007 api_checknelems(L
, 1);
1008 name
= lj_debug_uvnamev(f
, (uint32_t)(n
-1), &val
);
1011 copyTV(L
, val
, L
->top
);
1012 lj_gc_barrier(L
, funcV(f
), L
->top
);
1017 /* -- Calls --------------------------------------------------------------- */
1019 LUA_API
void lua_call(lua_State
*L
, int nargs
, int nresults
)
1021 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1022 api_checknelems(L
, nargs
+1);
1023 lj_vm_call(L
, L
->top
- nargs
, nresults
+1);
1026 LUA_API
int lua_pcall(lua_State
*L
, int nargs
, int nresults
, int errfunc
)
1028 global_State
*g
= G(L
);
1029 uint8_t oldh
= hook_save(g
);
1032 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1033 api_checknelems(L
, nargs
+1);
1037 cTValue
*o
= stkindex2adr(L
, errfunc
);
1038 api_checkvalidindex(L
, o
);
1039 ef
= savestack(L
, o
);
1041 status
= lj_vm_pcall(L
, L
->top
- nargs
, nresults
+1, ef
);
1042 if (status
) hook_restore(g
, oldh
);
1046 static TValue
*cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1048 GCfunc
*fn
= lj_func_newC(L
, 0, getcurrenv(L
));
1050 setfuncV(L
, L
->top
, fn
);
1051 setlightudV(L
->top
+1, checklightudptr(L
, ud
));
1052 cframe_nres(L
->cframe
) = 1+0; /* Zero results. */
1054 return L
->top
-1; /* Now call the newly allocated C function. */
1057 LUA_API
int lua_cpcall(lua_State
*L
, lua_CFunction func
, void *ud
)
1059 global_State
*g
= G(L
);
1060 uint8_t oldh
= hook_save(g
);
1062 api_check(L
, L
->status
== 0 || L
->status
== LUA_ERRERR
);
1063 status
= lj_vm_cpcall(L
, func
, ud
, cpcall
);
1064 if (status
) hook_restore(g
, oldh
);
1068 LUALIB_API
int luaL_callmeta(lua_State
*L
, int idx
, const char *field
)
1070 if (luaL_getmetafield(L
, idx
, field
)) {
1071 TValue
*base
= L
->top
--;
1072 copyTV(L
, base
, index2adr(L
, idx
));
1074 lj_vm_call(L
, base
, 1+1);
1080 /* -- Coroutine yield and resume ------------------------------------------ */
1082 LUA_API
int lua_yield(lua_State
*L
, int nresults
)
1084 void *cf
= L
->cframe
;
1085 global_State
*g
= G(L
);
1086 if (cframe_canyield(cf
)) {
1087 cf
= cframe_raw(cf
);
1088 if (!hook_active(g
)) { /* Regular yield: move results down if needed. */
1089 cTValue
*f
= L
->top
- nresults
;
1091 TValue
*t
= L
->base
;
1092 while (--nresults
>= 0) copyTV(L
, t
++, f
++);
1096 L
->status
= LUA_YIELD
;
1098 } else { /* Yield from hook: add a pseudo-frame. */
1099 TValue
*top
= L
->top
;
1101 top
->u64
= cframe_multres(cf
);
1102 setcont(top
+1, lj_cont_hook
);
1103 setframe_pc(top
+1, cframe_pc(cf
)-1);
1104 setframe_gc(top
+2, obj2gco(L
));
1105 setframe_ftsz(top
+2, (int)((char *)(top
+3)-(char *)L
->base
)+FRAME_CONT
);
1106 L
->top
= L
->base
= top
+3;
1108 lj_err_throw(L
, LUA_YIELD
);
1111 L
->status
= LUA_YIELD
;
1112 lj_vm_unwind_c(cf
, LUA_YIELD
);
1116 lj_err_msg(L
, LJ_ERR_CYIELD
);
1117 return 0; /* unreachable */
1120 LUA_API
int lua_resume(lua_State
*L
, int nargs
)
1122 if (L
->cframe
== NULL
&& L
->status
<= LUA_YIELD
)
1123 return lj_vm_resume(L
, L
->top
- nargs
, 0, 0);
1125 setstrV(L
, L
->top
, lj_err_str(L
, LJ_ERR_COSUSP
));
1130 /* -- GC and memory management -------------------------------------------- */
1132 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
)
1134 global_State
*g
= G(L
);
1138 g
->gc
.threshold
= LJ_MAX_MEM
;
1141 g
->gc
.threshold
= data
== -1 ? (g
->gc
.total
/100)*g
->gc
.pause
: g
->gc
.total
;
1147 res
= (int)(g
->gc
.total
>> 10);
1150 res
= (int)(g
->gc
.total
& 0x3ff);
1153 MSize a
= (MSize
)data
<< 10;
1154 g
->gc
.threshold
= (a
<= g
->gc
.total
) ? (g
->gc
.total
- a
) : 0;
1155 while (g
->gc
.total
>= g
->gc
.threshold
)
1156 if (lj_gc_step(L
)) {
1162 case LUA_GCSETPAUSE
:
1163 res
= (int)(g
->gc
.pause
);
1164 g
->gc
.pause
= (MSize
)data
;
1166 case LUA_GCSETSTEPMUL
:
1167 res
= (int)(g
->gc
.stepmul
);
1168 g
->gc
.stepmul
= (MSize
)data
;
1171 res
= -1; /* Invalid option. */
1176 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
)
1178 global_State
*g
= G(L
);
1179 if (ud
) *ud
= g
->allocd
;
1183 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
)
1185 global_State
*g
= G(L
);