35const char lua_ident[] =
36 "$LuaVersion: " LUA_COPYRIGHT
" $"
37 "$LuaAuthors: " LUA_AUTHORS
" $";
46#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
50#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
53#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
63 StkId o = ci->func.p + idx;
64 api_check(L, idx <= ci->top.p - (ci->func.p + 1),
"unacceptable index");
65 if (o >= L->top.p)
return &G(L)->nilvalue;
68 else if (!ispseudo(idx)) {
69 api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
71 return s2v(L->top.p + idx);
73 else if (idx == LUA_REGISTRYINDEX)
74 return &G(L)->l_registry;
76 idx = LUA_REGISTRYINDEX - idx;
77 api_check(L, idx <= MAXUPVAL + 1,
"upvalue index too large");
78 if (ttisCclosure(s2v(ci->func.p))) {
79 CClosure *func = clCvalue(s2v(ci->func.p));
80 return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
84 api_check(L, ttislcf(s2v(ci->func.p)),
"caller not a C function");
85 return &G(L)->nilvalue;
95l_sinline StkId index2stack (
lua_State *L,
int idx) {
98 StkId o = ci->func.p + idx;
99 api_check(L, o < L->top.p,
"invalid index");
103 api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
105 api_check(L, !ispseudo(idx),
"invalid index");
106 return L->top.p + idx;
111LUA_API
int lua_checkstack (
lua_State *L,
int n) {
116 api_check(L, n >= 0,
"negative 'n'");
117 if (L->stack_last.p - L->top.p > n)
120 res = luaD_growstack(L, n, 0);
121 if (res && ci->top.p < L->top.p + n)
122 ci->top.p = L->top.p + n;
130 if (from == to)
return;
132 api_checknelems(from, n);
133 api_check(from, G(from) == G(to),
"moving among independent states");
134 api_check(from, to->ci->top.p - to->top.p >= n,
"stack overflow");
136 for (i = 0; i < n; i++) {
137 setobjs2s(to, to->top.p, from->top.p + i);
144LUA_API lua_CFunction lua_atpanic (
lua_State *L, lua_CFunction panicf) {
148 G(L)->panic = panicf;
154LUA_API lua_Number lua_version (
lua_State *L) {
156 return LUA_VERSION_NUM;
169LUA_API
int lua_absindex (
lua_State *L,
int idx) {
170 return (idx > 0 || ispseudo(idx))
172 : cast_int(L->top.p - L->ci->func.p) + idx;
177 return cast_int(L->top.p - (L->ci->func.p + 1));
181LUA_API
void lua_settop (
lua_State *L,
int idx) {
189 api_check(L, idx <= ci->top.p - (func + 1),
"new top too large");
190 diff = ((func + 1) + idx) - L->top.p;
191 for (; diff > 0; diff--)
192 setnilvalue(s2v(L->top.p++));
195 api_check(L, -(idx+1) <= (L->top.p - (func + 1)),
"invalid new top");
198 api_check(L, L->tbclist.p < L->top.p,
"previous pop of an unclosed slot");
199 newtop = L->top.p + diff;
200 if (diff < 0 && L->tbclist.p >= newtop) {
201 lua_assert(hastocloseCfunc(ci->nresults));
202 newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
209LUA_API
void lua_closeslot (
lua_State *L,
int idx) {
212 level = index2stack(L, idx);
213 api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level,
214 "no variable to close at given level");
215 level = luaF_close(L, level, CLOSEKTOP, 0);
216 setnilvalue(s2v(level));
227l_sinline
void reverse (
lua_State *L, StkId from, StkId to) {
228 for (; from < to; from++, to--) {
230 setobj(L, &temp, s2v(from));
231 setobjs2s(L, from, to);
232 setobj2s(L, to, &temp);
241LUA_API
void lua_rotate (
lua_State *L,
int idx,
int n) {
245 p = index2stack(L, idx);
246 api_check(L, (n >= 0 ? n : -n) <= (t - p + 1),
"invalid 'n'");
247 m = (n >= 0 ? t - n : p - n - 1);
249 reverse(L, m + 1, t);
255LUA_API
void lua_copy (
lua_State *L,
int fromidx,
int toidx) {
258 fr = index2value(L, fromidx);
259 to = index2value(L, toidx);
260 api_check(L, isvalid(L, to),
"invalid index");
262 if (isupvalue(toidx))
263 luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
270LUA_API
void lua_pushvalue (
lua_State *L,
int idx) {
272 setobj2s(L, L->top.p, index2value(L, idx));
284LUA_API
int lua_type (
lua_State *L,
int idx) {
285 const TValue *o = index2value(L, idx);
286 return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
290LUA_API
const char *lua_typename (
lua_State *L,
int t) {
292 api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES,
"invalid type");
297LUA_API
int lua_iscfunction (
lua_State *L,
int idx) {
298 const TValue *o = index2value(L, idx);
299 return (ttislcf(o) || (ttisCclosure(o)));
303LUA_API
int lua_isinteger (
lua_State *L,
int idx) {
304 const TValue *o = index2value(L, idx);
305 return ttisinteger(o);
309LUA_API
int lua_isnumber (
lua_State *L,
int idx) {
311 const TValue *o = index2value(L, idx);
312 return tonumber(o, &n);
316LUA_API
int lua_isstring (
lua_State *L,
int idx) {
317 const TValue *o = index2value(L, idx);
318 return (ttisstring(o) || cvt2str(o));
322LUA_API
int lua_isuserdata (
lua_State *L,
int idx) {
323 const TValue *o = index2value(L, idx);
324 return (ttisfulluserdata(o) || ttislightuserdata(o));
328LUA_API
int lua_rawequal (
lua_State *L,
int index1,
int index2) {
329 const TValue *o1 = index2value(L, index1);
330 const TValue *o2 = index2value(L, index2);
331 return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
335LUA_API
void lua_arith (
lua_State *L,
int op) {
337 if (op != LUA_OPUNM && op != LUA_OPBNOT)
338 api_checknelems(L, 2);
340 api_checknelems(L, 1);
341 setobjs2s(L, L->top.p, L->top.p - 1);
345 luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
351LUA_API
int lua_compare (
lua_State *L,
int index1,
int index2,
int op) {
356 o1 = index2value(L, index1);
357 o2 = index2value(L, index2);
358 if (isvalid(L, o1) && isvalid(L, o2)) {
360 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2);
break;
361 case LUA_OPLT: i = luaV_lessthan(L, o1, o2);
break;
362 case LUA_OPLE: i = luaV_lessequal(L, o1, o2);
break;
363 default: api_check(L, 0,
"invalid option");
371LUA_API
size_t lua_stringtonumber (
lua_State *L,
const char *s) {
372 size_t sz = luaO_str2num(s, s2v(L->top.p));
379LUA_API lua_Number lua_tonumberx (
lua_State *L,
int idx,
int *pisnum) {
381 const TValue *o = index2value(L, idx);
382 int isnum = tonumber(o, &n);
389LUA_API lua_Integer lua_tointegerx (
lua_State *L,
int idx,
int *pisnum) {
391 const TValue *o = index2value(L, idx);
392 int isnum = tointeger(o, &res);
399LUA_API
int lua_toboolean (
lua_State *L,
int idx) {
400 const TValue *o = index2value(L, idx);
401 return !l_isfalse(o);
405LUA_API
const char *lua_tolstring (
lua_State *L,
int idx,
size_t *len) {
408 o = index2value(L, idx);
409 if (!ttisstring(o)) {
411 if (len != NULL) *len = 0;
417 o = index2value(L, idx);
426LUA_API lua_Unsigned lua_rawlen (
lua_State *L,
int idx) {
427 const TValue *o = index2value(L, idx);
428 switch (ttypetag(o)) {
429 case LUA_VSHRSTR:
return tsvalue(o)->shrlen;
430 case LUA_VLNGSTR:
return tsvalue(o)->u.lnglen;
431 case LUA_VUSERDATA:
return uvalue(o)->len;
432 case LUA_VTABLE:
return luaH_getn(hvalue(o));
438LUA_API lua_CFunction lua_tocfunction (
lua_State *L,
int idx) {
439 const TValue *o = index2value(L, idx);
440 if (ttislcf(o))
return fvalue(o);
441 else if (ttisCclosure(o))
442 return clCvalue(o)->f;
447l_sinline
void *touserdata (
const TValue *o) {
449 case LUA_TUSERDATA:
return getudatamem(uvalue(o));
450 case LUA_TLIGHTUSERDATA:
return pvalue(o);
451 default:
return NULL;
456LUA_API
void *lua_touserdata (
lua_State *L,
int idx) {
457 const TValue *o = index2value(L, idx);
458 return touserdata(o);
463 const TValue *o = index2value(L, idx);
464 return (!ttisthread(o)) ? NULL : thvalue(o);
475LUA_API
const void *lua_topointer (
lua_State *L,
int idx) {
476 const TValue *o = index2value(L, idx);
477 switch (ttypetag(o)) {
478 case LUA_VLCF:
return cast_voidp(cast_sizet(fvalue(o)));
479 case LUA_VUSERDATA:
case LUA_VLIGHTUSERDATA:
480 return touserdata(o);
482 if (iscollectable(o))
499 setnilvalue(s2v(L->top.p));
505LUA_API
void lua_pushnumber (
lua_State *L, lua_Number n) {
507 setfltvalue(s2v(L->top.p), n);
513LUA_API
void lua_pushinteger (
lua_State *L, lua_Integer n) {
515 setivalue(s2v(L->top.p), n);
526LUA_API
const char *lua_pushlstring (
lua_State *L,
const char *s,
size_t len) {
529 ts = (len == 0) ? luaS_new(L,
"") : luaS_newlstr(L, s, len);
530 setsvalue2s(L, L->top.p, ts);
538LUA_API
const char *lua_pushstring (
lua_State *L,
const char *s) {
541 setnilvalue(s2v(L->top.p));
545 setsvalue2s(L, L->top.p, ts);
555LUA_API
const char *lua_pushvfstring (
lua_State *L,
const char *fmt,
559 ret = luaO_pushvfstring(L, fmt, argp);
566LUA_API
const char *lua_pushfstring (
lua_State *L,
const char *fmt, ...) {
571 ret = luaO_pushvfstring(L, fmt, argp);
579LUA_API
void lua_pushcclosure (
lua_State *L, lua_CFunction fn,
int n) {
582 setfvalue(s2v(L->top.p), fn);
587 api_checknelems(L, n);
588 api_check(L, n <= MAXUPVAL,
"upvalue index too large");
589 cl = luaF_newCclosure(L, n);
593 setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
595 lua_assert(iswhite(cl));
597 setclCvalue(L, s2v(L->top.p), cl);
605LUA_API
void lua_pushboolean (
lua_State *L,
int b) {
608 setbtvalue(s2v(L->top.p));
610 setbfvalue(s2v(L->top.p));
616LUA_API
void lua_pushlightuserdata (
lua_State *L,
void *p) {
618 setpvalue(s2v(L->top.p), p);
624LUA_API
int lua_pushthread (
lua_State *L) {
626 setthvalue(L, s2v(L->top.p), L);
629 return (G(L)->mainthread == L);
639l_sinline
int auxgetstr (
lua_State *L,
const TValue *t,
const char *k) {
642 if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
643 setobj2s(L, L->top.p, slot);
647 setsvalue2s(L, L->top.p, str);
649 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
652 return ttype(s2v(L->top.p - 1));
662#define getGtable(L) \
663 (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
666LUA_API
int lua_getglobal (
lua_State *L,
const char *name) {
670 return auxgetstr(L, G, name);
674LUA_API
int lua_gettable (
lua_State *L,
int idx) {
678 t = index2value(L, idx);
679 if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
680 setobj2s(L, L->top.p - 1, slot);
683 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
685 return ttype(s2v(L->top.p - 1));
689LUA_API
int lua_getfield (
lua_State *L,
int idx,
const char *k) {
691 return auxgetstr(L, index2value(L, idx), k);
695LUA_API
int lua_geti (
lua_State *L,
int idx, lua_Integer n) {
699 t = index2value(L, idx);
700 if (luaV_fastgeti(L, t, n, slot)) {
701 setobj2s(L, L->top.p, slot);
706 luaV_finishget(L, t, &aux, L->top.p, slot);
710 return ttype(s2v(L->top.p - 1));
716 setnilvalue(s2v(L->top.p));
718 setobj2s(L, L->top.p, val);
721 return ttype(s2v(L->top.p - 1));
726 TValue *t = index2value(L, idx);
727 api_check(L, ttistable(t),
"table expected");
732LUA_API
int lua_rawget (
lua_State *L,
int idx) {
736 api_checknelems(L, 1);
737 t = gettable(L, idx);
738 val = luaH_get(t, s2v(L->top.p - 1));
740 return finishrawget(L, val);
744LUA_API
int lua_rawgeti (
lua_State *L,
int idx, lua_Integer n) {
747 t = gettable(L, idx);
748 return finishrawget(L, luaH_getint(t, n));
752LUA_API
int lua_rawgetp (
lua_State *L,
int idx,
const void *p) {
756 t = gettable(L, idx);
757 setpvalue(&k, cast_voidp(p));
758 return finishrawget(L, luaH_get(t, &k));
762LUA_API
void lua_createtable (
lua_State *L,
int narray,
int nrec) {
766 sethvalue2s(L, L->top.p, t);
768 if (narray > 0 || nrec > 0)
769 luaH_resize(L, t, narray, nrec);
775LUA_API
int lua_getmetatable (
lua_State *L,
int objindex) {
780 obj = index2value(L, objindex);
781 switch (ttype(obj)) {
783 mt = hvalue(obj)->metatable;
786 mt = uvalue(obj)->metatable;
789 mt = G(L)->mt[ttype(obj)];
793 sethvalue2s(L, L->top.p, mt);
802LUA_API
int lua_getiuservalue (
lua_State *L,
int idx,
int n) {
806 o = index2value(L, idx);
807 api_check(L, ttisfulluserdata(o),
"full userdata expected");
808 if (n <= 0 || n > uvalue(o)->nuvalue) {
809 setnilvalue(s2v(L->top.p));
813 setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
814 t = ttype(s2v(L->top.p));
832 api_checknelems(L, 1);
833 if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
834 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
838 setsvalue2s(L, L->top.p, str);
840 luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
847LUA_API
void lua_setglobal (
lua_State *L,
const char *name) {
851 auxsetstr(L, G, name);
855LUA_API
void lua_settable (
lua_State *L,
int idx) {
859 api_checknelems(L, 2);
860 t = index2value(L, idx);
861 if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
862 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
865 luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
871LUA_API
void lua_setfield (
lua_State *L,
int idx,
const char *k) {
873 auxsetstr(L, index2value(L, idx), k);
877LUA_API
void lua_seti (
lua_State *L,
int idx, lua_Integer n) {
881 api_checknelems(L, 1);
882 t = index2value(L, idx);
883 if (luaV_fastgeti(L, t, n, slot)) {
884 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
889 luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
899 api_checknelems(L, n);
900 t = gettable(L, idx);
901 luaH_set(L, t, key, s2v(L->top.p - 1));
902 invalidateTMcache(t);
903 luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
909LUA_API
void lua_rawset (
lua_State *L,
int idx) {
910 aux_rawset(L, idx, s2v(L->top.p - 2), 2);
914LUA_API
void lua_rawsetp (
lua_State *L,
int idx,
const void *p) {
916 setpvalue(&k, cast_voidp(p));
917 aux_rawset(L, idx, &k, 1);
921LUA_API
void lua_rawseti (
lua_State *L,
int idx, lua_Integer n) {
924 api_checknelems(L, 1);
925 t = gettable(L, idx);
926 luaH_setint(L, t, n, s2v(L->top.p - 1));
927 luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
933LUA_API
int lua_setmetatable (
lua_State *L,
int objindex) {
937 api_checknelems(L, 1);
938 obj = index2value(L, objindex);
939 if (ttisnil(s2v(L->top.p - 1)))
942 api_check(L, ttistable(s2v(L->top.p - 1)),
"table expected");
943 mt = hvalue(s2v(L->top.p - 1));
945 switch (ttype(obj)) {
947 hvalue(obj)->metatable = mt;
949 luaC_objbarrier(L, gcvalue(obj), mt);
950 luaC_checkfinalizer(L, gcvalue(obj), mt);
954 case LUA_TUSERDATA: {
955 uvalue(obj)->metatable = mt;
957 luaC_objbarrier(L, uvalue(obj), mt);
958 luaC_checkfinalizer(L, gcvalue(obj), mt);
963 G(L)->mt[ttype(obj)] = mt;
973LUA_API
int lua_setiuservalue (
lua_State *L,
int idx,
int n) {
977 api_checknelems(L, 1);
978 o = index2value(L, idx);
979 api_check(L, ttisfulluserdata(o),
"full userdata expected");
980 if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
983 setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
984 luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
998#define checkresults(L,na,nr) \
999 api_check(L, (nr) == LUA_MULTRET \
1000 || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1001 "results from function overflow current stack size")
1004LUA_API
void lua_callk (
lua_State *L,
int nargs,
int nresults,
1005 lua_KContext ctx, lua_KFunction k) {
1008 api_check(L, k == NULL || !isLua(L->ci),
1009 "cannot use continuations inside hooks");
1010 api_checknelems(L, nargs+1);
1011 api_check(L, L->status == LUA_OK,
"cannot do calls on non-normal thread");
1012 checkresults(L, nargs, nresults);
1013 func = L->top.p - (nargs+1);
1014 if (k != NULL && yieldable(L)) {
1016 L->ci->u.c.ctx = ctx;
1017 luaD_call(L, func, nresults);
1020 luaD_callnoyield(L, func, nresults);
1021 adjustresults(L, nresults);
1036static void f_call (
lua_State *L,
void *ud) {
1038 luaD_callnoyield(L, c->func, c->nresults);
1043LUA_API
int lua_pcallk (
lua_State *L,
int nargs,
int nresults,
int errfunc,
1044 lua_KContext ctx, lua_KFunction k) {
1049 api_check(L, k == NULL || !isLua(L->ci),
1050 "cannot use continuations inside hooks");
1051 api_checknelems(L, nargs+1);
1052 api_check(L, L->status == LUA_OK,
"cannot do calls on non-normal thread");
1053 checkresults(L, nargs, nresults);
1057 StkId o = index2stack(L, errfunc);
1058 api_check(L, ttisfunction(s2v(o)),
"error handler must be a function");
1059 func = savestack(L, o);
1061 c.func = L->top.p - (nargs+1);
1062 if (k == NULL || !yieldable(L)) {
1063 c.nresults = nresults;
1064 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
1071 ci->u2.funcidx = cast_int(savestack(L, c.func));
1072 ci->u.c.old_errfunc = L->errfunc;
1074 setoah(ci->callstatus, L->allowhook);
1075 ci->callstatus |= CIST_YPCALL;
1076 luaD_call(L, c.func, nresults);
1077 ci->callstatus &= ~CIST_YPCALL;
1078 L->errfunc = ci->u.c.old_errfunc;
1081 adjustresults(L, nresults);
1087LUA_API
int lua_load (
lua_State *L, lua_Reader reader,
void *data,
1088 const char *chunkname,
const char *mode) {
1092 if (!chunkname) chunkname =
"?";
1093 luaZ_init(L, &z, reader, data);
1094 status = luaD_protectedparser(L, &z, chunkname, mode);
1095 if (status == LUA_OK) {
1096 LClosure *f = clLvalue(s2v(L->top.p - 1));
1097 if (f->nupvalues >= 1) {
1099 const TValue *gt = getGtable(L);
1101 setobj(L, f->upvals[0]->v.p, gt);
1102 luaC_barrier(L, f->upvals[0], gt);
1110LUA_API
int lua_dump (
lua_State *L, lua_Writer writer,
void *data,
int strip) {
1114 api_checknelems(L, 1);
1115 o = s2v(L->top.p - 1);
1117 status = luaU_dump(L, getproto(o), writer, data, strip);
1133LUA_API
int lua_gc (
lua_State *L,
int what, ...) {
1137 if (g->gcstp & GCSTPGC)
1140 va_start(argp, what);
1143 g->gcstp = GCSTPUSR;
1146 case LUA_GCRESTART: {
1151 case LUA_GCCOLLECT: {
1157 res = cast_int(gettotalbytes(g) >> 10);
1160 case LUA_GCCOUNTB: {
1161 res = cast_int(gettotalbytes(g) & 0x3ff);
1165 int data = va_arg(argp,
int);
1167 lu_byte oldstp = g->gcstp;
1174 debt = cast(l_mem, data) * 1024 + g->GCdebt;
1175 luaE_setdebt(g, debt);
1179 if (debt > 0 && g->gcstate == GCSpause)
1183 case LUA_GCSETPAUSE: {
1184 int data = va_arg(argp,
int);
1185 res = getgcparam(g->gcpause);
1186 setgcparam(g->gcpause, data);
1189 case LUA_GCSETSTEPMUL: {
1190 int data = va_arg(argp,
int);
1191 res = getgcparam(g->gcstepmul);
1192 setgcparam(g->gcstepmul, data);
1195 case LUA_GCISRUNNING: {
1200 int minormul = va_arg(argp,
int);
1201 int majormul = va_arg(argp,
int);
1202 res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
1204 g->genminormul = minormul;
1206 setgcparam(g->genmajormul, majormul);
1207 luaC_changemode(L, KGC_GEN);
1211 int pause = va_arg(argp,
int);
1212 int stepmul = va_arg(argp,
int);
1213 int stepsize = va_arg(argp,
int);
1214 res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
1216 setgcparam(g->gcpause, pause);
1218 setgcparam(g->gcstepmul, stepmul);
1220 g->gcstepsize = stepsize;
1221 luaC_changemode(L, KGC_INC);
1241 errobj = s2v(L->top.p - 1);
1242 api_checknelems(L, 1);
1244 if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1253LUA_API
int lua_next (
lua_State *L,
int idx) {
1257 api_checknelems(L, 1);
1258 t = gettable(L, idx);
1259 more = luaH_next(L, t, L->top.p - 1);
1270LUA_API
void lua_toclose (
lua_State *L,
int idx) {
1274 o = index2stack(L, idx);
1275 nresults = L->ci->nresults;
1276 api_check(L, L->tbclist.p < o,
"given index below or equal a marked one");
1277 luaF_newtbcupval(L, o);
1278 if (!hastocloseCfunc(nresults))
1279 L->ci->nresults = codeNresults(nresults);
1280 lua_assert(hastocloseCfunc(L->ci->nresults));
1285LUA_API
void lua_concat (
lua_State *L,
int n) {
1287 api_checknelems(L, n);
1291 setsvalue2s(L, L->top.p, luaS_newlstr(L,
"", 0));
1299LUA_API
void lua_len (
lua_State *L,
int idx) {
1302 t = index2value(L, idx);
1303 luaV_objlen(L, L->top.p, t);
1309LUA_API lua_Alloc lua_getallocf (
lua_State *L,
void **ud) {
1312 if (ud) *ud = G(L)->ud;
1319LUA_API
void lua_setallocf (
lua_State *L, lua_Alloc f,
void *ud) {
1327void lua_setwarnf (
lua_State *L, lua_WarnFunction f,
void *ud) {
1335void lua_warning (
lua_State *L,
const char *msg,
int tocont) {
1337 luaE_warning(L, msg, tocont);
1343LUA_API
void *lua_newuserdatauv (
lua_State *L,
size_t size,
int nuvalue) {
1346 api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX,
"invalid value");
1347 u = luaS_newudata(L, size, nuvalue);
1348 setuvalue(L, s2v(L->top.p), u);
1352 return getudatamem(u);
1357static const char *aux_upvalue (
TValue *fi,
int n,
TValue **val,
1359 switch (ttypetag(fi)) {
1362 if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
1364 *val = &f->upvalue[n-1];
1365 if (owner) *owner = obj2gco(f);
1372 if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
1374 *val = f->upvals[n-1]->v.p;
1375 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1376 name = p->upvalues[n-1].name;
1377 return (name == NULL) ?
"(no name)" : getstr(name);
1379 default:
return NULL;
1384LUA_API
const char *lua_getupvalue (
lua_State *L,
int funcindex,
int n) {
1388 name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
1390 setobj2s(L, L->top.p, val);
1398LUA_API
const char *lua_setupvalue (
lua_State *L,
int funcindex,
int n) {
1404 fi = index2value(L, funcindex);
1405 api_checknelems(L, 1);
1406 name = aux_upvalue(fi, n, &val, &owner);
1409 setobj(L, val, s2v(L->top.p));
1410 luaC_barrier(L, owner, val);
1418 static const UpVal *
const nullup = NULL;
1420 TValue *fi = index2value(L, fidx);
1421 api_check(L, ttisLclosure(fi),
"Lua function expected");
1424 if (1 <= n && n <= f->p->sizeupvalues)
1425 return &f->upvals[n - 1];
1427 return (
UpVal**)&nullup;
1431LUA_API
void *lua_upvalueid (
lua_State *L,
int fidx,
int n) {
1432 TValue *fi = index2value(L, fidx);
1433 switch (ttypetag(fi)) {
1435 return *getupvalref(L, fidx, n, NULL);
1439 if (1 <= n && n <= f->nupvalues)
1440 return &f->upvalue[n - 1];
1446 api_check(L, 0,
"function expected");
1453LUA_API
void lua_upvaluejoin (
lua_State *L,
int fidx1,
int n1,
1454 int fidx2,
int n2) {
1456 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1457 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1458 api_check(L, *up1 != NULL && *up2 != NULL,
"invalid upvalue index");
1460 luaC_objbarrier(L, f1, *up1);