renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / liblua / lapi.c
blob26f552d1c31f3f2103f87a595c6754efc6b2aedd
1 /*
2 ** $Id: lapi.c,v 2.171 2013/03/16 21:10:18 roberto Exp $
3 ** Lua API
4 ** See Copyright Notice in lua.h
5 */
8 #include <stdarg.h>
9 #include <string.h>
11 #define lapi_c
12 #define LUA_CORE
14 #include "lua.h"
16 #include "lapi.h"
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lfunc.h"
20 #include "lgc.h"
21 #include "lmem.h"
22 #include "lobject.h"
23 #include "lstate.h"
24 #include "lstring.h"
25 #include "ltable.h"
26 #include "ltm.h"
27 #include "lundump.h"
28 #include "lvm.h"
32 const char lua_ident[] =
33 "$LuaVersion: " LUA_COPYRIGHT " $"
34 "$LuaAuthors: " LUA_AUTHORS " $";
37 /* value at a non-valid index */
38 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
40 /* corresponding test */
41 #define isvalid(o) ((o) != luaO_nilobject)
43 /* test for pseudo index */
44 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
46 /* test for valid but not pseudo index */
47 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
49 #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
51 #define api_checkstackindex(L, i, o) \
52 api_check(L, isstackindex(i, o), "index not in the stack")
55 static TValue *index2addr(lua_State *L, int idx) {
56 CallInfo *ci = L->ci;
57 if (idx > 0) {
58 TValue *o = ci->func + idx;
59 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
60 if (o >= L->top) return NONVALIDVALUE;
61 else return o;
62 } else if (!ispseudo(idx)) { /* negative index */
63 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
64 return L->top + idx;
65 } else if (idx == LUA_REGISTRYINDEX)
66 return &G(L)->l_registry;
67 else { /* upvalues */
68 idx = LUA_REGISTRYINDEX - idx;
69 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
70 if (ttislcf(ci->func)) /* light C function? */
71 return NONVALIDVALUE; /* it has no upvalues */
72 else {
73 CClosure *func = clCvalue(ci->func);
74 return (idx <= func->nupvalues) ? &func->upvalue[idx - 1] : NONVALIDVALUE;
81 ** to be called by 'lua_checkstack' in protected mode, to grow stack
82 ** capturing memory errors
84 static void growstack(lua_State *L, void *ud) {
85 int size = *(int *)ud;
86 luaD_growstack(L, size);
90 LUA_API int lua_checkstack(lua_State *L, int size) {
91 int res;
92 CallInfo *ci = L->ci;
93 lua_lock(L);
94 if (L->stack_last - L->top > size) /* stack large enough? */
95 res = 1; /* yes; check is OK */
96 else { /* no; need to grow stack */
97 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
98 if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
99 res = 0; /* no */
100 else /* try to grow stack */
101 res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
103 if (res && ci->top < L->top + size)
104 ci->top = L->top + size; /* adjust frame top */
105 lua_unlock(L);
106 return res;
110 LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) {
111 int i;
112 if (from == to) return;
113 lua_lock(to);
114 api_checknelems(from, n);
115 api_check(from, G(from) == G(to), "moving among independent states");
116 api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
117 from->top -= n;
118 for (i = 0; i < n; i++) {
119 setobj2s(to, to->top++, from->top + i);
121 lua_unlock(to);
125 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf) {
126 lua_CFunction old;
127 lua_lock(L);
128 old = G(L)->panic;
129 G(L)->panic = panicf;
130 lua_unlock(L);
131 return old;
135 LUA_API const lua_Number *lua_version(lua_State *L) {
136 static const lua_Number version = LUA_VERSION_NUM;
137 if (L == NULL) return &version;
138 else return G(L)->version;
144 ** basic stack manipulation
149 ** convert an acceptable stack index into an absolute index
151 LUA_API int lua_absindex(lua_State *L, int idx) {
152 return (idx > 0 || ispseudo(idx))
153 ? idx
154 : cast_int(L->top - L->ci->func + idx);
158 LUA_API int lua_gettop(lua_State *L) {
159 return cast_int(L->top - (L->ci->func + 1));
163 LUA_API void lua_settop(lua_State *L, int idx) {
164 StkId func = L->ci->func;
165 lua_lock(L);
166 if (idx >= 0) {
167 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
168 while (L->top < (func + 1) + idx)
169 setnilvalue(L->top++);
170 L->top = (func + 1) + idx;
171 } else {
172 api_check(L, -(idx + 1) <= (L->top - (func + 1)), "invalid new top");
173 L->top += idx + 1; /* `subtract' index (index is negative) */
175 lua_unlock(L);
179 LUA_API void lua_remove(lua_State *L, int idx) {
180 StkId p;
181 lua_lock(L);
182 p = index2addr(L, idx);
183 api_checkstackindex(L, idx, p);
184 while (++p < L->top) setobjs2s(L, p - 1, p);
185 L->top--;
186 lua_unlock(L);
190 LUA_API void lua_insert(lua_State *L, int idx) {
191 StkId p;
192 StkId q;
193 lua_lock(L);
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);
199 lua_unlock(L);
203 static void moveto(lua_State *L, TValue *fr, int idx) {
204 TValue *to = index2addr(L, idx);
205 api_checkvalidindex(L, to);
206 setobj(L, to, fr);
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) {
215 lua_lock(L);
216 api_checknelems(L, 1);
217 moveto(L, L->top - 1, idx);
218 L->top--;
219 lua_unlock(L);
223 LUA_API void lua_copy(lua_State *L, int fromidx, int toidx) {
224 TValue *fr;
225 lua_lock(L);
226 fr = index2addr(L, fromidx);
227 moveto(L, fr, toidx);
228 lua_unlock(L);
232 LUA_API void lua_pushvalue(lua_State *L, int idx) {
233 lua_lock(L);
234 setobj2s(L, L->top, index2addr(L, idx));
235 api_incr_top(L);
236 lua_unlock(L);
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) {
253 UNUSED(L);
254 return ttypename(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) {
265 TValue n;
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 */
293 lua_lock(L);
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);
299 L->top++;
301 o1 = L->top - 2;
302 o2 = L->top - 1;
303 if (ttisnumber(o1) && ttisnumber(o2)) {
304 setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
305 } else
306 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
307 L->top--;
308 lua_unlock(L);
312 LUA_API int lua_compare(lua_State *L, int index1, int index2, int op) {
313 StkId o1, o2;
314 int i = 0;
315 lua_lock(L); /* may call tag method */
316 o1 = index2addr(L, index1);
317 o2 = index2addr(L, index2);
318 if (isvalid(o1) && isvalid(o2)) {
319 switch (op) {
320 case LUA_OPEQ:
321 i = equalobj(L, o1, o2);
322 break;
323 case LUA_OPLT:
324 i = luaV_lessthan(L, o1, o2);
325 break;
326 case LUA_OPLE:
327 i = luaV_lessequal(L, o1, o2);
328 break;
329 default:
330 api_check(L, 0, "invalid option");
333 lua_unlock(L);
334 return i;
338 LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *isnum) {
339 TValue n;
340 const TValue *o = index2addr(L, idx);
341 if (tonumber(o, &n)) {
342 if (isnum) *isnum = 1;
343 return nvalue(o);
344 } else {
345 if (isnum) *isnum = 0;
346 return 0;
351 LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum) {
352 TValue n;
353 const TValue *o = index2addr(L, idx);
354 if (tonumber(o, &n)) {
355 lua_Integer res;
356 lua_Number num = nvalue(o);
357 lua_number2integer(res, num);
358 if (isnum) *isnum = 1;
359 return res;
360 } else {
361 if (isnum) *isnum = 0;
362 return 0;
367 LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *isnum) {
368 TValue n;
369 const TValue *o = index2addr(L, idx);
370 if (tonumber(o, &n)) {
371 lua_Unsigned res;
372 lua_Number num = nvalue(o);
373 lua_number2unsigned(res, num);
374 if (isnum) *isnum = 1;
375 return res;
376 } else {
377 if (isnum) *isnum = 0;
378 return 0;
383 LUA_API int lua_toboolean(lua_State *L, int idx) {
384 const TValue *o = index2addr(L, idx);
385 return !l_isfalse(o);
389 LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) {
390 StkId o = index2addr(L, idx);
391 if (!ttisstring(o)) {
392 lua_lock(L); /* `luaV_tostring' may create a new string */
393 if (!luaV_tostring(L, o)) { /* conversion failed? */
394 if (len != NULL) *len = 0;
395 lua_unlock(L);
396 return NULL;
398 luaC_checkGC(L);
399 o = index2addr(L, idx); /* previous call may reallocate the stack */
400 lua_unlock(L);
402 if (len != NULL) *len = tsvalue(o)->len;
403 return svalue(o);
407 LUA_API size_t lua_rawlen(lua_State *L, int idx) {
408 StkId o = index2addr(L, idx);
409 switch (ttypenv(o)) {
410 case LUA_TSTRING:
411 return tsvalue(o)->len;
412 case LUA_TUSERDATA:
413 return uvalue(o)->len;
414 case LUA_TTABLE:
415 return luaH_getn(hvalue(o));
416 default:
417 return 0;
422 LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) {
423 StkId o = index2addr(L, idx);
424 if (ttislcf(o)) return fvalue(o);
425 else if (ttisCclosure(o))
426 return clCvalue(o)->f;
427 else return NULL; /* not a C function */
431 LUA_API void *lua_touserdata(lua_State *L, int idx) {
432 StkId o = index2addr(L, idx);
433 switch (ttypenv(o)) {
434 case LUA_TUSERDATA:
435 return (rawuvalue(o) + 1);
436 case LUA_TLIGHTUSERDATA:
437 return pvalue(o);
438 default:
439 return NULL;
444 LUA_API lua_State *lua_tothread(lua_State *L, int idx) {
445 StkId o = index2addr(L, idx);
446 return (!ttisthread(o)) ? NULL : thvalue(o);
450 LUA_API const void *lua_topointer(lua_State *L, int idx) {
451 StkId o = index2addr(L, idx);
452 switch (ttype(o)) {
453 case LUA_TTABLE:
454 return hvalue(o);
455 case LUA_TLCL:
456 return clLvalue(o);
457 case LUA_TCCL:
458 return clCvalue(o);
459 case LUA_TLCF:
460 return cast(void *, cast(size_t, fvalue(o)));
461 case LUA_TTHREAD:
462 return thvalue(o);
463 case LUA_TUSERDATA:
464 case LUA_TLIGHTUSERDATA:
465 return lua_touserdata(L, idx);
466 default:
467 return NULL;
474 ** push functions (C -> stack)
478 LUA_API void lua_pushnil(lua_State *L) {
479 lua_lock(L);
480 setnilvalue(L->top);
481 api_incr_top(L);
482 lua_unlock(L);
486 LUA_API void lua_pushnumber(lua_State *L, lua_Number n) {
487 lua_lock(L);
488 setnvalue(L->top, n);
489 luai_checknum(L, L->top,
490 luaG_runerror(L, "C API - attempt to push a signaling NaN"));
491 api_incr_top(L);
492 lua_unlock(L);
496 LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) {
497 lua_lock(L);
498 setnvalue(L->top, cast_num(n));
499 api_incr_top(L);
500 lua_unlock(L);
504 LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned u) {
505 lua_Number n;
506 lua_lock(L);
507 n = lua_unsigned2number(u);
508 setnvalue(L->top, n);
509 api_incr_top(L);
510 lua_unlock(L);
514 LUA_API const char *lua_pushlstring(lua_State *L, const char *s, size_t len) {
515 TString *ts;
516 lua_lock(L);
517 luaC_checkGC(L);
518 ts = luaS_newlstr(L, s, len);
519 setsvalue2s(L, L->top, ts);
520 api_incr_top(L);
521 lua_unlock(L);
522 return getstr(ts);
526 LUA_API const char *lua_pushstring(lua_State *L, const char *s) {
527 if (s == NULL) {
528 lua_pushnil(L);
529 return NULL;
530 } else {
531 TString *ts;
532 lua_lock(L);
533 luaC_checkGC(L);
534 ts = luaS_new(L, s);
535 setsvalue2s(L, L->top, ts);
536 api_incr_top(L);
537 lua_unlock(L);
538 return getstr(ts);
543 LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
544 va_list argp) {
545 const char *ret;
546 lua_lock(L);
547 luaC_checkGC(L);
548 ret = luaO_pushvfstring(L, fmt, argp);
549 lua_unlock(L);
550 return ret;
554 LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) {
555 const char *ret;
556 va_list argp;
557 lua_lock(L);
558 luaC_checkGC(L);
559 va_start(argp, fmt);
560 ret = luaO_pushvfstring(L, fmt, argp);
561 va_end(argp);
562 lua_unlock(L);
563 return ret;
567 LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n) {
568 lua_lock(L);
569 if (n == 0) {
570 setfvalue(L->top, fn);
571 } else {
572 Closure *cl;
573 api_checknelems(L, n);
574 api_check(L, n <= MAXUPVAL, "upvalue index too large");
575 luaC_checkGC(L);
576 cl = luaF_newCclosure(L, n);
577 cl->c.f = fn;
578 L->top -= n;
579 while (n--)
580 setobj2n(L, &cl->c.upvalue[n], L->top + n);
581 setclCvalue(L, L->top, cl);
583 api_incr_top(L);
584 lua_unlock(L);
588 LUA_API void lua_pushboolean(lua_State *L, int b) {
589 lua_lock(L);
590 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
591 api_incr_top(L);
592 lua_unlock(L);
596 LUA_API void lua_pushlightuserdata(lua_State *L, void *p) {
597 lua_lock(L);
598 setpvalue(L->top, p);
599 api_incr_top(L);
600 lua_unlock(L);
604 LUA_API int lua_pushthread(lua_State *L) {
605 lua_lock(L);
606 setthvalue(L, L->top, L);
607 api_incr_top(L);
608 lua_unlock(L);
609 return (G(L)->mainthread == L);
615 ** get functions (Lua -> stack)
619 LUA_API void lua_getglobal(lua_State *L, const char *var) {
620 Table *reg = hvalue(&G(L)->l_registry);
621 const TValue *gt; /* global table */
622 lua_lock(L);
623 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
624 setsvalue2s(L, L->top++, luaS_new(L, var));
625 luaV_gettable(L, gt, L->top - 1, L->top - 1);
626 lua_unlock(L);
630 LUA_API void lua_gettable(lua_State *L, int idx) {
631 StkId t;
632 lua_lock(L);
633 t = index2addr(L, idx);
634 luaV_gettable(L, t, L->top - 1, L->top - 1);
635 lua_unlock(L);
639 LUA_API void lua_getfield(lua_State *L, int idx, const char *k) {
640 StkId t;
641 lua_lock(L);
642 t = index2addr(L, idx);
643 setsvalue2s(L, L->top, luaS_new(L, k));
644 api_incr_top(L);
645 luaV_gettable(L, t, L->top - 1, L->top - 1);
646 lua_unlock(L);
650 LUA_API void lua_rawget(lua_State *L, int idx) {
651 StkId t;
652 lua_lock(L);
653 t = index2addr(L, idx);
654 api_check(L, ttistable(t), "table expected");
655 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
656 lua_unlock(L);
660 LUA_API void lua_rawgeti(lua_State *L, int idx, int n) {
661 StkId t;
662 lua_lock(L);
663 t = index2addr(L, idx);
664 api_check(L, ttistable(t), "table expected");
665 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
666 api_incr_top(L);
667 lua_unlock(L);
671 LUA_API void lua_rawgetp(lua_State *L, int idx, const void *p) {
672 StkId t;
673 TValue k;
674 lua_lock(L);
675 t = index2addr(L, idx);
676 api_check(L, ttistable(t), "table expected");
677 setpvalue(&k, cast(void *, p));
678 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
679 api_incr_top(L);
680 lua_unlock(L);
684 LUA_API void lua_createtable(lua_State *L, int narray, int nrec) {
685 Table *t;
686 lua_lock(L);
687 luaC_checkGC(L);
688 t = luaH_new(L);
689 sethvalue(L, L->top, t);
690 api_incr_top(L);
691 if (narray > 0 || nrec > 0)
692 luaH_resize(L, t, narray, nrec);
693 lua_unlock(L);
697 LUA_API int lua_getmetatable(lua_State *L, int objindex) {
698 const TValue *obj;
699 Table *mt = NULL;
700 int res;
701 lua_lock(L);
702 obj = index2addr(L, objindex);
703 switch (ttypenv(obj)) {
704 case LUA_TTABLE:
705 mt = hvalue(obj)->metatable;
706 break;
707 case LUA_TUSERDATA:
708 mt = uvalue(obj)->metatable;
709 break;
710 default:
711 mt = G(L)->mt[ttypenv(obj)];
712 break;
714 if (mt == NULL)
715 res = 0;
716 else {
717 sethvalue(L, L->top, mt);
718 api_incr_top(L);
719 res = 1;
721 lua_unlock(L);
722 return res;
726 LUA_API void lua_getuservalue(lua_State *L, int idx) {
727 StkId o;
728 lua_lock(L);
729 o = index2addr(L, idx);
730 api_check(L, ttisuserdata(o), "userdata expected");
731 if (uvalue(o)->env) {
732 sethvalue(L, L->top, uvalue(o)->env);
733 } else
734 setnilvalue(L->top);
735 api_incr_top(L);
736 lua_unlock(L);
741 ** set functions (stack -> Lua)
745 LUA_API void lua_setglobal(lua_State *L, const char *var) {
746 Table *reg = hvalue(&G(L)->l_registry);
747 const TValue *gt; /* global table */
748 lua_lock(L);
749 api_checknelems(L, 1);
750 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
751 setsvalue2s(L, L->top++, luaS_new(L, var));
752 luaV_settable(L, gt, L->top - 1, L->top - 2);
753 L->top -= 2; /* pop value and key */
754 lua_unlock(L);
758 LUA_API void lua_settable(lua_State *L, int idx) {
759 StkId t;
760 lua_lock(L);
761 api_checknelems(L, 2);
762 t = index2addr(L, idx);
763 luaV_settable(L, t, L->top - 2, L->top - 1);
764 L->top -= 2; /* pop index and value */
765 lua_unlock(L);
769 LUA_API void lua_setfield(lua_State *L, int idx, const char *k) {
770 StkId t;
771 lua_lock(L);
772 api_checknelems(L, 1);
773 t = index2addr(L, idx);
774 setsvalue2s(L, L->top++, luaS_new(L, k));
775 luaV_settable(L, t, L->top - 1, L->top - 2);
776 L->top -= 2; /* pop value and key */
777 lua_unlock(L);
781 LUA_API void lua_rawset(lua_State *L, int idx) {
782 StkId t;
783 lua_lock(L);
784 api_checknelems(L, 2);
785 t = index2addr(L, idx);
786 api_check(L, ttistable(t), "table expected");
787 setobj2t(L, luaH_set(L, hvalue(t), L->top - 2), L->top - 1);
788 invalidateTMcache(hvalue(t));
789 luaC_barrierback(L, gcvalue(t), L->top - 1);
790 L->top -= 2;
791 lua_unlock(L);
795 LUA_API void lua_rawseti(lua_State *L, int idx, int n) {
796 StkId t;
797 lua_lock(L);
798 api_checknelems(L, 1);
799 t = index2addr(L, idx);
800 api_check(L, ttistable(t), "table expected");
801 luaH_setint(L, hvalue(t), n, L->top - 1);
802 luaC_barrierback(L, gcvalue(t), L->top - 1);
803 L->top--;
804 lua_unlock(L);
808 LUA_API void lua_rawsetp(lua_State *L, int idx, const void *p) {
809 StkId t;
810 TValue k;
811 lua_lock(L);
812 api_checknelems(L, 1);
813 t = index2addr(L, idx);
814 api_check(L, ttistable(t), "table expected");
815 setpvalue(&k, cast(void *, p));
816 setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
817 luaC_barrierback(L, gcvalue(t), L->top - 1);
818 L->top--;
819 lua_unlock(L);
823 LUA_API int lua_setmetatable(lua_State *L, int objindex) {
824 TValue *obj;
825 Table *mt;
826 lua_lock(L);
827 api_checknelems(L, 1);
828 obj = index2addr(L, objindex);
829 if (ttisnil(L->top - 1))
830 mt = NULL;
831 else {
832 api_check(L, ttistable(L->top - 1), "table expected");
833 mt = hvalue(L->top - 1);
835 switch (ttypenv(obj)) {
836 case LUA_TTABLE: {
837 hvalue(obj)->metatable = mt;
838 if (mt) {
839 luaC_objbarrierback(L, gcvalue(obj), mt);
840 luaC_checkfinalizer(L, gcvalue(obj), mt);
842 break;
844 case LUA_TUSERDATA: {
845 uvalue(obj)->metatable = mt;
846 if (mt) {
847 luaC_objbarrier(L, rawuvalue(obj), mt);
848 luaC_checkfinalizer(L, gcvalue(obj), mt);
850 break;
852 default: {
853 G(L)->mt[ttypenv(obj)] = mt;
854 break;
857 L->top--;
858 lua_unlock(L);
859 return 1;
863 LUA_API void lua_setuservalue(lua_State *L, int idx) {
864 StkId o;
865 lua_lock(L);
866 api_checknelems(L, 1);
867 o = index2addr(L, idx);
868 api_check(L, ttisuserdata(o), "userdata expected");
869 if (ttisnil(L->top - 1))
870 uvalue(o)->env = NULL;
871 else {
872 api_check(L, ttistable(L->top - 1), "table expected");
873 uvalue(o)->env = hvalue(L->top - 1);
874 luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
876 L->top--;
877 lua_unlock(L);
882 ** `load' and `call' functions (run Lua code)
886 #define checkresults(L,na,nr) \
887 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
888 "results from function overflow current stack size")
891 LUA_API int lua_getctx(lua_State *L, int *ctx) {
892 if (L->ci->callstatus & CIST_YIELDED) {
893 if (ctx) *ctx = L->ci->u.c.ctx;
894 return L->ci->u.c.status;
895 } else return LUA_OK;
899 LUA_API void lua_callk(lua_State *L, int nargs, int nresults, int ctx,
900 lua_CFunction k) {
901 StkId func;
902 lua_lock(L);
903 api_check(L, k == NULL || !isLua(L->ci),
904 "cannot use continuations inside hooks");
905 api_checknelems(L, nargs + 1);
906 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
907 checkresults(L, nargs, nresults);
908 func = L->top - (nargs + 1);
909 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
910 L->ci->u.c.k = k; /* save continuation */
911 L->ci->u.c.ctx = ctx; /* save context */
912 luaD_call(L, func, nresults, 1); /* do the call */
913 } else /* no continuation or no yieldable */
914 luaD_call(L, func, nresults, 0); /* just do the call */
915 adjustresults(L, nresults);
916 lua_unlock(L);
922 ** Execute a protected call.
924 struct CallS { /* data to `f_call' */
925 StkId func;
926 int nresults;
930 static void f_call(lua_State *L, void *ud) {
931 struct CallS *c = cast(struct CallS *, ud);
932 luaD_call(L, c->func, c->nresults, 0);
937 LUA_API int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc,
938 int ctx, lua_CFunction k) {
939 struct CallS c;
940 int status;
941 ptrdiff_t func;
942 lua_lock(L);
943 api_check(L, k == NULL || !isLua(L->ci),
944 "cannot use continuations inside hooks");
945 api_checknelems(L, nargs + 1);
946 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
947 checkresults(L, nargs, nresults);
948 if (errfunc == 0)
949 func = 0;
950 else {
951 StkId o = index2addr(L, errfunc);
952 api_checkstackindex(L, errfunc, o);
953 func = savestack(L, o);
955 c.func = L->top - (nargs + 1); /* function to be called */
956 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
957 c.nresults = nresults; /* do a 'conventional' protected call */
958 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
959 } else { /* prepare continuation (call is already protected by 'resume') */
960 CallInfo *ci = L->ci;
961 ci->u.c.k = k; /* save continuation */
962 ci->u.c.ctx = ctx; /* save context */
963 /* save information for error recovery */
964 ci->extra = savestack(L, c.func);
965 ci->u.c.old_allowhook = L->allowhook;
966 ci->u.c.old_errfunc = L->errfunc;
967 L->errfunc = func;
968 /* mark that function may do error recovery */
969 ci->callstatus |= CIST_YPCALL;
970 luaD_call(L, c.func, nresults, 1); /* do the call */
971 ci->callstatus &= ~CIST_YPCALL;
972 L->errfunc = ci->u.c.old_errfunc;
973 status = LUA_OK; /* if it is here, there were no errors */
975 adjustresults(L, nresults);
976 lua_unlock(L);
977 return status;
981 LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
982 const char *chunkname, const char *mode) {
983 ZIO z;
984 int status;
985 lua_lock(L);
986 if (!chunkname) chunkname = "?";
987 luaZ_init(L, &z, reader, data);
988 status = luaD_protectedparser(L, &z, chunkname, mode);
989 if (status == LUA_OK) { /* no errors? */
990 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
991 if (f->nupvalues == 1) { /* does it have one upvalue? */
992 /* get global table from registry */
993 Table *reg = hvalue(&G(L)->l_registry);
994 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
995 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
996 setobj(L, f->upvals[0]->v, gt);
997 luaC_barrier(L, f->upvals[0], gt);
1000 lua_unlock(L);
1001 return status;
1005 LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) {
1006 int status;
1007 TValue *o;
1008 lua_lock(L);
1009 api_checknelems(L, 1);
1010 o = L->top - 1;
1011 if (isLfunction(o))
1012 status = luaU_dump(L, getproto(o), writer, data, 0);
1013 else
1014 status = 1;
1015 lua_unlock(L);
1016 return status;
1020 LUA_API int lua_status(lua_State *L) {
1021 return L->status;
1026 ** Garbage-collection function
1029 LUA_API int lua_gc(lua_State *L, int what, int data) {
1030 int res = 0;
1031 global_State *g;
1032 lua_lock(L);
1033 g = G(L);
1034 switch (what) {
1035 case LUA_GCSTOP: {
1036 g->gcrunning = 0;
1037 break;
1039 case LUA_GCRESTART: {
1040 luaE_setdebt(g, 0);
1041 g->gcrunning = 1;
1042 break;
1044 case LUA_GCCOLLECT: {
1045 luaC_fullgc(L, 0);
1046 break;
1048 case LUA_GCCOUNT: {
1049 /* GC values are expressed in Kbytes: #bytes/2^10 */
1050 res = cast_int(gettotalbytes(g) >> 10);
1051 break;
1053 case LUA_GCCOUNTB: {
1054 res = cast_int(gettotalbytes(g) & 0x3ff);
1055 break;
1057 case LUA_GCSTEP: {
1058 if (g->gckind == KGC_GEN) { /* generational mode? */
1059 res = (g->GCestimate == 0); /* true if it will do major collection */
1060 luaC_forcestep(L); /* do a single step */
1061 } else {
1062 lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
1063 if (g->gcrunning)
1064 debt += g->GCdebt; /* include current debt */
1065 luaE_setdebt(g, debt);
1066 luaC_forcestep(L);
1067 if (g->gcstate == GCSpause) /* end of cycle? */
1068 res = 1; /* signal it */
1070 break;
1072 case LUA_GCSETPAUSE: {
1073 res = g->gcpause;
1074 g->gcpause = data;
1075 break;
1077 case LUA_GCSETMAJORINC: {
1078 res = g->gcmajorinc;
1079 g->gcmajorinc = data;
1080 break;
1082 case LUA_GCSETSTEPMUL: {
1083 res = g->gcstepmul;
1084 g->gcstepmul = data;
1085 break;
1087 case LUA_GCISRUNNING: {
1088 res = g->gcrunning;
1089 break;
1091 case LUA_GCGEN: { /* change collector to generational mode */
1092 luaC_changemode(L, KGC_GEN);
1093 break;
1095 case LUA_GCINC: { /* change collector to incremental mode */
1096 luaC_changemode(L, KGC_NORMAL);
1097 break;
1099 default:
1100 res = -1; /* invalid option */
1102 lua_unlock(L);
1103 return res;
1109 ** miscellaneous functions
1113 LUA_API int lua_error(lua_State *L) {
1114 lua_lock(L);
1115 api_checknelems(L, 1);
1116 luaG_errormsg(L);
1117 /* code unreachable; will unlock when control actually leaves the kernel */
1118 return 0; /* to avoid warnings */
1122 LUA_API int lua_next(lua_State *L, int idx) {
1123 StkId t;
1124 int more;
1125 lua_lock(L);
1126 t = index2addr(L, idx);
1127 api_check(L, ttistable(t), "table expected");
1128 more = luaH_next(L, hvalue(t), L->top - 1);
1129 if (more) {
1130 api_incr_top(L);
1131 } else /* no more elements */
1132 L->top -= 1; /* remove key */
1133 lua_unlock(L);
1134 return more;
1138 LUA_API void lua_concat(lua_State *L, int n) {
1139 lua_lock(L);
1140 api_checknelems(L, n);
1141 if (n >= 2) {
1142 luaC_checkGC(L);
1143 luaV_concat(L, n);
1144 } else if (n == 0) { /* push empty string */
1145 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1146 api_incr_top(L);
1148 /* else n == 1; nothing to do */
1149 lua_unlock(L);
1153 LUA_API void lua_len(lua_State *L, int idx) {
1154 StkId t;
1155 lua_lock(L);
1156 t = index2addr(L, idx);
1157 luaV_objlen(L, L->top, t);
1158 api_incr_top(L);
1159 lua_unlock(L);
1163 LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) {
1164 lua_Alloc f;
1165 lua_lock(L);
1166 if (ud) *ud = G(L)->ud;
1167 f = G(L)->frealloc;
1168 lua_unlock(L);
1169 return f;
1173 LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) {
1174 lua_lock(L);
1175 G(L)->ud = ud;
1176 G(L)->frealloc = f;
1177 lua_unlock(L);
1181 LUA_API void *lua_newuserdata(lua_State *L, size_t size) {
1182 Udata *u;
1183 lua_lock(L);
1184 luaC_checkGC(L);
1185 u = luaS_newudata(L, size, NULL);
1186 setuvalue(L, L->top, u);
1187 api_incr_top(L);
1188 lua_unlock(L);
1189 return u + 1;
1194 static const char *aux_upvalue(StkId fi, int n, TValue **val,
1195 GCObject **owner) {
1196 switch (ttype(fi)) {
1197 case LUA_TCCL: { /* C closure */
1198 CClosure *f = clCvalue(fi);
1199 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1200 *val = &f->upvalue[n - 1];
1201 if (owner) *owner = obj2gco(f);
1202 return "";
1204 case LUA_TLCL: { /* Lua closure */
1205 LClosure *f = clLvalue(fi);
1206 TString *name;
1207 Proto *p = f->p;
1208 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1209 *val = f->upvals[n - 1]->v;
1210 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1211 name = p->upvalues[n - 1].name;
1212 return (name == NULL) ? "" : getstr(name);
1214 default:
1215 return NULL; /* not a closure */
1220 LUA_API const char *lua_getupvalue(lua_State *L, int funcindex, int n) {
1221 const char *name;
1222 TValue *val = NULL; /* to avoid warnings */
1223 lua_lock(L);
1224 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1225 if (name) {
1226 setobj2s(L, L->top, val);
1227 api_incr_top(L);
1229 lua_unlock(L);
1230 return name;
1234 LUA_API const char *lua_setupvalue(lua_State *L, int funcindex, int n) {
1235 const char *name;
1236 TValue *val = NULL; /* to avoid warnings */
1237 GCObject *owner = NULL; /* to avoid warnings */
1238 StkId fi;
1239 lua_lock(L);
1240 fi = index2addr(L, funcindex);
1241 api_checknelems(L, 1);
1242 name = aux_upvalue(fi, n, &val, &owner);
1243 if (name) {
1244 L->top--;
1245 setobj(L, val, L->top);
1246 luaC_barrier(L, owner, L->top);
1248 lua_unlock(L);
1249 return name;
1253 static UpVal **getupvalref(lua_State *L, int fidx, int n, LClosure **pf) {
1254 LClosure *f;
1255 StkId fi = index2addr(L, fidx);
1256 api_check(L, ttisLclosure(fi), "Lua function expected");
1257 f = clLvalue(fi);
1258 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1259 if (pf) *pf = f;
1260 return &f->upvals[n - 1]; /* get its upvalue pointer */
1264 LUA_API void *lua_upvalueid(lua_State *L, int fidx, int n) {
1265 StkId fi = index2addr(L, fidx);
1266 switch (ttype(fi)) {
1267 case LUA_TLCL: { /* lua closure */
1268 return *getupvalref(L, fidx, n, NULL);
1270 case LUA_TCCL: { /* C closure */
1271 CClosure *f = clCvalue(fi);
1272 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1273 return &f->upvalue[n - 1];
1275 default: {
1276 api_check(L, 0, "closure expected");
1277 return NULL;
1283 LUA_API void lua_upvaluejoin(lua_State *L, int fidx1, int n1,
1284 int fidx2, int n2) {
1285 LClosure *f1;
1286 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1287 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1288 *up1 = *up2;
1289 luaC_objbarrier(L, f1, *up2);