2 ** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $
4 ** See Copyright Notice in lua.h
34 const char lua_ident[] =
35 "$LuaVersion: " LUA_COPYRIGHT " $"
36 "$LuaAuthors: " LUA_AUTHORS " $";
39 /* value at a non-valid index */
40 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
42 /* corresponding test */
43 #define isvalid(o) ((o) != luaO_nilobject)
45 /* test for pseudo index */
46 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
48 /* test for upvalue */
49 #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
51 /* test for valid but not pseudo index */
52 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
54 #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
56 #define api_checkstackindex(l, i, o) \
57 api_check(l, isstackindex(i, o), "index not in the stack")
60 static TValue *index2addr (lua_State *L, int idx) {
63 TValue *o = ci->func + idx;
64 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
65 if (o >= L->top) return NONVALIDVALUE;
68 else if (!ispseudo(idx)) { /* negative index */
69 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
72 else if (idx == LUA_REGISTRYINDEX)
73 return &G(L)->l_registry;
75 idx = LUA_REGISTRYINDEX - idx;
76 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
77 if (ttislcf(ci->func)) /* light C function? */
78 return NONVALIDVALUE; /* it has no upvalues */
80 CClosure *func = clCvalue(ci->func);
81 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
88 ** to be called by 'lua_checkstack' in protected mode, to grow stack
89 ** capturing memory errors
91 static void growstack (lua_State *L, void *ud) {
92 int size = *(int *)ud;
93 luaD_growstack(L, size);
97 LUA_API int lua_checkstack (lua_State *L, int n) {
101 api_check(L, n >= 0, "negative 'n'");
102 if (L->stack_last - L->top > n) /* stack large enough? */
103 res = 1; /* yes; check is OK */
104 else { /* no; need to grow stack */
105 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
106 if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
108 else /* try to grow stack */
109 res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
111 if (res && ci->top < L->top + n)
112 ci->top = L->top + n; /* adjust frame top */
118 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
120 if (from == to) return;
122 api_checknelems(from, n);
123 api_check(from, G(from) == G(to), "moving among independent states");
124 api_check(from, to->ci->top - to->top >= n, "stack overflow");
126 for (i = 0; i < n; i++) {
127 setobj2s(to, to->top, from->top + i);
128 to->top++; /* stack already checked by previous 'api_check' */
134 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
138 G(L)->panic = panicf;
144 LUA_API const lua_Number *lua_version (lua_State *L) {
145 static const lua_Number version = LUA_VERSION_NUM;
146 if (L == NULL) return &version;
147 else return G(L)->version;
153 ** basic stack manipulation
158 ** convert an acceptable stack index into an absolute index
160 LUA_API int lua_absindex (lua_State *L, int idx) {
161 return (idx > 0 || ispseudo(idx))
163 : cast_int(L->top - L->ci->func) + idx;
167 LUA_API int lua_gettop (lua_State *L) {
168 return cast_int(L->top - (L->ci->func + 1));
172 LUA_API void lua_settop (lua_State *L, int idx) {
173 StkId func = L->ci->func;
176 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
177 while (L->top < (func + 1) + idx)
178 setnilvalue(L->top++);
179 L->top = (func + 1) + idx;
182 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
183 L->top += idx+1; /* 'subtract' index (index is negative) */
190 ** Reverse the stack segment from 'from' to 'to'
191 ** (auxiliary to 'lua_rotate')
193 static void reverse (lua_State *L, StkId from, StkId to) {
194 for (; from < to; from++, to--) {
196 setobj(L, &temp, from);
197 setobjs2s(L, from, to);
198 setobj2s(L, to, &temp);
204 ** Let x = AB, where A is a prefix of length 'n'. Then,
205 ** rotate x n == BA. But BA == (A^r . B^r)^r.
207 LUA_API void lua_rotate (lua_State *L, int idx, int n) {
210 t = L->top - 1; /* end of stack segment being rotated */
211 p = index2addr(L, idx); /* start of segment */
212 api_checkstackindex(L, idx, p);
213 api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
214 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
215 reverse(L, p, m); /* reverse the prefix with length 'n' */
216 reverse(L, m + 1, t); /* reverse the suffix */
217 reverse(L, p, t); /* reverse the entire segment */
222 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
225 fr = index2addr(L, fromidx);
226 to = index2addr(L, toidx);
227 api_checkvalidindex(L, to);
229 if (isupvalue(toidx)) /* function upvalue? */
230 luaC_barrier(L, clCvalue(L->ci->func), fr);
231 /* LUA_REGISTRYINDEX does not need gc barrier
232 (collector revisits it before finishing collection) */
237 LUA_API void lua_pushvalue (lua_State *L, int idx) {
239 setobj2s(L, L->top, index2addr(L, idx));
247 ** access functions (stack -> C)
251 LUA_API int lua_type (lua_State *L, int idx) {
252 StkId o = index2addr(L, idx);
253 return (isvalid(o) ? ttnov(o) : LUA_TNONE);
257 LUA_API const char *lua_typename (lua_State *L, int t) {
259 api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
264 LUA_API int lua_iscfunction (lua_State *L, int idx) {
265 StkId o = index2addr(L, idx);
266 return (ttislcf(o) || (ttisCclosure(o)));
270 LUA_API int lua_isinteger (lua_State *L, int idx) {
271 StkId o = index2addr(L, idx);
272 return ttisinteger(o);
276 LUA_API int lua_isnumber (lua_State *L, int idx) {
278 const TValue *o = index2addr(L, idx);
279 return tonumber(o, &n);
283 LUA_API int lua_isstring (lua_State *L, int idx) {
284 const TValue *o = index2addr(L, idx);
285 return (ttisstring(o) || cvt2str(o));
289 LUA_API int lua_isuserdata (lua_State *L, int idx) {
290 const TValue *o = index2addr(L, idx);
291 return (ttisfulluserdata(o) || ttislightuserdata(o));
295 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
296 StkId o1 = index2addr(L, index1);
297 StkId o2 = index2addr(L, index2);
298 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
302 LUA_API void lua_arith (lua_State *L, int op) {
304 if (op != LUA_OPUNM && op != LUA_OPBNOT)
305 api_checknelems(L, 2); /* all other operations expect two operands */
306 else { /* for unary operations, add fake 2nd operand */
307 api_checknelems(L, 1);
308 setobjs2s(L, L->top, L->top - 1);
311 /* first operand at top - 2, second at top - 1; result go to top - 2 */
312 luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
313 L->top--; /* remove second operand */
318 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
321 lua_lock(L); /* may call tag method */
322 o1 = index2addr(L, index1);
323 o2 = index2addr(L, index2);
324 if (isvalid(o1) && isvalid(o2)) {
326 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
327 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
328 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
329 default: api_check(L, 0, "invalid option");
337 LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
338 size_t sz = luaO_str2num(s, L->top);
345 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
347 const TValue *o = index2addr(L, idx);
348 int isnum = tonumber(o, &n);
350 n = 0; /* call to 'tonumber' may change 'n' even if it fails */
351 if (pisnum) *pisnum = isnum;
356 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
358 const TValue *o = index2addr(L, idx);
359 int isnum = tointeger(o, &res);
361 res = 0; /* call to 'tointeger' may change 'n' even if it fails */
362 if (pisnum) *pisnum = isnum;
367 LUA_API int lua_toboolean (lua_State *L, int idx) {
368 const TValue *o = index2addr(L, idx);
369 return !l_isfalse(o);
373 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
374 StkId o = index2addr(L, idx);
375 if (!ttisstring(o)) {
376 if (!cvt2str(o)) { /* not convertible? */
377 if (len != NULL) *len = 0;
380 lua_lock(L); /* 'luaO_tostring' may create a new string */
383 o = index2addr(L, idx); /* previous call may reallocate the stack */
392 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
393 StkId o = index2addr(L, idx);
395 case LUA_TSHRSTR: return tsvalue(o)->shrlen;
396 case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
397 case LUA_TUSERDATA: return uvalue(o)->len;
398 case LUA_TTABLE: return luaH_getn(hvalue(o));
404 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
405 StkId o = index2addr(L, idx);
406 if (ttislcf(o)) return fvalue(o);
407 else if (ttisCclosure(o))
408 return clCvalue(o)->f;
409 else return NULL; /* not a C function */
413 LUA_API void *lua_touserdata (lua_State *L, int idx) {
414 StkId o = index2addr(L, idx);
416 case LUA_TUSERDATA: return getudatamem(uvalue(o));
417 case LUA_TLIGHTUSERDATA: return pvalue(o);
418 default: return NULL;
423 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
424 StkId o = index2addr(L, idx);
425 return (!ttisthread(o)) ? NULL : thvalue(o);
429 LUA_API const void *lua_topointer (lua_State *L, int idx) {
430 StkId o = index2addr(L, idx);
432 case LUA_TTABLE: return hvalue(o);
433 case LUA_TLCL: return clLvalue(o);
434 case LUA_TCCL: return clCvalue(o);
435 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
436 case LUA_TTHREAD: return thvalue(o);
437 case LUA_TUSERDATA: return getudatamem(uvalue(o));
438 case LUA_TLIGHTUSERDATA: return pvalue(o);
439 default: return NULL;
446 ** push functions (C -> stack)
450 LUA_API void lua_pushnil (lua_State *L) {
458 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
460 setfltvalue(L->top, n);
466 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
468 setivalue(L->top, n);
475 ** Pushes on the stack a string with given length. Avoid using 's' when
476 ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
477 ** 'memcmp' and 'memcpy'.
479 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
482 ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
483 setsvalue2s(L, L->top, ts);
491 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
498 setsvalue2s(L, L->top, ts);
499 s = getstr(ts); /* internal copy's address */
508 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
512 ret = luaO_pushvfstring(L, fmt, argp);
519 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
524 ret = luaO_pushvfstring(L, fmt, argp);
532 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
535 setfvalue(L->top, fn);
540 api_checknelems(L, n);
541 api_check(L, n <= MAXUPVAL, "upvalue index too large");
542 cl = luaF_newCclosure(L, n);
546 setobj2n(L, &cl->upvalue[n], L->top + n);
547 /* does not need barrier because closure is white */
549 setclCvalue(L, L->top, cl);
557 LUA_API void lua_pushboolean (lua_State *L, int b) {
559 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
565 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
567 setpvalue(L->top, p);
573 LUA_API int lua_pushthread (lua_State *L) {
575 setthvalue(L, L->top, L);
578 return (G(L)->mainthread == L);
584 ** get functions (Lua -> stack)
588 static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
590 TString *str = luaS_new(L, k);
591 if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
592 setobj2s(L, L->top, slot);
596 setsvalue2s(L, L->top, str);
598 luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
601 return ttnov(L->top - 1);
605 LUA_API int lua_getglobal (lua_State *L, const char *name) {
606 Table *reg = hvalue(&G(L)->l_registry);
608 return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
612 LUA_API int lua_gettable (lua_State *L, int idx) {
615 t = index2addr(L, idx);
616 luaV_gettable(L, t, L->top - 1, L->top - 1);
618 return ttnov(L->top - 1);
622 LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
624 return auxgetstr(L, index2addr(L, idx), k);
628 LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
632 t = index2addr(L, idx);
633 if (luaV_fastget(L, t, n, slot, luaH_getint)) {
634 setobj2s(L, L->top, slot);
638 setivalue(L->top, n);
640 luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
643 return ttnov(L->top - 1);
647 LUA_API int lua_rawget (lua_State *L, int idx) {
650 t = index2addr(L, idx);
651 api_check(L, ttistable(t), "table expected");
652 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
654 return ttnov(L->top - 1);
658 LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
661 t = index2addr(L, idx);
662 api_check(L, ttistable(t), "table expected");
663 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
666 return ttnov(L->top - 1);
670 LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
674 t = index2addr(L, idx);
675 api_check(L, ttistable(t), "table expected");
676 setpvalue(&k, cast(void *, p));
677 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
680 return ttnov(L->top - 1);
684 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
688 sethvalue(L, L->top, t);
690 if (narray > 0 || nrec > 0)
691 luaH_resize(L, t, narray, nrec);
697 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
702 obj = index2addr(L, objindex);
703 switch (ttnov(obj)) {
705 mt = hvalue(obj)->metatable;
708 mt = uvalue(obj)->metatable;
711 mt = G(L)->mt[ttnov(obj)];
715 sethvalue(L, L->top, mt);
724 LUA_API int lua_getuservalue (lua_State *L, int idx) {
727 o = index2addr(L, idx);
728 api_check(L, ttisfulluserdata(o), "full userdata expected");
729 getuservalue(L, uvalue(o), L->top);
732 return ttnov(L->top - 1);
737 ** set functions (stack -> Lua)
741 ** t[k] = value at the top of the stack (where 'k' is a string)
743 static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
745 TString *str = luaS_new(L, k);
746 api_checknelems(L, 1);
747 if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1))
748 L->top--; /* pop value */
750 setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
752 luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
753 L->top -= 2; /* pop value and key */
755 lua_unlock(L); /* lock done by caller */
759 LUA_API void lua_setglobal (lua_State *L, const char *name) {
760 Table *reg = hvalue(&G(L)->l_registry);
761 lua_lock(L); /* unlock done in 'auxsetstr' */
762 auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
766 LUA_API void lua_settable (lua_State *L, int idx) {
769 api_checknelems(L, 2);
770 t = index2addr(L, idx);
771 luaV_settable(L, t, L->top - 2, L->top - 1);
772 L->top -= 2; /* pop index and value */
777 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
778 lua_lock(L); /* unlock done in 'auxsetstr' */
779 auxsetstr(L, index2addr(L, idx), k);
783 LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
787 api_checknelems(L, 1);
788 t = index2addr(L, idx);
789 if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1))
790 L->top--; /* pop value */
792 setivalue(L->top, n);
794 luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
795 L->top -= 2; /* pop value and key */
801 LUA_API void lua_rawset (lua_State *L, int idx) {
805 api_checknelems(L, 2);
806 o = index2addr(L, idx);
807 api_check(L, ttistable(o), "table expected");
808 slot = luaH_set(L, hvalue(o), L->top - 2);
809 setobj2t(L, slot, L->top - 1);
810 invalidateTMcache(hvalue(o));
811 luaC_barrierback(L, hvalue(o), L->top-1);
817 LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
820 api_checknelems(L, 1);
821 o = index2addr(L, idx);
822 api_check(L, ttistable(o), "table expected");
823 luaH_setint(L, hvalue(o), n, L->top - 1);
824 luaC_barrierback(L, hvalue(o), L->top-1);
830 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
834 api_checknelems(L, 1);
835 o = index2addr(L, idx);
836 api_check(L, ttistable(o), "table expected");
837 setpvalue(&k, cast(void *, p));
838 slot = luaH_set(L, hvalue(o), &k);
839 setobj2t(L, slot, L->top - 1);
840 luaC_barrierback(L, hvalue(o), L->top - 1);
846 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
850 api_checknelems(L, 1);
851 obj = index2addr(L, objindex);
852 if (ttisnil(L->top - 1))
855 api_check(L, ttistable(L->top - 1), "table expected");
856 mt = hvalue(L->top - 1);
858 switch (ttnov(obj)) {
860 hvalue(obj)->metatable = mt;
862 luaC_objbarrier(L, gcvalue(obj), mt);
863 luaC_checkfinalizer(L, gcvalue(obj), mt);
867 case LUA_TUSERDATA: {
868 uvalue(obj)->metatable = mt;
870 luaC_objbarrier(L, uvalue(obj), mt);
871 luaC_checkfinalizer(L, gcvalue(obj), mt);
876 G(L)->mt[ttnov(obj)] = mt;
886 LUA_API void lua_setuservalue (lua_State *L, int idx) {
889 api_checknelems(L, 1);
890 o = index2addr(L, idx);
891 api_check(L, ttisfulluserdata(o), "full userdata expected");
892 setuservalue(L, uvalue(o), L->top - 1);
893 luaC_barrier(L, gcvalue(o), L->top - 1);
900 ** 'load' and 'call' functions (run Lua code)
904 #define checkresults(L,na,nr) \
905 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
906 "results from function overflow current stack size")
909 LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
910 lua_KContext ctx, lua_KFunction k) {
913 api_check(L, k == NULL || !isLua(L->ci),
914 "cannot use continuations inside hooks");
915 api_checknelems(L, nargs+1);
916 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
917 checkresults(L, nargs, nresults);
918 func = L->top - (nargs+1);
919 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
920 L->ci->u.c.k = k; /* save continuation */
921 L->ci->u.c.ctx = ctx; /* save context */
922 luaD_call(L, func, nresults); /* do the call */
924 else /* no continuation or no yieldable */
925 luaD_callnoyield(L, func, nresults); /* just do the call */
926 adjustresults(L, nresults);
933 ** Execute a protected call.
935 struct CallS { /* data to 'f_call' */
941 static void f_call (lua_State *L, void *ud) {
942 struct CallS *c = cast(struct CallS *, ud);
943 luaD_callnoyield(L, c->func, c->nresults);
948 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
949 lua_KContext ctx, lua_KFunction k) {
954 api_check(L, k == NULL || !isLua(L->ci),
955 "cannot use continuations inside hooks");
956 api_checknelems(L, nargs+1);
957 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
958 checkresults(L, nargs, nresults);
962 StkId o = index2addr(L, errfunc);
963 api_checkstackindex(L, errfunc, o);
964 func = savestack(L, o);
966 c.func = L->top - (nargs+1); /* function to be called */
967 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
968 c.nresults = nresults; /* do a 'conventional' protected call */
969 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
971 else { /* prepare continuation (call is already protected by 'resume') */
972 CallInfo *ci = L->ci;
973 ci->u.c.k = k; /* save continuation */
974 ci->u.c.ctx = ctx; /* save context */
975 /* save information for error recovery */
976 ci->extra = savestack(L, c.func);
977 ci->u.c.old_errfunc = L->errfunc;
979 setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
980 ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
981 luaD_call(L, c.func, nresults); /* do the call */
982 ci->callstatus &= ~CIST_YPCALL;
983 L->errfunc = ci->u.c.old_errfunc;
984 status = LUA_OK; /* if it is here, there were no errors */
986 adjustresults(L, nresults);
992 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
993 const char *chunkname, const char *mode) {
997 if (!chunkname) chunkname = "?";
998 luaZ_init(L, &z, reader, data);
999 status = luaD_protectedparser(L, &z, chunkname, mode);
1000 if (status == LUA_OK) { /* no errors? */
1001 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
1002 if (f->nupvalues >= 1) { /* does it have an upvalue? */
1003 /* get global table from registry */
1004 Table *reg = hvalue(&G(L)->l_registry);
1005 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
1006 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1007 setobj(L, f->upvals[0]->v, gt);
1008 luaC_upvalbarrier(L, f->upvals[0]);
1016 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1020 api_checknelems(L, 1);
1023 status = luaU_dump(L, getproto(o), writer, data, strip);
1031 LUA_API int lua_status (lua_State *L) {
1037 ** Garbage-collection function
1040 LUA_API int lua_gc (lua_State *L, int what, int data) {
1050 case LUA_GCRESTART: {
1055 case LUA_GCCOLLECT: {
1060 /* GC values are expressed in Kbytes: #bytes/2^10 */
1061 res = cast_int(gettotalbytes(g) >> 10);
1064 case LUA_GCCOUNTB: {
1065 res = cast_int(gettotalbytes(g) & 0x3ff);
1069 l_mem debt = 1; /* =1 to signal that it did an actual step */
1070 lu_byte oldrunning = g->gcrunning;
1071 g->gcrunning = 1; /* allow GC to run */
1073 luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
1076 else { /* add 'data' to total debt */
1077 debt = cast(l_mem, data) * 1024 + g->GCdebt;
1078 luaE_setdebt(g, debt);
1081 g->gcrunning = oldrunning; /* restore previous state */
1082 if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
1083 res = 1; /* signal it */
1086 case LUA_GCSETPAUSE: {
1091 case LUA_GCSETSTEPMUL: {
1093 if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
1094 g->gcstepmul = data;
1097 case LUA_GCISRUNNING: {
1101 default: res = -1; /* invalid option */
1110 ** miscellaneous functions
1114 LUA_API int lua_error (lua_State *L) {
1116 api_checknelems(L, 1);
1118 /* code unreachable; will unlock when control actually leaves the kernel */
1119 return 0; /* to avoid warnings */
1123 LUA_API int lua_next (lua_State *L, int idx) {
1127 t = index2addr(L, idx);
1128 api_check(L, ttistable(t), "table expected");
1129 more = luaH_next(L, hvalue(t), L->top - 1);
1133 else /* no more elements */
1134 L->top -= 1; /* remove key */
1140 LUA_API void lua_concat (lua_State *L, int n) {
1142 api_checknelems(L, n);
1146 else if (n == 0) { /* push empty string */
1147 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1150 /* else n == 1; nothing to do */
1156 LUA_API void lua_len (lua_State *L, int idx) {
1159 t = index2addr(L, idx);
1160 luaV_objlen(L, L->top, t);
1166 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1169 if (ud) *ud = G(L)->ud;
1176 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1184 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1187 u = luaS_newudata(L, size);
1188 setuvalue(L, L->top, u);
1192 return getudatamem(u);
1197 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1198 CClosure **owner, UpVal **uv) {
1199 switch (ttype(fi)) {
1200 case LUA_TCCL: { /* C closure */
1201 CClosure *f = clCvalue(fi);
1202 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1203 *val = &f->upvalue[n-1];
1204 if (owner) *owner = f;
1207 case LUA_TLCL: { /* Lua closure */
1208 LClosure *f = clLvalue(fi);
1211 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1212 *val = f->upvals[n-1]->v;
1213 if (uv) *uv = f->upvals[n - 1];
1214 name = p->upvalues[n-1].name;
1215 return (name == NULL) ? "(*no name)" : getstr(name);
1217 default: return NULL; /* not a closure */
1222 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1224 TValue *val = NULL; /* to avoid warnings */
1226 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
1228 setobj2s(L, L->top, val);
1236 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1238 TValue *val = NULL; /* to avoid warnings */
1239 CClosure *owner = NULL;
1243 fi = index2addr(L, funcindex);
1244 api_checknelems(L, 1);
1245 name = aux_upvalue(fi, n, &val, &owner, &uv);
1248 setobj(L, val, L->top);
1249 if (owner) { luaC_barrier(L, owner, L->top); }
1250 else if (uv) { luaC_upvalbarrier(L, uv); }
1257 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1259 StkId fi = index2addr(L, fidx);
1260 api_check(L, ttisLclosure(fi), "Lua function expected");
1262 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1264 return &f->upvals[n - 1]; /* get its upvalue pointer */
1268 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1269 StkId fi = index2addr(L, fidx);
1270 switch (ttype(fi)) {
1271 case LUA_TLCL: { /* lua closure */
1272 return *getupvalref(L, fidx, n, NULL);
1274 case LUA_TCCL: { /* C closure */
1275 CClosure *f = clCvalue(fi);
1276 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1277 return &f->upvalue[n - 1];
1280 api_check(L, 0, "closure expected");
1287 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1288 int fidx2, int n2) {
1290 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1291 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1292 luaC_upvdeccount(L, *up1);
1295 if (upisopen(*up1)) (*up1)->u.open.touched = 1;
1296 luaC_upvalbarrier(L, *up1);