2 ** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** See Copyright Notice in lua.h
8 #include <sys/zfs_context.h>
31 const char lua_ident[] =
32 "$LuaVersion: " LUA_COPYRIGHT " $"
33 "$LuaAuthors: " LUA_AUTHORS " $";
36 /* value at a non-valid index */
37 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
39 /* corresponding test */
40 #define isvalid(o) ((o) != luaO_nilobject)
42 /* test for pseudo index */
43 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
45 /* test for valid but not pseudo index */
46 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
48 #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
50 #define api_checkstackindex(L, i, o) \
51 api_check(L, isstackindex(i, o), "index not in the stack")
54 static TValue *index2addr (lua_State *L, int idx) {
57 TValue *o = ci->func + idx;
58 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
59 if (o >= L->top) return NONVALIDVALUE;
62 else if (!ispseudo(idx)) { /* negative index */
63 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
66 else if (idx == LUA_REGISTRYINDEX)
67 return &G(L)->l_registry;
69 idx = LUA_REGISTRYINDEX - idx;
70 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
71 if (ttislcf(ci->func)) /* light C function? */
72 return NONVALIDVALUE; /* it has no upvalues */
74 CClosure *func = clCvalue(ci->func);
75 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
82 ** to be called by 'lua_checkstack' in protected mode, to grow stack
83 ** capturing memory errors
85 static void growstack (lua_State *L, void *ud) {
86 int size = *(int *)ud;
87 luaD_growstack(L, size);
91 LUA_API int lua_checkstack (lua_State *L, int size) {
95 if (L->stack_last - L->top > size) /* stack large enough? */
96 res = 1; /* yes; check is OK */
97 else { /* no; need to grow stack */
98 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
99 if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
101 else /* try to grow stack */
102 res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
104 if (res && ci->top < L->top + size)
105 ci->top = L->top + size; /* adjust frame top */
111 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
113 if (from == to) return;
115 api_checknelems(from, n);
116 api_check(from, G(from) == G(to), "moving among independent states");
117 api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
119 for (i = 0; i < n; i++) {
120 setobj2s(to, to->top++, from->top + i);
126 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
130 G(L)->panic = panicf;
136 LUA_API const lua_Number *lua_version (lua_State *L) {
137 static const lua_Number version = LUA_VERSION_NUM;
138 if (L == NULL) return &version;
139 else return G(L)->version;
145 ** basic stack manipulation
150 ** convert an acceptable stack index into an absolute index
152 LUA_API int lua_absindex (lua_State *L, int idx) {
153 return (idx > 0 || ispseudo(idx))
155 : cast_int(L->top - L->ci->func + idx);
159 LUA_API int lua_gettop (lua_State *L) {
160 return cast_int(L->top - (L->ci->func + 1));
164 LUA_API void lua_settop (lua_State *L, int idx) {
165 StkId func = L->ci->func;
168 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
169 while (L->top < (func + 1) + idx)
170 setnilvalue(L->top++);
171 L->top = (func + 1) + idx;
174 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
175 L->top += idx+1; /* `subtract' index (index is negative) */
181 LUA_API void lua_remove (lua_State *L, int idx) {
184 p = index2addr(L, idx);
185 api_checkstackindex(L, idx, p);
186 while (++p < L->top) setobjs2s(L, p-1, p);
192 LUA_API void lua_insert (lua_State *L, int idx) {
196 p = index2addr(L, idx);
197 api_checkstackindex(L, idx, p);
198 for (q = L->top; q > p; q--) /* use L->top as a temporary */
199 setobjs2s(L, q, q - 1);
200 setobjs2s(L, p, L->top);
205 static void moveto (lua_State *L, TValue *fr, int idx) {
206 TValue *to = index2addr(L, idx);
207 api_checkvalidindex(L, to);
209 if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
210 luaC_barrier(L, clCvalue(L->ci->func), fr);
211 /* LUA_REGISTRYINDEX does not need gc barrier
212 (collector revisits it before finishing collection) */
216 LUA_API void lua_replace (lua_State *L, int idx) {
218 api_checknelems(L, 1);
219 moveto(L, L->top - 1, idx);
225 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
228 fr = index2addr(L, fromidx);
229 moveto(L, fr, toidx);
234 LUA_API void lua_pushvalue (lua_State *L, int idx) {
236 setobj2s(L, L->top, index2addr(L, idx));
244 ** access functions (stack -> C)
248 LUA_API int lua_type (lua_State *L, int idx) {
249 StkId o = index2addr(L, idx);
250 return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
254 LUA_API const char *lua_typename (lua_State *L, int t) {
260 LUA_API int lua_iscfunction (lua_State *L, int idx) {
261 StkId o = index2addr(L, idx);
262 return (ttislcf(o) || (ttisCclosure(o)));
266 LUA_API int lua_isnumber (lua_State *L, int idx) {
268 const TValue *o = index2addr(L, idx);
269 return tonumber(o, &n);
273 LUA_API int lua_isstring (lua_State *L, int idx) {
274 int t = lua_type(L, idx);
275 return (t == LUA_TSTRING || t == LUA_TNUMBER);
279 LUA_API int lua_isuserdata (lua_State *L, int idx) {
280 const TValue *o = index2addr(L, idx);
281 return (ttisuserdata(o) || ttislightuserdata(o));
285 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
286 StkId o1 = index2addr(L, index1);
287 StkId o2 = index2addr(L, index2);
288 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
292 LUA_API void lua_arith (lua_State *L, int op) {
293 StkId o1; /* 1st operand */
294 StkId o2; /* 2nd operand */
296 if (op != LUA_OPUNM) /* all other operations expect two operands */
297 api_checknelems(L, 2);
298 else { /* for unary minus, add fake 2nd operand */
299 api_checknelems(L, 1);
300 setobjs2s(L, L->top, L->top - 1);
305 if (ttisnumber(o1) && ttisnumber(o2)) {
306 setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
309 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
315 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
318 lua_lock(L); /* may call tag method */
319 o1 = index2addr(L, index1);
320 o2 = index2addr(L, index2);
321 if (isvalid(o1) && isvalid(o2)) {
323 case LUA_OPEQ: i = equalobj(L, o1, o2); break;
324 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
325 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
326 default: api_check(L, 0, "invalid option");
334 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
336 const TValue *o = index2addr(L, idx);
337 if (tonumber(o, &n)) {
338 if (isnum) *isnum = 1;
342 if (isnum) *isnum = 0;
348 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
350 const TValue *o = index2addr(L, idx);
351 if (tonumber(o, &n)) {
353 lua_Number num = nvalue(o);
354 lua_number2integer(res, num);
355 if (isnum) *isnum = 1;
359 if (isnum) *isnum = 0;
365 LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
367 const TValue *o = index2addr(L, idx);
368 if (tonumber(o, &n)) {
370 lua_Number num = nvalue(o);
371 lua_number2unsigned(res, num);
372 if (isnum) *isnum = 1;
376 if (isnum) *isnum = 0;
382 LUA_API int lua_toboolean (lua_State *L, int idx) {
383 const TValue *o = index2addr(L, idx);
384 return !l_isfalse(o);
388 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
389 StkId o = index2addr(L, idx);
390 if (!ttisstring(o)) {
391 lua_lock(L); /* `luaV_tostring' may create a new string */
392 if (!luaV_tostring(L, o)) { /* conversion failed? */
393 if (len != NULL) *len = 0;
398 o = index2addr(L, idx); /* previous call may reallocate the stack */
401 if (len != NULL) *len = tsvalue(o)->len;
406 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
407 StkId o = index2addr(L, idx);
408 switch (ttypenv(o)) {
409 case LUA_TSTRING: return tsvalue(o)->len;
410 case LUA_TUSERDATA: return uvalue(o)->len;
411 case LUA_TTABLE: return luaH_getn(hvalue(o));
417 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
418 StkId o = index2addr(L, idx);
419 if (ttislcf(o)) return fvalue(o);
420 else if (ttisCclosure(o))
421 return clCvalue(o)->f;
422 else return NULL; /* not a C function */
426 LUA_API void *lua_touserdata (lua_State *L, int idx) {
427 StkId o = index2addr(L, idx);
428 switch (ttypenv(o)) {
429 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
430 case LUA_TLIGHTUSERDATA: return pvalue(o);
431 default: return NULL;
436 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
437 StkId o = index2addr(L, idx);
438 return (!ttisthread(o)) ? NULL : thvalue(o);
442 LUA_API const void *lua_topointer (lua_State *L, int idx) {
443 StkId o = index2addr(L, idx);
445 case LUA_TTABLE: return hvalue(o);
446 case LUA_TLCL: return clLvalue(o);
447 case LUA_TCCL: return clCvalue(o);
448 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
449 case LUA_TTHREAD: return thvalue(o);
451 case LUA_TLIGHTUSERDATA:
452 return lua_touserdata(L, idx);
453 default: return NULL;
460 ** push functions (C -> stack)
464 LUA_API void lua_pushnil (lua_State *L) {
472 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
474 setnvalue(L->top, n);
475 luai_checknum(L, L->top,
476 luaG_runerror(L, "C API - attempt to push a signaling NaN"));
482 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
484 setnvalue(L->top, cast_num(n));
490 LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
493 n = lua_unsigned2number(u);
494 setnvalue(L->top, n);
500 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
504 ts = luaS_newlstr(L, s, len);
505 setsvalue2s(L, L->top, ts);
512 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
522 setsvalue2s(L, L->top, ts);
530 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
535 ret = luaO_pushvfstring(L, fmt, argp);
541 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
547 ret = luaO_pushvfstring(L, fmt, argp);
554 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
557 setfvalue(L->top, fn);
561 api_checknelems(L, n);
562 api_check(L, n <= MAXUPVAL, "upvalue index too large");
564 cl = luaF_newCclosure(L, n);
568 setobj2n(L, &cl->c.upvalue[n], L->top + n);
569 setclCvalue(L, L->top, cl);
576 LUA_API void lua_pushboolean (lua_State *L, int b) {
578 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
584 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
586 setpvalue(L->top, p);
592 LUA_API int lua_pushthread (lua_State *L) {
594 setthvalue(L, L->top, L);
597 return (G(L)->mainthread == L);
603 ** get functions (Lua -> stack)
607 LUA_API void lua_getglobal (lua_State *L, const char *var) {
608 Table *reg = hvalue(&G(L)->l_registry);
609 const TValue *gt; /* global table */
611 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
612 setsvalue2s(L, L->top++, luaS_new(L, var));
613 luaV_gettable(L, gt, L->top - 1, L->top - 1);
618 LUA_API void lua_gettable (lua_State *L, int idx) {
621 t = index2addr(L, idx);
622 luaV_gettable(L, t, L->top - 1, L->top - 1);
627 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
630 t = index2addr(L, idx);
631 setsvalue2s(L, L->top, luaS_new(L, k));
633 luaV_gettable(L, t, L->top - 1, L->top - 1);
638 LUA_API void lua_rawget (lua_State *L, int idx) {
641 t = index2addr(L, idx);
642 api_check(L, ttistable(t), "table expected");
643 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
648 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
651 t = index2addr(L, idx);
652 api_check(L, ttistable(t), "table expected");
653 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
659 LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
663 t = index2addr(L, idx);
664 api_check(L, ttistable(t), "table expected");
665 setpvalue(&k, cast(void *, p));
666 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
672 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
677 sethvalue(L, L->top, t);
679 if (narray > 0 || nrec > 0)
680 luaH_resize(L, t, narray, nrec);
685 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
690 obj = index2addr(L, objindex);
691 switch (ttypenv(obj)) {
693 mt = hvalue(obj)->metatable;
696 mt = uvalue(obj)->metatable;
699 mt = G(L)->mt[ttypenv(obj)];
705 sethvalue(L, L->top, mt);
714 LUA_API void lua_getuservalue (lua_State *L, int idx) {
717 o = index2addr(L, idx);
718 api_check(L, ttisuserdata(o), "userdata expected");
719 if (uvalue(o)->env) {
720 sethvalue(L, L->top, uvalue(o)->env);
729 ** set functions (stack -> Lua)
733 LUA_API void lua_setglobal (lua_State *L, const char *var) {
734 Table *reg = hvalue(&G(L)->l_registry);
735 const TValue *gt; /* global table */
737 api_checknelems(L, 1);
738 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
739 setsvalue2s(L, L->top++, luaS_new(L, var));
740 luaV_settable(L, gt, L->top - 1, L->top - 2);
741 L->top -= 2; /* pop value and key */
746 LUA_API void lua_settable (lua_State *L, int idx) {
749 api_checknelems(L, 2);
750 t = index2addr(L, idx);
751 luaV_settable(L, t, L->top - 2, L->top - 1);
752 L->top -= 2; /* pop index and value */
757 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
760 api_checknelems(L, 1);
761 t = index2addr(L, idx);
762 setsvalue2s(L, L->top++, luaS_new(L, k));
763 luaV_settable(L, t, L->top - 1, L->top - 2);
764 L->top -= 2; /* pop value and key */
769 LUA_API void lua_rawset (lua_State *L, int idx) {
772 api_checknelems(L, 2);
773 t = index2addr(L, idx);
774 api_check(L, ttistable(t), "table expected");
775 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
776 invalidateTMcache(hvalue(t));
777 luaC_barrierback(L, gcvalue(t), L->top-1);
783 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
786 api_checknelems(L, 1);
787 t = index2addr(L, idx);
788 api_check(L, ttistable(t), "table expected");
789 luaH_setint(L, hvalue(t), n, L->top - 1);
790 luaC_barrierback(L, gcvalue(t), L->top-1);
796 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
800 api_checknelems(L, 1);
801 t = index2addr(L, idx);
802 api_check(L, ttistable(t), "table expected");
803 setpvalue(&k, cast(void *, p));
804 setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
805 luaC_barrierback(L, gcvalue(t), L->top - 1);
811 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
815 api_checknelems(L, 1);
816 obj = index2addr(L, objindex);
817 if (ttisnil(L->top - 1))
820 api_check(L, ttistable(L->top - 1), "table expected");
821 mt = hvalue(L->top - 1);
823 switch (ttypenv(obj)) {
825 hvalue(obj)->metatable = mt;
827 luaC_objbarrierback(L, gcvalue(obj), mt);
828 luaC_checkfinalizer(L, gcvalue(obj), mt);
832 case LUA_TUSERDATA: {
833 uvalue(obj)->metatable = mt;
835 luaC_objbarrier(L, rawuvalue(obj), mt);
836 luaC_checkfinalizer(L, gcvalue(obj), mt);
841 G(L)->mt[ttypenv(obj)] = mt;
851 LUA_API void lua_setuservalue (lua_State *L, int idx) {
854 api_checknelems(L, 1);
855 o = index2addr(L, idx);
856 api_check(L, ttisuserdata(o), "userdata expected");
857 if (ttisnil(L->top - 1))
858 uvalue(o)->env = NULL;
860 api_check(L, ttistable(L->top - 1), "table expected");
861 uvalue(o)->env = hvalue(L->top - 1);
862 luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
870 ** `load' and `call' functions (run Lua code)
874 #define checkresults(L,na,nr) \
875 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
876 "results from function overflow current stack size")
879 LUA_API int lua_getctx (lua_State *L, int *ctx) {
880 if (L->ci->callstatus & CIST_YIELDED) {
881 if (ctx) *ctx = L->ci->u.c.ctx;
882 return L->ci->u.c.status;
888 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
892 api_check(L, k == NULL || !isLua(L->ci),
893 "cannot use continuations inside hooks");
894 api_checknelems(L, nargs+1);
895 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
896 checkresults(L, nargs, nresults);
897 func = L->top - (nargs+1);
898 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
899 L->ci->u.c.k = k; /* save continuation */
900 L->ci->u.c.ctx = ctx; /* save context */
901 luaD_call(L, func, nresults, 1); /* do the call */
903 else /* no continuation or no yieldable */
904 luaD_call(L, func, nresults, 0); /* just do the call */
905 adjustresults(L, nresults);
912 ** Execute a protected call.
914 struct CallS { /* data to `f_call' */
920 static void f_call (lua_State *L, void *ud) {
921 struct CallS *c = cast(struct CallS *, ud);
922 luaD_call(L, c->func, c->nresults, 0);
927 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
928 int ctx, lua_CFunction k) {
933 api_check(L, k == NULL || !isLua(L->ci),
934 "cannot use continuations inside hooks");
935 api_checknelems(L, nargs+1);
936 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
937 checkresults(L, nargs, nresults);
941 StkId o = index2addr(L, errfunc);
942 api_checkstackindex(L, errfunc, o);
943 func = savestack(L, o);
945 c.func = L->top - (nargs+1); /* function to be called */
946 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
947 c.nresults = nresults; /* do a 'conventional' protected call */
948 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
950 else { /* prepare continuation (call is already protected by 'resume') */
951 CallInfo *ci = L->ci;
952 ci->u.c.k = k; /* save continuation */
953 ci->u.c.ctx = ctx; /* save context */
954 /* save information for error recovery */
955 ci->extra = savestack(L, c.func);
956 ci->u.c.old_allowhook = L->allowhook;
957 ci->u.c.old_errfunc = L->errfunc;
959 /* mark that function may do error recovery */
960 ci->callstatus |= CIST_YPCALL;
961 luaD_call(L, c.func, nresults, 1); /* do the call */
962 ci->callstatus &= ~CIST_YPCALL;
963 L->errfunc = ci->u.c.old_errfunc;
964 status = LUA_OK; /* if it is here, there were no errors */
966 adjustresults(L, nresults);
972 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
973 const char *chunkname, const char *mode) {
977 if (!chunkname) chunkname = "?";
978 luaZ_init(L, &z, reader, data);
979 status = luaD_protectedparser(L, &z, chunkname, mode);
980 if (status == LUA_OK) { /* no errors? */
981 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
982 if (f->nupvalues == 1) { /* does it have one upvalue? */
983 /* get global table from registry */
984 Table *reg = hvalue(&G(L)->l_registry);
985 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
986 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
987 setobj(L, f->upvals[0]->v, gt);
988 luaC_barrier(L, f->upvals[0], gt);
996 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
1000 api_checknelems(L, 1);
1003 status = luaU_dump(L, getproto(o), writer, data, 0);
1011 LUA_API int lua_status (lua_State *L) {
1017 ** Garbage-collection function
1020 LUA_API int lua_gc (lua_State *L, int what, int data) {
1030 case LUA_GCRESTART: {
1035 case LUA_GCCOLLECT: {
1040 /* GC values are expressed in Kbytes: #bytes/2^10 */
1041 res = cast_int(gettotalbytes(g) >> 10);
1044 case LUA_GCCOUNTB: {
1045 res = cast_int(gettotalbytes(g) & 0x3ff);
1049 if (g->gckind == KGC_GEN) { /* generational mode? */
1050 res = (g->GCestimate == 0); /* true if it will do major collection */
1051 luaC_forcestep(L); /* do a single step */
1054 lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
1056 debt += g->GCdebt; /* include current debt */
1057 luaE_setdebt(g, debt);
1059 if (g->gcstate == GCSpause) /* end of cycle? */
1060 res = 1; /* signal it */
1064 case LUA_GCSETPAUSE: {
1069 case LUA_GCSETMAJORINC: {
1070 res = g->gcmajorinc;
1071 g->gcmajorinc = data;
1074 case LUA_GCSETSTEPMUL: {
1076 g->gcstepmul = data;
1079 case LUA_GCISRUNNING: {
1083 case LUA_GCGEN: { /* change collector to generational mode */
1084 luaC_changemode(L, KGC_GEN);
1087 case LUA_GCINC: { /* change collector to incremental mode */
1088 luaC_changemode(L, KGC_NORMAL);
1091 default: res = -1; /* invalid option */
1100 ** miscellaneous functions
1104 LUA_API int lua_error (lua_State *L) {
1106 api_checknelems(L, 1);
1108 /* code unreachable; will unlock when control actually leaves the kernel */
1109 return 0; /* to avoid warnings */
1113 LUA_API int lua_next (lua_State *L, int idx) {
1117 t = index2addr(L, idx);
1118 api_check(L, ttistable(t), "table expected");
1119 more = luaH_next(L, hvalue(t), L->top - 1);
1123 else /* no more elements */
1124 L->top -= 1; /* remove key */
1130 LUA_API void lua_concat (lua_State *L, int n) {
1132 api_checknelems(L, n);
1137 else if (n == 0) { /* push empty string */
1138 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1141 /* else n == 1; nothing to do */
1146 LUA_API void lua_len (lua_State *L, int idx) {
1149 t = index2addr(L, idx);
1150 luaV_objlen(L, L->top, t);
1156 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1159 if (ud) *ud = G(L)->ud;
1166 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1174 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1178 u = luaS_newudata(L, size, NULL);
1179 setuvalue(L, L->top, u);
1187 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1189 switch (ttype(fi)) {
1190 case LUA_TCCL: { /* C closure */
1191 CClosure *f = clCvalue(fi);
1192 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1193 *val = &f->upvalue[n-1];
1194 if (owner) *owner = obj2gco(f);
1197 case LUA_TLCL: { /* Lua closure */
1198 LClosure *f = clLvalue(fi);
1201 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1202 *val = f->upvals[n-1]->v;
1203 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1204 name = p->upvalues[n-1].name;
1205 return (name == NULL) ? "" : getstr(name);
1207 default: return NULL; /* not a closure */
1212 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1214 TValue *val = NULL; /* to avoid warnings */
1216 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1218 setobj2s(L, L->top, val);
1226 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1228 TValue *val = NULL; /* to avoid warnings */
1229 GCObject *owner = NULL; /* to avoid warnings */
1232 fi = index2addr(L, funcindex);
1233 api_checknelems(L, 1);
1234 name = aux_upvalue(fi, n, &val, &owner);
1237 setobj(L, val, L->top);
1238 luaC_barrier(L, owner, L->top);
1245 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1247 StkId fi = index2addr(L, fidx);
1248 api_check(L, ttisLclosure(fi), "Lua function expected");
1250 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1252 return &f->upvals[n - 1]; /* get its upvalue pointer */
1256 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1257 StkId fi = index2addr(L, fidx);
1258 switch (ttype(fi)) {
1259 case LUA_TLCL: { /* lua closure */
1260 return *getupvalref(L, fidx, n, NULL);
1262 case LUA_TCCL: { /* C closure */
1263 CClosure *f = clCvalue(fi);
1264 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1265 return &f->upvalue[n - 1];
1268 api_check(L, 0, "closure expected");
1275 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1276 int fidx2, int n2) {
1278 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1279 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1281 luaC_objbarrier(L, f1, *up2);