3 ** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
5 ** See Copyright Notice in lua.h
12 #include <sys/lua/lua.h>
29 const char lua_ident[] =
30 "$LuaVersion: " LUA_COPYRIGHT " $"
31 "$LuaAuthors: " LUA_AUTHORS " $";
34 /* value at a non-valid index */
35 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
37 /* corresponding test */
38 #define isvalid(o) ((o) != luaO_nilobject)
40 /* test for pseudo index */
41 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
43 /* test for valid but not pseudo index */
44 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
46 #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
48 #define api_checkstackindex(L, i, o) \
49 api_check(L, isstackindex(i, o), "index not in the stack")
52 static TValue *index2addr (lua_State *L, int idx) {
55 TValue *o = ci->func + idx;
56 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
57 if (o >= L->top) return NONVALIDVALUE;
60 else if (!ispseudo(idx)) { /* negative index */
61 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
64 else if (idx == LUA_REGISTRYINDEX)
65 return &G(L)->l_registry;
67 idx = LUA_REGISTRYINDEX - idx;
68 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
69 if (ttislcf(ci->func)) /* light C function? */
70 return NONVALIDVALUE; /* it has no upvalues */
72 CClosure *func = clCvalue(ci->func);
73 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
80 ** to be called by 'lua_checkstack' in protected mode, to grow stack
81 ** capturing memory errors
83 static void growstack (lua_State *L, void *ud) {
84 int size = *(int *)ud;
85 luaD_growstack(L, size);
89 LUA_API int lua_checkstack (lua_State *L, int size) {
93 if (L->stack_last - L->top > size) /* stack large enough? */
94 res = 1; /* yes; check is OK */
95 else { /* no; need to grow stack */
96 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
97 if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
99 else /* try to grow stack */
100 res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
102 if (res && ci->top < L->top + size)
103 ci->top = L->top + size; /* adjust frame top */
109 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
111 if (from == to) return;
113 api_checknelems(from, n);
114 api_check(from, G(from) == G(to), "moving among independent states");
115 api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
117 for (i = 0; i < n; i++) {
118 setobj2s(to, to->top++, from->top + i);
124 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
128 G(L)->panic = panicf;
134 LUA_API const lua_Number *lua_version (lua_State *L) {
135 static const lua_Number version = LUA_VERSION_NUM;
136 if (L == NULL) return &version;
137 else return G(L)->version;
143 ** basic stack manipulation
148 ** convert an acceptable stack index into an absolute index
150 LUA_API int lua_absindex (lua_State *L, int idx) {
151 return (idx > 0 || ispseudo(idx))
153 : cast_int(L->top - L->ci->func + idx);
157 LUA_API int lua_gettop (lua_State *L) {
158 return cast_int(L->top - (L->ci->func + 1));
162 LUA_API void lua_settop (lua_State *L, int idx) {
163 StkId func = L->ci->func;
166 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
167 while (L->top < (func + 1) + idx)
168 setnilvalue(L->top++);
169 L->top = (func + 1) + idx;
172 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
173 L->top += idx+1; /* `subtract' index (index is negative) */
179 LUA_API void lua_remove (lua_State *L, int idx) {
182 p = index2addr(L, idx);
183 api_checkstackindex(L, idx, p);
184 while (++p < L->top) setobjs2s(L, p-1, p);
190 LUA_API void lua_insert (lua_State *L, int idx) {
194 p = index2addr(L, idx);
195 api_checkstackindex(L, idx, p);
196 for (q = L->top; q > p; q--) /* use L->top as a temporary */
197 setobjs2s(L, q, q - 1);
198 setobjs2s(L, p, L->top);
203 static void moveto (lua_State *L, TValue *fr, int idx) {
204 TValue *to = index2addr(L, idx);
205 api_checkvalidindex(L, to);
207 if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
208 luaC_barrier(L, clCvalue(L->ci->func), fr);
209 /* LUA_REGISTRYINDEX does not need gc barrier
210 (collector revisits it before finishing collection) */
214 LUA_API void lua_replace (lua_State *L, int idx) {
216 api_checknelems(L, 1);
217 moveto(L, L->top - 1, idx);
223 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
226 fr = index2addr(L, fromidx);
227 moveto(L, fr, toidx);
232 LUA_API void lua_pushvalue (lua_State *L, int idx) {
234 setobj2s(L, L->top, index2addr(L, idx));
242 ** access functions (stack -> C)
246 LUA_API int lua_type (lua_State *L, int idx) {
247 StkId o = index2addr(L, idx);
248 return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
252 LUA_API const char *lua_typename (lua_State *L, int t) {
258 LUA_API int lua_iscfunction (lua_State *L, int idx) {
259 StkId o = index2addr(L, idx);
260 return (ttislcf(o) || (ttisCclosure(o)));
264 LUA_API int lua_isnumber (lua_State *L, int idx) {
266 const TValue *o = index2addr(L, idx);
267 return tonumber(o, &n);
271 LUA_API int lua_isstring (lua_State *L, int idx) {
272 int t = lua_type(L, idx);
273 return (t == LUA_TSTRING || t == LUA_TNUMBER);
277 LUA_API int lua_isuserdata (lua_State *L, int idx) {
278 const TValue *o = index2addr(L, idx);
279 return (ttisuserdata(o) || ttislightuserdata(o));
283 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
284 StkId o1 = index2addr(L, index1);
285 StkId o2 = index2addr(L, index2);
286 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
290 LUA_API void lua_arith (lua_State *L, int op) {
291 StkId o1; /* 1st operand */
292 StkId o2; /* 2nd operand */
294 if (op != LUA_OPUNM) /* all other operations expect two operands */
295 api_checknelems(L, 2);
296 else { /* for unary minus, add fake 2nd operand */
297 api_checknelems(L, 1);
298 setobjs2s(L, L->top, L->top - 1);
303 if (ttisnumber(o1) && ttisnumber(o2)) {
304 setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
307 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
313 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
316 lua_lock(L); /* may call tag method */
317 o1 = index2addr(L, index1);
318 o2 = index2addr(L, index2);
319 if (isvalid(o1) && isvalid(o2)) {
321 case LUA_OPEQ: i = equalobj(L, o1, o2); break;
322 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
323 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
324 default: api_check(L, 0, "invalid option");
332 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
334 const TValue *o = index2addr(L, idx);
335 if (tonumber(o, &n)) {
336 if (isnum) *isnum = 1;
340 if (isnum) *isnum = 0;
346 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
348 const TValue *o = index2addr(L, idx);
349 if (tonumber(o, &n)) {
351 lua_Number num = nvalue(o);
352 lua_number2integer(res, num);
353 if (isnum) *isnum = 1;
357 if (isnum) *isnum = 0;
363 LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
365 const TValue *o = index2addr(L, idx);
366 if (tonumber(o, &n)) {
368 lua_Number num = nvalue(o);
369 lua_number2unsigned(res, num);
370 if (isnum) *isnum = 1;
374 if (isnum) *isnum = 0;
380 LUA_API int lua_toboolean (lua_State *L, int idx) {
381 const TValue *o = index2addr(L, idx);
382 return !l_isfalse(o);
386 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
387 StkId o = index2addr(L, idx);
388 if (!ttisstring(o)) {
389 lua_lock(L); /* `luaV_tostring' may create a new string */
390 if (!luaV_tostring(L, o)) { /* conversion failed? */
391 if (len != NULL) *len = 0;
396 o = index2addr(L, idx); /* previous call may reallocate the stack */
399 if (len != NULL) *len = tsvalue(o)->len;
404 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
405 StkId o = index2addr(L, idx);
406 switch (ttypenv(o)) {
407 case LUA_TSTRING: return tsvalue(o)->len;
408 case LUA_TUSERDATA: return uvalue(o)->len;
409 case LUA_TTABLE: return luaH_getn(hvalue(o));
415 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
416 StkId o = index2addr(L, idx);
417 if (ttislcf(o)) return fvalue(o);
418 else if (ttisCclosure(o))
419 return clCvalue(o)->f;
420 else return NULL; /* not a C function */
424 LUA_API void *lua_touserdata (lua_State *L, int idx) {
425 StkId o = index2addr(L, idx);
426 switch (ttypenv(o)) {
427 case LUA_TUSERDATA: return ((void *)(rawuvalue(o) + 1));
428 case LUA_TLIGHTUSERDATA: return pvalue(o);
429 default: return NULL;
434 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
435 StkId o = index2addr(L, idx);
436 return (!ttisthread(o)) ? NULL : thvalue(o);
440 LUA_API const void *lua_topointer (lua_State *L, int idx) {
441 StkId o = index2addr(L, idx);
443 case LUA_TTABLE: return hvalue(o);
444 case LUA_TLCL: return clLvalue(o);
445 case LUA_TCCL: return clCvalue(o);
446 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
447 case LUA_TTHREAD: return thvalue(o);
449 case LUA_TLIGHTUSERDATA:
450 return lua_touserdata(L, idx);
451 default: return NULL;
458 ** push functions (C -> stack)
462 LUA_API void lua_pushnil (lua_State *L) {
470 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
472 setnvalue(L->top, n);
473 luai_checknum(L, L->top,
474 luaG_runerror(L, "C API - attempt to push a signaling NaN"));
480 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
482 setnvalue(L->top, cast_num(n));
488 LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
491 n = lua_unsigned2number(u);
492 setnvalue(L->top, n);
498 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
502 ts = luaS_newlstr(L, s, len);
503 setsvalue2s(L, L->top, ts);
510 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
520 setsvalue2s(L, L->top, ts);
528 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
533 ret = luaO_pushvfstring(L, fmt, argp);
539 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
545 ret = luaO_pushvfstring(L, fmt, argp);
552 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
555 setfvalue(L->top, fn);
559 api_checknelems(L, n);
560 api_check(L, n <= MAXUPVAL, "upvalue index too large");
562 cl = luaF_newCclosure(L, n);
566 setobj2n(L, &cl->c.upvalue[n], L->top + n);
567 setclCvalue(L, L->top, cl);
574 LUA_API void lua_pushboolean (lua_State *L, int b) {
576 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
582 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
584 setpvalue(L->top, p);
590 LUA_API int lua_pushthread (lua_State *L) {
592 setthvalue(L, L->top, L);
595 return (G(L)->mainthread == L);
601 ** get functions (Lua -> stack)
605 LUA_API void lua_getglobal (lua_State *L, const char *var) {
606 Table *reg = hvalue(&G(L)->l_registry);
607 const TValue *gt; /* global table */
609 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
610 setsvalue2s(L, L->top++, luaS_new(L, var));
611 luaV_gettable(L, gt, L->top - 1, L->top - 1);
616 LUA_API void lua_gettable (lua_State *L, int idx) {
619 t = index2addr(L, idx);
620 luaV_gettable(L, t, L->top - 1, L->top - 1);
625 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
628 t = index2addr(L, idx);
629 setsvalue2s(L, L->top, luaS_new(L, k));
631 luaV_gettable(L, t, L->top - 1, L->top - 1);
636 LUA_API void lua_rawget (lua_State *L, int idx) {
639 t = index2addr(L, idx);
640 api_check(L, ttistable(t), "table expected");
641 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
646 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
649 t = index2addr(L, idx);
650 api_check(L, ttistable(t), "table expected");
651 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
657 LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
661 t = index2addr(L, idx);
662 api_check(L, ttistable(t), "table expected");
663 setpvalue(&k, cast(void *, p));
664 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
670 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
675 sethvalue(L, L->top, t);
677 if (narray > 0 || nrec > 0)
678 luaH_resize(L, t, narray, nrec);
683 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
688 obj = index2addr(L, objindex);
689 switch (ttypenv(obj)) {
691 mt = hvalue(obj)->metatable;
694 mt = uvalue(obj)->metatable;
697 mt = G(L)->mt[ttypenv(obj)];
703 sethvalue(L, L->top, mt);
712 LUA_API void lua_getuservalue (lua_State *L, int idx) {
715 o = index2addr(L, idx);
716 api_check(L, ttisuserdata(o), "userdata expected");
717 if (uvalue(o)->env) {
718 sethvalue(L, L->top, uvalue(o)->env);
727 ** set functions (stack -> Lua)
731 LUA_API void lua_setglobal (lua_State *L, const char *var) {
732 Table *reg = hvalue(&G(L)->l_registry);
733 const TValue *gt; /* global table */
735 api_checknelems(L, 1);
736 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
737 setsvalue2s(L, L->top++, luaS_new(L, var));
738 luaV_settable(L, gt, L->top - 1, L->top - 2);
739 L->top -= 2; /* pop value and key */
744 LUA_API void lua_settable (lua_State *L, int idx) {
747 api_checknelems(L, 2);
748 t = index2addr(L, idx);
749 luaV_settable(L, t, L->top - 2, L->top - 1);
750 L->top -= 2; /* pop index and value */
755 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
758 api_checknelems(L, 1);
759 t = index2addr(L, idx);
760 setsvalue2s(L, L->top++, luaS_new(L, k));
761 luaV_settable(L, t, L->top - 1, L->top - 2);
762 L->top -= 2; /* pop value and key */
767 LUA_API void lua_rawset (lua_State *L, int idx) {
770 api_checknelems(L, 2);
771 t = index2addr(L, idx);
772 api_check(L, ttistable(t), "table expected");
773 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
774 invalidateTMcache(hvalue(t));
775 luaC_barrierback(L, gcvalue(t), L->top-1);
781 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
784 api_checknelems(L, 1);
785 t = index2addr(L, idx);
786 api_check(L, ttistable(t), "table expected");
787 luaH_setint(L, hvalue(t), n, L->top - 1);
788 luaC_barrierback(L, gcvalue(t), L->top-1);
794 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
798 api_checknelems(L, 1);
799 t = index2addr(L, idx);
800 api_check(L, ttistable(t), "table expected");
801 setpvalue(&k, cast(void *, p));
802 setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
803 luaC_barrierback(L, gcvalue(t), L->top - 1);
809 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
813 api_checknelems(L, 1);
814 obj = index2addr(L, objindex);
815 if (ttisnil(L->top - 1))
818 api_check(L, ttistable(L->top - 1), "table expected");
819 mt = hvalue(L->top - 1);
821 switch (ttypenv(obj)) {
823 hvalue(obj)->metatable = mt;
825 luaC_objbarrierback(L, gcvalue(obj), mt);
826 luaC_checkfinalizer(L, gcvalue(obj), mt);
830 case LUA_TUSERDATA: {
831 uvalue(obj)->metatable = mt;
833 luaC_objbarrier(L, rawuvalue(obj), mt);
834 luaC_checkfinalizer(L, gcvalue(obj), mt);
839 G(L)->mt[ttypenv(obj)] = mt;
849 LUA_API void lua_setuservalue (lua_State *L, int idx) {
852 api_checknelems(L, 1);
853 o = index2addr(L, idx);
854 api_check(L, ttisuserdata(o), "userdata expected");
855 if (ttisnil(L->top - 1))
856 uvalue(o)->env = NULL;
858 api_check(L, ttistable(L->top - 1), "table expected");
859 uvalue(o)->env = hvalue(L->top - 1);
860 luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
868 ** `load' and `call' functions (run Lua code)
872 #define checkresults(L,na,nr) \
873 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
874 "results from function overflow current stack size")
877 LUA_API int lua_getctx (lua_State *L, int *ctx) {
878 if (L->ci->callstatus & CIST_YIELDED) {
879 if (ctx) *ctx = L->ci->u.c.ctx;
880 return L->ci->u.c.status;
886 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
890 api_check(L, k == NULL || !isLua(L->ci),
891 "cannot use continuations inside hooks");
892 api_checknelems(L, nargs+1);
893 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
894 checkresults(L, nargs, nresults);
895 func = L->top - (nargs+1);
896 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
897 L->ci->u.c.k = k; /* save continuation */
898 L->ci->u.c.ctx = ctx; /* save context */
899 luaD_call(L, func, nresults, 1); /* do the call */
901 else /* no continuation or no yieldable */
902 luaD_call(L, func, nresults, 0); /* just do the call */
903 adjustresults(L, nresults);
910 ** Execute a protected call.
912 struct CallS { /* data to `f_call' */
918 static void f_call (lua_State *L, void *ud) {
919 struct CallS *c = cast(struct CallS *, ud);
920 luaD_call(L, c->func, c->nresults, 0);
925 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
926 int ctx, lua_CFunction k) {
931 api_check(L, k == NULL || !isLua(L->ci),
932 "cannot use continuations inside hooks");
933 api_checknelems(L, nargs+1);
934 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
935 checkresults(L, nargs, nresults);
939 StkId o = index2addr(L, errfunc);
940 api_checkstackindex(L, errfunc, o);
941 func = savestack(L, o);
943 c.func = L->top - (nargs+1); /* function to be called */
944 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
945 c.nresults = nresults; /* do a 'conventional' protected call */
946 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
948 else { /* prepare continuation (call is already protected by 'resume') */
949 CallInfo *ci = L->ci;
950 ci->u.c.k = k; /* save continuation */
951 ci->u.c.ctx = ctx; /* save context */
952 /* save information for error recovery */
953 ci->extra = savestack(L, c.func);
954 ci->u.c.old_allowhook = L->allowhook;
955 ci->u.c.old_errfunc = L->errfunc;
957 /* mark that function may do error recovery */
958 ci->callstatus |= CIST_YPCALL;
959 luaD_call(L, c.func, nresults, 1); /* do the call */
960 ci->callstatus &= ~CIST_YPCALL;
961 L->errfunc = ci->u.c.old_errfunc;
962 status = LUA_OK; /* if it is here, there were no errors */
964 adjustresults(L, nresults);
970 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
971 const char *chunkname, const char *mode) {
975 if (!chunkname) chunkname = "?";
976 luaZ_init(L, &z, reader, data);
977 status = luaD_protectedparser(L, &z, chunkname, mode);
978 if (status == LUA_OK) { /* no errors? */
979 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
980 if (f->nupvalues == 1) { /* does it have one upvalue? */
981 /* get global table from registry */
982 Table *reg = hvalue(&G(L)->l_registry);
983 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
984 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
985 setobj(L, f->upvals[0]->v, gt);
986 luaC_barrier(L, f->upvals[0], gt);
993 #if defined(LUA_USE_DUMP)
994 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
998 api_checknelems(L, 1);
1001 status = luaU_dump(L, getproto(o), writer, data, 0);
1009 LUA_API int lua_status (lua_State *L) {
1015 ** Garbage-collection function
1018 LUA_API int lua_gc (lua_State *L, int what, int data) {
1028 case LUA_GCRESTART: {
1033 case LUA_GCCOLLECT: {
1038 /* GC values are expressed in Kbytes: #bytes/2^10 */
1039 res = cast_int(gettotalbytes(g) >> 10);
1042 case LUA_GCCOUNTB: {
1043 res = cast_int(gettotalbytes(g) & 0x3ff);
1047 if (g->gckind == KGC_GEN) { /* generational mode? */
1048 res = (g->GCestimate == 0); /* true if it will do major collection */
1049 luaC_forcestep(L); /* do a single step */
1052 lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
1054 debt += g->GCdebt; /* include current debt */
1055 luaE_setdebt(g, debt);
1057 if (g->gcstate == GCSpause) /* end of cycle? */
1058 res = 1; /* signal it */
1062 case LUA_GCSETPAUSE: {
1067 case LUA_GCSETMAJORINC: {
1068 res = g->gcmajorinc;
1069 g->gcmajorinc = data;
1072 case LUA_GCSETSTEPMUL: {
1074 g->gcstepmul = data;
1077 case LUA_GCISRUNNING: {
1081 case LUA_GCGEN: { /* change collector to generational mode */
1082 luaC_changemode(L, KGC_GEN);
1085 case LUA_GCINC: { /* change collector to incremental mode */
1086 luaC_changemode(L, KGC_NORMAL);
1089 default: res = -1; /* invalid option */
1098 ** miscellaneous functions
1102 LUA_API int lua_error (lua_State *L) {
1104 api_checknelems(L, 1);
1106 /* code unreachable; will unlock when control actually leaves the kernel */
1107 return 0; /* to avoid warnings */
1111 LUA_API int lua_next (lua_State *L, int idx) {
1115 t = index2addr(L, idx);
1116 api_check(L, ttistable(t), "table expected");
1117 more = luaH_next(L, hvalue(t), L->top - 1);
1121 else /* no more elements */
1122 L->top -= 1; /* remove key */
1128 LUA_API void lua_concat (lua_State *L, int n) {
1130 api_checknelems(L, n);
1135 else if (n == 0) { /* push empty string */
1136 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1139 /* else n == 1; nothing to do */
1144 LUA_API void lua_len (lua_State *L, int idx) {
1147 t = index2addr(L, idx);
1148 luaV_objlen(L, L->top, t);
1154 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1157 if (ud) *ud = G(L)->ud;
1164 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1172 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1176 u = luaS_newudata(L, size, NULL);
1177 setuvalue(L, L->top, u);
1185 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1187 switch (ttype(fi)) {
1188 case LUA_TCCL: { /* C closure */
1189 CClosure *f = clCvalue(fi);
1190 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1191 *val = &f->upvalue[n-1];
1192 if (owner) *owner = obj2gco(f);
1195 case LUA_TLCL: { /* Lua closure */
1196 LClosure *f = clLvalue(fi);
1199 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1200 *val = f->upvals[n-1]->v;
1201 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1202 name = p->upvalues[n-1].name;
1203 return (name == NULL) ? "" : getstr(name);
1205 default: return NULL; /* not a closure */
1210 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1212 TValue *val = NULL; /* to avoid warnings */
1214 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1216 setobj2s(L, L->top, val);
1224 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1226 TValue *val = NULL; /* to avoid warnings */
1227 GCObject *owner = NULL; /* to avoid warnings */
1230 fi = index2addr(L, funcindex);
1231 api_checknelems(L, 1);
1232 name = aux_upvalue(fi, n, &val, &owner);
1235 setobj(L, val, L->top);
1236 luaC_barrier(L, owner, L->top);
1243 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1245 StkId fi = index2addr(L, fidx);
1246 api_check(L, ttisLclosure(fi), "Lua function expected");
1248 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1250 return &f->upvals[n - 1]; /* get its upvalue pointer */
1254 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1255 StkId fi = index2addr(L, fidx);
1256 switch (ttype(fi)) {
1257 case LUA_TLCL: { /* lua closure */
1258 return *getupvalref(L, fidx, n, NULL);
1260 case LUA_TCCL: { /* C closure */
1261 CClosure *f = clCvalue(fi);
1262 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1263 return &f->upvalue[n - 1];
1266 api_check(L, 0, "closure expected");
1273 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1274 int fidx2, int n2) {
1276 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1277 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1279 luaC_objbarrier(L, f1, *up2);
1282 #if defined(_KERNEL)
1295 module_init(lua_init);
1296 module_exit(lua_fini);
1301 ZFS_MODULE_DESCRIPTION("Lua Interpreter for ZFS");
1302 ZFS_MODULE_AUTHOR("Lua.org");
1303 ZFS_MODULE_LICENSE("MIT");
1304 ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
1306 EXPORT_SYMBOL(lua_absindex);
1307 EXPORT_SYMBOL(lua_atpanic);
1308 EXPORT_SYMBOL(lua_checkstack);
1309 EXPORT_SYMBOL(lua_close);
1310 EXPORT_SYMBOL(lua_createtable);
1311 EXPORT_SYMBOL(lua_error);
1312 EXPORT_SYMBOL(lua_getfield);
1313 EXPORT_SYMBOL(lua_gettable);
1314 EXPORT_SYMBOL(lua_gettop);
1315 EXPORT_SYMBOL(lua_isnumber);
1316 EXPORT_SYMBOL(lua_isstring);
1317 EXPORT_SYMBOL(lua_newstate);
1318 EXPORT_SYMBOL(lua_newuserdata);
1319 EXPORT_SYMBOL(lua_next);
1320 EXPORT_SYMBOL(lua_pcallk);
1321 EXPORT_SYMBOL(lua_pushboolean);
1322 EXPORT_SYMBOL(lua_pushcclosure);
1323 EXPORT_SYMBOL(lua_pushfstring);
1324 EXPORT_SYMBOL(lua_pushinteger);
1325 EXPORT_SYMBOL(lua_pushlightuserdata);
1326 EXPORT_SYMBOL(lua_pushnil);
1327 EXPORT_SYMBOL(lua_pushnumber);
1328 EXPORT_SYMBOL(lua_pushstring);
1329 EXPORT_SYMBOL(lua_pushvalue);
1330 EXPORT_SYMBOL(lua_pushvfstring);
1331 EXPORT_SYMBOL(lua_remove);
1332 EXPORT_SYMBOL(lua_replace);
1333 EXPORT_SYMBOL(lua_setfield);
1334 EXPORT_SYMBOL(lua_setglobal);
1335 EXPORT_SYMBOL(lua_sethook);
1336 EXPORT_SYMBOL(lua_setmetatable);
1337 EXPORT_SYMBOL(lua_settable);
1338 EXPORT_SYMBOL(lua_settop);
1339 EXPORT_SYMBOL(lua_toboolean);
1340 EXPORT_SYMBOL(lua_tointegerx);
1341 EXPORT_SYMBOL(lua_tolstring);
1342 EXPORT_SYMBOL(lua_tonumberx);
1343 EXPORT_SYMBOL(lua_touserdata);
1344 EXPORT_SYMBOL(lua_type);
1345 EXPORT_SYMBOL(lua_typename);