47#define obj_at(L,k) s2v(L->ci->func.p + (k))
53static void setnameval (
lua_State *L,
const char *name,
int val) {
54 lua_pushinteger(L, val);
55 lua_setfield(L, -2, name);
60 setobj2s(L, L->top.p, o);
65static void badexit (
const char *fmt,
const char *s1,
const char *s2) {
66 fprintf(stderr, fmt, s1);
68 fprintf(stderr,
"extra info: %s\n", s2);
70 l_memcontrol.numblocks = l_memcontrol.total = 0;
76 const char *msg = lua_tostring(L, -1);
77 if (msg == NULL) msg =
"error object is not a string";
78 return (badexit(
"PANIC: unprotected error in call to Lua API (%s)\n",
94static void warnf (
void *ud,
const char *msg,
int tocont) {
96 static char buff[200] =
"";
99 static int lasttocont = 0;
100 if (!lasttocont && !tocont && *msg ==
'@') {
102 badexit(
"Control warning during warning: %s\naborting...\n", msg, buff);
103 if (strcmp(msg,
"@off") == 0)
105 else if (strcmp(msg,
"@on") == 0)
107 else if (strcmp(msg,
"@normal") == 0)
109 else if (strcmp(msg,
"@allow") == 0)
111 else if (strcmp(msg,
"@store") == 0)
114 badexit(
"Invalid control warning in test mode: %s\naborting...\n",
119 if (strlen(msg) >=
sizeof(buff) - strlen(buff))
120 badexit(
"warnf-buffer overflow (%s)\n", msg, buff);
124 luaL_checkstack(L, 1,
"warn stack space");
125 lua_getglobal(L,
"_WARN");
126 if (!lua_toboolean(L, -1))
129 badexit(
"Unhandled warning in store mode: %s\naborting...\n",
130 lua_tostring(L, -1), buff);
135 if (buff[0] !=
'#' && onoff)
136 badexit(
"Unexpected warning in test mode: %s\naborting...\n",
141 fprintf(stderr,
"Lua warning: %s\n", buff);
146 luaL_checkstack(L, 1,
"warn stack space");
147 lua_pushstring(L, buff);
148 lua_setglobal(L,
"_WARN");
175#if !defined(EXTERNMEMCHECK)
179#define fillmem(mem,size) memset(mem, -MARK, size)
185#define fillmem(mem,size)
191 {0, 0UL, 0UL, 0UL, 0UL, (~0UL),
192 {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
197 size_t size = block->d.size;
199 for (i = 0; i < MARKSIZE; i++)
200 lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
201 mc->objcount[block->d.type]--;
202 fillmem(block,
sizeof(
Header) + size + MARKSIZE);
210void *debug_realloc (
void *ud,
void *b,
size_t oldsize,
size_t size) {
214 if (mc->memlimit == 0) {
215 char *limit = getenv(
"MEMLIMIT");
216 mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
219 type = (oldsize < LUA_NUMTAGS) ? oldsize : 0;
224 type = block->d.type;
225 lua_assert(oldsize == block->d.size);
228 freeblock(mc, block);
235 if (mc->countlimit != ~0UL && size != oldsize) {
236 if (mc->countlimit == 0)
240 if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
245 size_t commonsize = (oldsize < size) ? oldsize : size;
246 size_t realsize =
sizeof(
Header) + size + MARKSIZE;
247 if (realsize < size)
return NULL;
248 newblock = cast(
Header *, malloc(realsize));
249 if (newblock == NULL)
252 memcpy(newblock + 1, block + 1, commonsize);
253 freeblock(mc, block);
256 fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize);
258 for (i = 0; i < MARKSIZE; i++)
259 *(cast_charp(newblock + 1) + size + i) = MARK;
260 newblock->d.size = size;
261 newblock->d.type = type;
263 if (mc->total > mc->maxmem)
264 mc->maxmem = mc->total;
266 mc->objcount[type]++;
297 if (isdead(g,t))
return 0;
300 else if (g->gckind == KGC_INC)
301 return !(isblack(f) && iswhite(t));
303 if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
305 if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) &&
314 printf(
"||%s(%p)-%c%c(%02X)||",
315 ttypename(novariant(o->tt)), (
void *)o,
316 isdead(g,o) ?
'd' : isblack(o) ?
'b' : iswhite(o) ?
'w' :
'g',
317 "ns01oTt"[getage(o)], o->marked);
318 if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR)
319 printf(
" '%s'", getstr(gco2ts(o)));
328 int r1 = testobjref1(g, f, t);
330 printf(
"%d(%02X) - ", g->gcstate, g->currentwhite);
341 assert(testobjref(g, f, t));
351#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); }
355 assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t))));
361 unsigned int asize = luaH_realasize(h);
362 Node *n, *limit = gnode(h, sizenode(h));
364 checkobjrefN(g, hgc, h->metatable);
365 for (i = 0; i < asize; i++)
366 checkvalref(g, hgc, &h->array[i]);
367 for (n = gnode(h, 0); n < limit; n++) {
368 if (!isempty(gval(n))) {
370 getnodekey(g->mainthread, &k, n);
371 assert(!keyisnil(n));
372 checkvalref(g, hgc, &k);
373 checkvalref(g, hgc, gval(n));
382 checkobjrefN(g, hgc, u->metatable);
383 for (i = 0; i < u->nuvalue; i++)
384 checkvalref(g, hgc, &u->uv[i].uv);
391 checkobjrefN(g, fgc, f->source);
392 for (i=0; i<f->sizek; i++) {
393 if (iscollectable(f->k + i))
394 checkobjref(g, fgc, gcvalue(f->k + i));
396 for (i=0; i<f->sizeupvalues; i++)
397 checkobjrefN(g, fgc, f->upvalues[i].name);
398 for (i=0; i<f->sizep; i++)
399 checkobjrefN(g, fgc, f->p[i]);
400 for (i=0; i<f->sizelocvars; i++)
401 checkobjrefN(g, fgc, f->locvars[i].varname);
408 for (i = 0; i < cl->nupvalues; i++)
409 checkvalref(g, clgc, &cl->upvalue[i]);
416 checkobjrefN(g, clgc, cl->p);
417 for (i=0; i<cl->nupvalues; i++) {
418 UpVal *uv = cl->upvals[i];
420 checkobjrefN(g, clgc, uv);
422 checkvalref(g, obj2gco(uv), uv->v.p);
428static int lua_checkpc (
CallInfo *ci) {
429 if (!isLua(ci))
return 1;
431 StkId f = ci->func.p;
432 Proto *p = clLvalue(s2v(f))->p;
433 return p->code <= ci->u.l.savedpc &&
434 ci->u.l.savedpc <= p->code + p->sizecode;
443 assert(!isdead(g, L1));
444 if (L1->stack.p == NULL) {
445 assert(L1->openupval == NULL && L1->ci == NULL);
448 for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
449 assert(upisopen(uv));
450 assert(L1->top.p <= L1->stack_last.p);
451 assert(L1->tbclist.p <= L1->top.p);
452 for (ci = L1->ci; ci != NULL; ci = ci->previous) {
453 assert(ci->top.p <= L1->stack_last.p);
454 assert(lua_checkpc(ci));
456 for (o = L1->stack.p; o < L1->stack_last.p; o++)
457 checkliveness(L1, s2v(o));
463 case LUA_VUSERDATA: {
464 checkudata(g, gco2u(o));
468 checkvalref(g, o, gco2upv(o)->v.p);
472 checktable(g, gco2t(o));
476 checkstack(g, gco2th(o));
480 checkLclosure(g, gco2lcl(o));
484 checkCclosure(g, gco2ccl(o));
488 checkproto(g, gco2p(o));
517 assert(g->gcstate != GCSpause || iswhite(o));
518 if (g->gckind == KGC_GEN) {
519 assert(getage(o) >= listage);
520 assert(!iswhite(o) || !isold(o));
523 getage(o) == G_TOUCHED1 ||
524 getage(o) == G_OLD0 ||
525 o->tt == LUA_VTHREAD ||
526 (o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
538 assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2));
539 assert(!testbit(o->marked, TESTBIT));
540 if (keepinvariant(g))
541 l_setbit(o->marked, TESTBIT);
544 case LUA_VTABLE: o = gco2t(o)->gclist;
break;
545 case LUA_VLCL: o = gco2lcl(o)->gclist;
break;
546 case LUA_VCCL: o = gco2ccl(o)->gclist;
break;
547 case LUA_VTHREAD: o = gco2th(o)->gclist;
break;
548 case LUA_VPROTO: o = gco2p(o)->gclist;
break;
550 assert(gco2u(o)->nuvalue > 0);
551 o = gco2u(o)->gclist;
565 if (!keepinvariant(g))
return total;
566 total += checkgraylist(g, g->gray);
567 total += checkgraylist(g, g->grayagain);
568 total += checkgraylist(g, g->weak);
569 total += checkgraylist(g, g->allweak);
570 total += checkgraylist(g, g->ephemeron);
581 if (!keepinvariant(g))
583 if (o->tt == LUA_VUPVAL) {
585 assert(!isgray(o) || upisopen(gco2upv(o)));
589 if (isgray(o) || getage(o) == G_TOUCHED2) {
591 assert(testbit(o->marked, TESTBIT));
592 resetbit(o->marked, TESTBIT);
597static lu_mem checklist (
global_State *g,
int maybedead,
int tof,
601 for (o = newl; o != survival; o = o->next) {
602 checkobject(g, o, maybedead, G_NEW);
603 incifingray(g, o, &total);
604 assert(!tof == !tofinalize(o));
606 for (o = survival; o != old; o = o->next) {
607 checkobject(g, o, 0, G_SURVIVAL);
608 incifingray(g, o, &total);
609 assert(!tof == !tofinalize(o));
611 for (o = old; o != reallyold; o = o->next) {
612 checkobject(g, o, 0, G_OLD1);
613 incifingray(g, o, &total);
614 assert(!tof == !tofinalize(o));
616 for (o = reallyold; o != NULL; o = o->next) {
617 checkobject(g, o, 0, G_OLD);
618 incifingray(g, o, &total);
619 assert(!tof == !tofinalize(o));
631 if (keepinvariant(g)) {
632 assert(!iswhite(g->mainthread));
633 assert(!iswhite(gcvalue(&g->l_registry)));
635 assert(!isdead(g, gcvalue(&g->l_registry)));
636 assert(g->sweepgc == NULL || issweepphase(g));
637 totalin = checkgrays(g);
640 for (o = g->fixedgc; o != NULL; o = o->next) {
641 assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD);
645 maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
646 totalshould = checklist(g, maybedead, 0, g->allgc,
647 g->survival, g->old1, g->reallyold);
650 totalshould += checklist(g, 0, 1, g->finobj,
651 g->finobjsur, g->finobjold1, g->finobjrold);
654 for (o = g->tobefnz; o != NULL; o = o->next) {
655 checkobject(g, o, 0, G_NEW);
656 incifingray(g, o, &totalshould);
657 assert(tofinalize(o));
658 assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE);
660 if (keepinvariant(g))
661 assert(totalin == totalshould);
676static char *buildop (
Proto *p,
int pc,
char *buff) {
678 Instruction i = p->code[pc];
679 OpCode o = GET_OPCODE(i);
680 const char *name = opnames[o];
681 int line = luaG_getfuncline(p, pc);
682 int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0;
683 if (lineinfo == ABSLINEINFO)
684 buff += sprintf(buff,
"(__");
686 buff += sprintf(buff,
"(%2d", lineinfo);
687 buff += sprintf(buff,
" - %4d) %4d - ", line, pc);
688 switch (getOpMode(o)) {
690 sprintf(buff,
"%-12s%4d %4d %4d%s", name,
691 GETARG_A(i), GETARG_B(i), GETARG_C(i),
692 GETARG_k(i) ?
" (k)" :
"");
695 sprintf(buff,
"%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
698 sprintf(buff,
"%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
701 sprintf(buff,
"%-12s%4d", name, GETARG_Ax(i));
704 sprintf(buff,
"%-12s%4d", name, GETARG_sJ(i));
712void luaI_printcode (
Proto *pt,
int size) {
714 for (pc=0; pc<size; pc++) {
716 printf(
"%s\n", buildop(pt, pc, buff));
722void luaI_printinst (
Proto *pt,
int pc) {
724 printf(
"%s\n", buildop(pt, pc, buff));
732 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
733 1,
"Lua function expected");
734 p = getproto(obj_at(L, 1));
736 setnameval(L,
"maxstack", p->maxstacksize);
737 setnameval(L,
"numparams", p->numparams);
738 for (pc=0; pc<p->sizecode; pc++) {
740 lua_pushinteger(L, pc+1);
741 lua_pushstring(L, buildop(p, pc, buff));
751 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
752 1,
"Lua function expected");
753 p = getproto(obj_at(L, 1));
754 printf(
"maxstack: %d\n", p->maxstacksize);
755 printf(
"numparams: %d\n", p->numparams);
756 for (pc=0; pc<p->sizecode; pc++) {
758 printf(
"%s\n", buildop(p, pc, buff));
767 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
768 1,
"Lua function expected");
769 p = getproto(obj_at(L, 1));
770 lua_createtable(L, p->sizek, 0);
771 for (i=0; i<p->sizek; i++) {
772 pushobject(L, p->k+i);
773 lua_rawseti(L, -2, i+1);
779static int listabslineinfo (
lua_State *L) {
782 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
783 1,
"Lua function expected");
784 p = getproto(obj_at(L, 1));
785 luaL_argcheck(L, p->abslineinfo != NULL, 1,
"function has no debug info");
786 lua_createtable(L, 2 * p->sizeabslineinfo, 0);
787 for (i=0; i < p->sizeabslineinfo; i++) {
788 lua_pushinteger(L, p->abslineinfo[i].pc);
789 lua_rawseti(L, -2, 2 * i + 1);
790 lua_pushinteger(L, p->abslineinfo[i].line);
791 lua_rawseti(L, -2, 2 * i + 2);
799 int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
802 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
803 1,
"Lua function expected");
804 p = getproto(obj_at(L, 1));
805 while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
806 lua_pushstring(L, name);
816 int n = lua_gettop(L);
817 printf(
"stack: >>\n");
818 for (i = 1; i <= n; i++) {
819 printf(
"%3d: %s\n", i, luaL_tolstring(L, i, NULL));
827 lua_createtable(L, 0, 6);
828 setnameval(L,
"IS32INT", LUAI_IS32INT);
829 setnameval(L,
"MAXARG_Ax", MAXARG_Ax);
830 setnameval(L,
"MAXARG_Bx", MAXARG_Bx);
831 setnameval(L,
"OFFSET_sBx", OFFSET_sBx);
832 setnameval(L,
"LFPF", LFIELDS_PER_FLUSH);
833 setnameval(L,
"NUM_OPCODES", NUM_OPCODES);
839 if (lua_isnone(L, 1)) {
840 lua_pushinteger(L, l_memcontrol.total);
841 lua_pushinteger(L, l_memcontrol.numblocks);
842 lua_pushinteger(L, l_memcontrol.maxmem);
845 else if (lua_isnumber(L, 1)) {
846 unsigned long limit = cast(
unsigned long, luaL_checkinteger(L, 1));
847 if (limit == 0) limit = ULONG_MAX;
848 l_memcontrol.memlimit = limit;
852 const char *t = luaL_checkstring(L, 1);
854 for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
855 if (strcmp(t, ttypename(i)) == 0) {
856 lua_pushinteger(L, l_memcontrol.objcount[i]);
860 return luaL_error(L,
"unknown type '%s'", t);
866 if (lua_isnone(L, 1))
867 l_memcontrol.countlimit = ~0L;
869 l_memcontrol.countlimit = luaL_checkinteger(L, 1);
874static int alloc_failnext (
lua_State *L) {
876 l_memcontrol.failnext = 1;
882 if (ttisnil(obj_at(L, 1)))
885 l_Trick = gcvalue(obj_at(L, 1));
894 if (!iscollectable(o))
895 lua_pushstring(L,
"no collectable");
898 lua_pushstring(L, isdead(G(L), obj) ?
"dead" :
899 iswhite(obj) ?
"white" :
900 isblack(obj) ?
"black" :
"gray");
910 if (!iscollectable(o))
911 lua_pushstring(L,
"no collectable");
913 static const char *gennames[] = {
"new",
"survival",
"old0",
"old1",
914 "old",
"touched1",
"touched2"};
916 lua_pushstring(L, gennames[getage(obj)]);
926 if (!iscollectable(o))
927 printf(
"no collectable\n");
938 static const char *statenames[] = {
939 "propagate",
"atomic",
"enteratomic",
"sweepallgc",
"sweepfinobj",
940 "sweeptobefnz",
"sweepend",
"callfin",
"pause",
""};
941 static const int states[] = {
942 GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj,
943 GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1};
944 int option = states[luaL_checkoption(L, 1,
"", statenames)];
946 lua_pushstring(L, statenames[G(L)->gcstate]);
951 if (G(L)->gckind == KGC_GEN)
952 luaL_error(L,
"cannot change states in generational mode");
954 if (option < g->gcstate) {
955 luaC_runtilstate(L, bitmask(GCSpause));
957 luaC_runtilstate(L, bitmask(option));
958 lua_assert(G(L)->gcstate == option);
966 if (lua_isnone(L, 2)) {
967 luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1,
"string expected");
968 lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
973 luaL_checktype(L, 2, LUA_TTABLE);
974 t = hvalue(obj_at(L, 2));
975 lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
983 lua_pushinteger(L, (L->top.p - L->stack.p));
984 lua_pushinteger(L, stacksize(L));
985 lua_pushinteger(L, L->nCcalls);
986 lua_pushinteger(L, L->nci);
987 lua_pushinteger(L, (
unsigned long)&a);
994 int i = cast_int(luaL_optinteger(L, 2, -1));
996 luaL_checktype(L, 1, LUA_TTABLE);
997 t = hvalue(obj_at(L, 1));
998 asize = luaH_realasize(t);
1000 lua_pushinteger(L, asize);
1001 lua_pushinteger(L, allocsizenode(t));
1002 lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node);
1003 lua_pushinteger(L, t->alimit);
1006 else if ((
unsigned int)i < asize) {
1007 lua_pushinteger(L, i);
1008 pushobject(L, &t->array[i]);
1011 else if ((i -= asize) < sizenode(t)) {
1013 getnodekey(L, &k, gnode(t, i));
1014 if (!isempty(gval(gnode(t, i))) ||
1020 lua_pushliteral(L,
"<undef>");
1021 pushobject(L, gval(gnode(t, i)));
1022 if (gnext(&t->node[i]) != 0)
1023 lua_pushinteger(L, gnext(&t->node[i]));
1033 int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
1035 lua_pushinteger(L ,tb->size);
1036 lua_pushinteger(L ,tb->nuse);
1039 else if (s < tb->size) {
1042 for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
1043 setsvalue2s(L, L->top.p, ts);
1054 int level = lua_gettop(L);
1055 luaL_checkany(L, 1);
1056 lua_pushvalue(L, 1);
1057 lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
1059 lua_assert(lua_gettop(L) == level+1);
1064 int level = lua_gettop(L);
1065 lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
1067 lua_assert(lua_gettop(L) == level+1);
1072 int level = lua_gettop(L);
1073 luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
1075 lua_assert(lua_gettop(L) == level);
1081 int n = cast_int(luaL_checkinteger(L, 2));
1082 luaL_checktype(L, 1, LUA_TFUNCTION);
1083 if (lua_isnone(L, 3)) {
1084 const char *name = lua_getupvalue(L, 1, n);
1085 if (name == NULL)
return 0;
1086 lua_pushstring(L, name);
1090 const char *name = lua_setupvalue(L, 1, n);
1091 lua_pushstring(L, name);
1098 size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
1099 int nuv = luaL_optinteger(L, 2, 0);
1100 char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
1101 while (size--) *p++ =
'\0';
1107 lua_Integer u = luaL_checkinteger(L, 1);
1108 lua_pushlightuserdata(L, cast_voidp(cast_sizet(u)));
1114 lua_pushinteger(L, cast(
long, lua_touserdata(L, 1)));
1122 const char *s = luaL_checklstring(L, 1, &l);
1123 int status = luaL_loadbuffer(L1, s, l, s);
1124 if (status == LUA_OK)
1125 status = lua_pcall(L1, 0, 0, 0);
1126 lua_pushinteger(L, status);
1132 lua_pushnumber(L, cast_num(*cast(
const double *, luaL_checkstring(L, 1))));
1138 double d = cast(
double, luaL_checknumber(L, 1));
1139 lua_pushlstring(L, cast_charp(&d),
sizeof(d));
1145 lua_pushinteger(L, lua_tointeger(L, 1));
1152 lua_Alloc f = lua_getallocf(L, &ud);
1155 lua_atpanic(L1, tpanic);
1156 lua_pushlightuserdata(L, L1);
1166 luaL_argcheck(L, L1 != NULL, 1,
"state expected");
1173 {LUA_GNAME, luaopen_base},
1174 {
"coroutine", luaopen_coroutine},
1175 {
"debug", luaopen_debug},
1178 {
"math", luaopen_math},
1179 {
"string", luaopen_string},
1180 {
"table", luaopen_table},
1181 {
"T", luaB_opentests},
1186 luaL_requiref(L1,
"package", luaopen_package, 0);
1187 lua_assert(lua_type(L1, -1) == LUA_TTABLE);
1189 luaL_requiref(L1,
"package", NULL, 1);
1191 lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
1192 luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
1193 for (i = 0; libs[i].name; i++) {
1194 lua_pushcfunction(L1, libs[i].func);
1195 lua_setfield(L1, -2, libs[i].name);
1209 const char *
code = luaL_checklstring(L, 2, &lcode);
1212 status = luaL_loadbuffer(L1,
code, lcode,
code);
1213 if (status == LUA_OK)
1214 status = lua_pcall(L1, 0, LUA_MULTRET, 0);
1215 if (status != LUA_OK) {
1217 lua_pushstring(L, lua_tostring(L1, -1));
1218 lua_pushinteger(L, status);
1223 while (!lua_isnone(L1, ++i))
1224 lua_pushstring(L, lua_tostring(L1, i));
1232 unsigned int x = (
unsigned int)luaL_checkinteger(L, 1);
1233 lua_pushinteger(L, luaO_ceillog2(x));
1238struct Aux { jmp_buf jb;
const char *paniccode; lua_State *L; };
1245 lua_checkstack(L, 1);
1246 lua_getfield(L, LUA_REGISTRYINDEX,
"_jmpbuf");
1247 b = (
struct Aux *)lua_touserdata(L, -1);
1249 runC(b->L, L, b->paniccode);
1258 const char *
code = luaL_checkstring(L, 1);
1259 lua_Alloc f = lua_getallocf(L, &ud);
1260 b.paniccode = luaL_optstring(L, 2,
"");
1262 L1 = lua_newstate(f, ud);
1267 lua_atpanic(L1, panicback);
1268 lua_pushlightuserdata(L1, &b);
1269 lua_setfield(L1, LUA_REGISTRYINDEX,
"_jmpbuf");
1270 if (setjmp(b.jb) == 0) {
1272 lua_pushliteral(L,
"no errors");
1276 lua_pushstring(L, lua_tostring(L1, -1));
1292static void sethookaux (
lua_State *L,
int mask,
int count,
const char *
code);
1294static const char *
const delimits =
" \t\n,;";
1296static void skip (
const char **pc) {
1298 if (**pc !=
'\0' && strchr(delimits, **pc)) (*pc)++;
1299 else if (**pc ==
'#') {
1300 while (**pc !=
'\n' && **pc !=
'\0') (*pc)++;
1311 res = cast_int(lua_tointeger(L1, -1));
1316 else if (**pc ==
'*') {
1317 res = lua_gettop(L1);
1321 else if (**pc ==
'-') {
1325 if (!lisdigit(cast_uchar(**pc)))
1326 luaL_error(L,
"number expected (%s)", *pc);
1327 while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) -
'0';
1331static const char *getstring_aux (
lua_State *L,
char *buff,
const char **pc) {
1334 if (**pc ==
'"' || **pc ==
'\'') {
1335 int quote = *(*pc)++;
1336 while (**pc != quote) {
1337 if (**pc ==
'\0') luaL_error(L,
"unfinished string in C script");
1338 buff[i++] = *(*pc)++;
1343 while (**pc !=
'\0' && !strchr(delimits, **pc))
1344 buff[i++] = *(*pc)++;
1354 case 'R':
return LUA_REGISTRYINDEX;
1355 case 'G':
return luaL_error(L,
"deprecated index 'G'");
1356 case 'U':
return lua_upvalueindex(getnum_aux(L, L1, pc));
1357 default: (*pc)--;
return getnum_aux(L, L1, pc);
1362static const char *
const statcodes[] = {
"OK",
"YIELD",
"ERRRUN",
1363 "ERRSYNTAX", MEMERRMSG,
"ERRGCMM",
"ERRERR"};
1371 for (i = 0; i <
sizeof(statcodes) /
sizeof(statcodes[0]); i++) {
1372 lua_pushboolean(L, 1);
1373 lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]);
1378#define EQ(s1) (strcmp(s1, inst) == 0)
1380#define getnum (getnum_aux(L, L1, &pc))
1381#define getstring (getstring_aux(L, buff, &pc))
1382#define getindex (getindex_aux(L, L1, &pc))
1386static int Cfunck (
lua_State *L,
int status, lua_KContext ctx);
1396static const char ops[] =
"+-*%^/\\&|~<>_!";
1401 if (pc == NULL)
return luaL_error(L,
"attempt to runC null script");
1403 const char *inst = getstring;
1405 else if EQ(
"absindex") {
1406 lua_pushnumber(L1, lua_absindex(L1, getindex));
1408 else if EQ(
"append") {
1410 int i = lua_rawlen(L1, t);
1411 lua_rawseti(L1, t, i + 1);
1413 else if EQ(
"arith") {
1416 op = strchr(ops, *pc++) - ops;
1419 else if EQ(
"call") {
1422 lua_call(L1, narg, nres);
1424 else if EQ(
"callk") {
1428 lua_callk(L1, narg, nres, i, Cfunck);
1430 else if EQ(
"checkstack") {
1432 const char *msg = getstring;
1435 luaL_checkstack(L1, sz, msg);
1437 else if EQ(
"rawcheckstack") {
1439 lua_pushboolean(L1, lua_checkstack(L1, sz));
1441 else if EQ(
"compare") {
1442 const char *opt = getstring;
1443 int op = (opt[0] ==
'E') ? LUA_OPEQ
1444 : (opt[1] ==
'T') ? LUA_OPLT : LUA_OPLE;
1447 lua_pushboolean(L1, lua_compare(L1, a, b, op));
1449 else if EQ(
"concat") {
1450 lua_concat(L1, getnum);
1452 else if EQ(
"copy") {
1454 lua_copy(L1, f, getindex);
1456 else if EQ(
"func2num") {
1457 lua_CFunction func = lua_tocfunction(L1, getindex);
1458 lua_pushnumber(L1, cast_sizet(func));
1460 else if EQ(
"getfield") {
1462 lua_getfield(L1, t, getstring);
1464 else if EQ(
"getglobal") {
1465 lua_getglobal(L1, getstring);
1467 else if EQ(
"getmetatable") {
1468 if (lua_getmetatable(L1, getindex) == 0)
1471 else if EQ(
"gettable") {
1472 lua_gettable(L1, getindex);
1474 else if EQ(
"gettop") {
1475 lua_pushinteger(L1, lua_gettop(L1));
1477 else if EQ(
"gsub") {
1478 int a = getnum;
int b = getnum;
int c = getnum;
1479 luaL_gsub(L1, lua_tostring(L1, a),
1480 lua_tostring(L1, b),
1481 lua_tostring(L1, c));
1483 else if EQ(
"insert") {
1484 lua_insert(L1, getnum);
1486 else if EQ(
"iscfunction") {
1487 lua_pushboolean(L1, lua_iscfunction(L1, getindex));
1489 else if EQ(
"isfunction") {
1490 lua_pushboolean(L1, lua_isfunction(L1, getindex));
1492 else if EQ(
"isnil") {
1493 lua_pushboolean(L1, lua_isnil(L1, getindex));
1495 else if EQ(
"isnull") {
1496 lua_pushboolean(L1, lua_isnone(L1, getindex));
1498 else if EQ(
"isnumber") {
1499 lua_pushboolean(L1, lua_isnumber(L1, getindex));
1501 else if EQ(
"isstring") {
1502 lua_pushboolean(L1, lua_isstring(L1, getindex));
1504 else if EQ(
"istable") {
1505 lua_pushboolean(L1, lua_istable(L1, getindex));
1507 else if EQ(
"isudataval") {
1508 lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
1510 else if EQ(
"isuserdata") {
1511 lua_pushboolean(L1, lua_isuserdata(L1, getindex));
1514 lua_len(L1, getindex);
1516 else if EQ(
"Llen") {
1517 lua_pushinteger(L1, luaL_len(L1, getindex));
1519 else if EQ(
"loadfile") {
1520 luaL_loadfile(L1, luaL_checkstring(L1, getnum));
1522 else if EQ(
"loadstring") {
1523 const char *s = luaL_checkstring(L1, getnum);
1524 luaL_loadstring(L1, s);
1526 else if EQ(
"newmetatable") {
1527 lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
1529 else if EQ(
"newtable") {
1532 else if EQ(
"newthread") {
1535 else if EQ(
"resetthread") {
1536 lua_pushinteger(L1, lua_resetthread(L1));
1538 else if EQ(
"newuserdata") {
1539 lua_newuserdata(L1, getnum);
1541 else if EQ(
"next") {
1544 else if EQ(
"objsize") {
1545 lua_pushinteger(L1, lua_rawlen(L1, getindex));
1547 else if EQ(
"pcall") {
1550 status = lua_pcall(L1, narg, nres, getnum);
1552 else if EQ(
"pcallk") {
1556 status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
1559 lua_pop(L1, getnum);
1561 else if EQ(
"printstack") {
1564 printf(
"%s\n", luaL_tolstring(L1, n, NULL));
1567 else printstack(L1);
1569 else if EQ(
"print") {
1570 const char *msg = getstring;
1571 printf(
"%s\n", msg);
1573 else if EQ(
"warningC") {
1574 const char *msg = getstring;
1575 lua_warning(L1, msg, 1);
1577 else if EQ(
"warning") {
1578 const char *msg = getstring;
1579 lua_warning(L1, msg, 0);
1581 else if EQ(
"pushbool") {
1582 lua_pushboolean(L1, getnum);
1584 else if EQ(
"pushcclosure") {
1585 lua_pushcclosure(L1, testC, getnum);
1587 else if EQ(
"pushint") {
1588 lua_pushinteger(L1, getnum);
1590 else if EQ(
"pushnil") {
1593 else if EQ(
"pushnum") {
1594 lua_pushnumber(L1, (lua_Number)getnum);
1596 else if EQ(
"pushstatus") {
1597 lua_pushstring(L1, statcodes[status]);
1599 else if EQ(
"pushstring") {
1600 lua_pushstring(L1, getstring);
1602 else if EQ(
"pushupvalueindex") {
1603 lua_pushinteger(L1, lua_upvalueindex(getnum));
1605 else if EQ(
"pushvalue") {
1606 lua_pushvalue(L1, getindex);
1608 else if EQ(
"pushfstringI") {
1609 lua_pushfstring(L1, lua_tostring(L, -2), (
int)lua_tointeger(L, -1));
1611 else if EQ(
"pushfstringS") {
1612 lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1));
1614 else if EQ(
"pushfstringP") {
1615 lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
1617 else if EQ(
"rawget") {
1621 else if EQ(
"rawgeti") {
1623 lua_rawgeti(L1, t, getnum);
1625 else if EQ(
"rawgetp") {
1627 lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
1629 else if EQ(
"rawset") {
1633 else if EQ(
"rawseti") {
1635 lua_rawseti(L1, t, getnum);
1637 else if EQ(
"rawsetp") {
1639 lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
1641 else if EQ(
"remove") {
1642 lua_remove(L1, getnum);
1644 else if EQ(
"replace") {
1645 lua_replace(L1, getindex);
1647 else if EQ(
"resume") {
1650 status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
1652 else if EQ(
"return") {
1656 for (i = 0; i < n; i++) {
1658 switch (lua_type(L1, idx)) {
1660 lua_pushboolean(L, lua_toboolean(L1, idx));
1663 lua_pushstring(L, lua_tostring(L1, idx));
1670 else if EQ(
"rotate") {
1672 lua_rotate(L1, i, getnum);
1674 else if EQ(
"setfield") {
1676 const char *s = getstring;
1677 lua_setfield(L1, t, s);
1679 else if EQ(
"seti") {
1681 lua_seti(L1, t, getnum);
1683 else if EQ(
"setglobal") {
1684 const char *s = getstring;
1685 lua_setglobal(L1, s);
1687 else if EQ(
"sethook") {
1690 const char *s = getstring;
1691 sethookaux(L1, mask, count, s);
1693 else if EQ(
"setmetatable") {
1695 lua_setmetatable(L1, idx);
1697 else if EQ(
"settable") {
1698 lua_settable(L1, getindex);
1700 else if EQ(
"settop") {
1701 lua_settop(L1, getnum);
1703 else if EQ(
"testudata") {
1705 lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
1707 else if EQ(
"error") {
1710 else if EQ(
"abort") {
1713 else if EQ(
"throw") {
1714#if defined(__cplusplus)
1715static struct X {
int x; } x;
1718 luaL_error(L1,
"C++");
1722 else if EQ(
"tobool") {
1723 lua_pushboolean(L1, lua_toboolean(L1, getindex));
1725 else if EQ(
"tocfunction") {
1726 lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
1728 else if EQ(
"tointeger") {
1729 lua_pushinteger(L1, lua_tointeger(L1, getindex));
1731 else if EQ(
"tonumber") {
1732 lua_pushnumber(L1, lua_tonumber(L1, getindex));
1734 else if EQ(
"topointer") {
1735 lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex)));
1737 else if EQ(
"touserdata") {
1738 lua_pushlightuserdata(L1, lua_touserdata(L1, getindex));
1740 else if EQ(
"tostring") {
1741 const char *s = lua_tostring(L1, getindex);
1742 const char *s1 = lua_pushstring(L1, s);
1744 lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
1746 else if EQ(
"Ltolstring") {
1747 luaL_tolstring(L1, getindex, NULL);
1749 else if EQ(
"type") {
1750 lua_pushstring(L1, luaL_typename(L1, getnum));
1752 else if EQ(
"xmove") {
1755 lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
1756 lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
1758 if (n == 0) n = lua_gettop(fs);
1759 lua_xmove(fs, ts, n);
1761 else if EQ(
"isyieldable") {
1762 lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex)));
1764 else if EQ(
"yield") {
1765 return lua_yield(L1, getnum);
1767 else if EQ(
"yieldk") {
1770 return lua_yieldk(L1, nres, i, Cfunck);
1772 else if EQ(
"toclose") {
1773 lua_toclose(L1, getnum);
1775 else if EQ(
"closeslot") {
1776 lua_closeslot(L1, getnum);
1778 else luaL_error(L,
"unknown instruction %s", buff);
1787 if (lua_isuserdata(L, 1)) {
1789 pc = luaL_checkstring(L, 2);
1791 else if (lua_isthread(L, 1)) {
1792 L1 = lua_tothread(L, 1);
1793 pc = luaL_checkstring(L, 2);
1797 pc = luaL_checkstring(L, 1);
1799 return runC(L, L1, pc);
1804 return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
1808static int Cfunck (
lua_State *L,
int status, lua_KContext ctx) {
1809 lua_pushstring(L, statcodes[status]);
1810 lua_setglobal(L,
"status");
1811 lua_pushinteger(L, ctx);
1812 lua_setglobal(L,
"ctx");
1813 return runC(L, L, lua_tostring(L, ctx));
1818 luaL_checkstring(L, 1);
1819 lua_pushcclosure(L, Cfunc, lua_gettop(L));
1838 const char *
const events [] = {
"call",
"ret",
"line",
"count",
"tailcall"};
1839 lua_getfield(L, LUA_REGISTRYINDEX,
"C_HOOK");
1840 lua_pushlightuserdata(L, L);
1841 lua_gettable(L, -2);
1842 scpt = lua_tostring(L, -1);
1844 lua_pushstring(L, events[ar->event]);
1845 lua_pushinteger(L, ar->currentline);
1853static void sethookaux (
lua_State *L,
int mask,
int count,
const char *scpt) {
1854 if (*scpt ==
'\0') {
1855 lua_sethook(L, NULL, 0, 0);
1858 lua_getfield(L, LUA_REGISTRYINDEX,
"C_HOOK");
1859 if (!lua_istable(L, -1)) {
1862 lua_pushvalue(L, -1);
1863 lua_setfield(L, LUA_REGISTRYINDEX,
"C_HOOK");
1865 lua_pushlightuserdata(L, L);
1866 lua_pushstring(L, scpt);
1867 lua_settable(L, -3);
1868 lua_sethook(L, Chook, mask, count);
1873 if (lua_isnoneornil(L, 1))
1874 lua_sethook(L, NULL, 0, 0);
1876 const char *scpt = luaL_checkstring(L, 1);
1877 const char *smask = luaL_checkstring(L, 2);
1878 int count = cast_int(luaL_optinteger(L, 3, 0));
1880 if (strchr(smask,
'c')) mask |= LUA_MASKCALL;
1881 if (strchr(smask,
'r')) mask |= LUA_MASKRET;
1882 if (strchr(smask,
'l')) mask |= LUA_MASKLINE;
1883 if (count > 0) mask |= LUA_MASKCOUNT;
1884 sethookaux(L, mask, count, scpt);
1893 luaL_argcheck(L, co, 1,
"coroutine expected");
1894 status = lua_resume(co, L, 0, &nres);
1895 if (status != LUA_OK && status != LUA_YIELD) {
1896 lua_pushboolean(L, 0);
1901 lua_pushboolean(L, 1);
1910static const struct luaL_Reg tests_funcs[] = {
1911 {
"checkmemory", lua_checkmemory},
1912 {
"closestate", closestate},
1914 {
"doonnewstack", doonnewstack},
1915 {
"doremote", doremote},
1916 {
"gccolor", gc_color},
1918 {
"gcstate", gc_state},
1919 {
"pobj", gc_printobj},
1921 {
"hash", hash_query},
1923 {
"limits", get_limits},
1924 {
"listcode", listcode},
1925 {
"printcode", printcode},
1927 {
"listabslineinfo", listabslineinfo},
1928 {
"listlocals", listlocals},
1929 {
"loadlib", loadlib},
1930 {
"checkpanic", checkpanic},
1931 {
"newstate", newstate},
1932 {
"newuserdata", newuserdata},
1933 {
"num2int", num2int},
1934 {
"pushuserdata", pushuserdata},
1935 {
"querystr", string_query},
1936 {
"querytab", table_query},
1938 {
"resume", coresume},
1940 {
"sethook", sethook},
1941 {
"stacklevel", stacklevel},
1943 {
"makeCfunc", makeCfunc},
1944 {
"totalmem", mem_query},
1945 {
"alloccount", alloc_count},
1946 {
"allocfailnext", alloc_failnext},
1947 {
"trick", settrick},
1948 {
"udataval", udataval},
1950 {
"upvalue", upvalue},
1955static void checkfinalmem (
void) {
1956 lua_assert(l_memcontrol.numblocks == 0);
1957 lua_assert(l_memcontrol.total == 0);
1963 lua_Alloc f = lua_getallocf(L, &ud);
1964 lua_atpanic(L, &tpanic);
1965 lua_setwarnf(L, &warnf, L);
1966 lua_pushboolean(L, 0);
1967 lua_setglobal(L,
"_WARN");
1969 atexit(checkfinalmem);
1970 lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol));
1971 lua_setallocf(L, f, ud);
1972 luaL_newlib(L, tests_funcs);