ltests.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. ** $Id: ltests.c $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ltests_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <setjmp.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "lua.h"
  15. #include "lapi.h"
  16. #include "lauxlib.h"
  17. #include "lcode.h"
  18. #include "lctype.h"
  19. #include "ldebug.h"
  20. #include "ldo.h"
  21. #include "lfunc.h"
  22. #include "lmem.h"
  23. #include "lopcodes.h"
  24. #include "lopnames.h"
  25. #include "lstate.h"
  26. #include "lstring.h"
  27. #include "ltable.h"
  28. #include "lualib.h"
  29. /*
  30. ** The whole module only makes sense with LUA_DEBUG on
  31. */
  32. #if defined(LUA_DEBUG)
  33. void *l_Trick = 0;
  34. #define obj_at(L,k) s2v(L->ci->func + (k))
  35. static int runC (lua_State *L, lua_State *L1, const char *pc);
  36. static void setnameval (lua_State *L, const char *name, int val) {
  37. lua_pushinteger(L, val);
  38. lua_setfield(L, -2, name);
  39. }
  40. static void pushobject (lua_State *L, const TValue *o) {
  41. setobj2s(L, L->top, o);
  42. api_incr_top(L);
  43. }
  44. static void badexit (const char *fmt, const char *s) {
  45. fprintf(stderr, fmt, s);
  46. /* avoid assertion failures when exiting */
  47. l_memcontrol.numblocks = l_memcontrol.total = 0;
  48. exit(EXIT_FAILURE);
  49. }
  50. static int tpanic (lua_State *L) {
  51. return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
  52. lua_tostring(L, -1)),
  53. 0); /* do not return to Lua */
  54. }
  55. /*
  56. ** Warning function for tests. Fist, it concatenates all parts of
  57. ** a warning in buffer 'buff'. Then:
  58. ** - messages starting with '#' are shown on standard output (used to
  59. ** test explicit warnings);
  60. ** - messages containing '@' are stored in global '_WARN' (used to test
  61. ** errors that generate warnings);
  62. ** - other messages abort the tests (they represent real warning
  63. ** conditions; the standard tests should not generate these conditions
  64. ** unexpectedly).
  65. */
  66. static void warnf (void *ud, const char *msg, int tocont) {
  67. static char buff[200] = ""; /* should be enough for tests... */
  68. if (strlen(msg) >= sizeof(buff) - strlen(buff))
  69. badexit("%s", "warnf-buffer overflow");
  70. strcat(buff, msg); /* add new message to current warning */
  71. if (!tocont) { /* message finished? */
  72. if (buff[0] == '#') /* expected warning? */
  73. printf("Expected Lua warning: %s\n", buff); /* print it */
  74. else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */
  75. lua_State *L = cast(lua_State *, ud);
  76. lua_unlock(L);
  77. lua_pushstring(L, buff);
  78. lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
  79. lua_lock(L);
  80. }
  81. else /* a real warning; should not happen during tests */
  82. badexit("Unexpected warning in test mode: %s\naborting...\n", buff);
  83. buff[0] = '\0'; /* prepare buffer for next warning */
  84. }
  85. }
  86. /*
  87. ** {======================================================================
  88. ** Controlled version for realloc.
  89. ** =======================================================================
  90. */
  91. #define MARK 0x55 /* 01010101 (a nice pattern) */
  92. typedef union Header {
  93. LUAI_MAXALIGN;
  94. struct {
  95. size_t size;
  96. int type;
  97. } d;
  98. } Header;
  99. #if !defined(EXTERNMEMCHECK)
  100. /* full memory check */
  101. #define MARKSIZE 16 /* size of marks after each block */
  102. #define fillmem(mem,size) memset(mem, -MARK, size)
  103. #else
  104. /* external memory check: don't do it twice */
  105. #define MARKSIZE 0
  106. #define fillmem(mem,size) /* empty */
  107. #endif
  108. Memcontrol l_memcontrol =
  109. {0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
  110. static void freeblock (Memcontrol *mc, Header *block) {
  111. if (block) {
  112. size_t size = block->d.size;
  113. int i;
  114. for (i = 0; i < MARKSIZE; i++) /* check marks after block */
  115. lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
  116. mc->objcount[block->d.type]--;
  117. fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */
  118. free(block); /* actually free block */
  119. mc->numblocks--; /* update counts */
  120. mc->total -= size;
  121. }
  122. }
  123. void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
  124. Memcontrol *mc = cast(Memcontrol *, ud);
  125. Header *block = cast(Header *, b);
  126. int type;
  127. if (mc->memlimit == 0) { /* first time? */
  128. char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
  129. mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
  130. }
  131. if (block == NULL) {
  132. type = (oldsize < LUA_NUMTAGS) ? oldsize : 0;
  133. oldsize = 0;
  134. }
  135. else {
  136. block--; /* go to real header */
  137. type = block->d.type;
  138. lua_assert(oldsize == block->d.size);
  139. }
  140. if (size == 0) {
  141. freeblock(mc, block);
  142. return NULL;
  143. }
  144. if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */
  145. if (mc->countlimit == 0)
  146. return NULL; /* fake a memory allocation error */
  147. mc->countlimit--;
  148. }
  149. if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  150. return NULL; /* fake a memory allocation error */
  151. else {
  152. Header *newblock;
  153. int i;
  154. size_t commonsize = (oldsize < size) ? oldsize : size;
  155. size_t realsize = sizeof(Header) + size + MARKSIZE;
  156. if (realsize < size) return NULL; /* arithmetic overflow! */
  157. newblock = cast(Header *, malloc(realsize)); /* alloc a new block */
  158. if (newblock == NULL)
  159. return NULL; /* really out of memory? */
  160. if (block) {
  161. memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
  162. freeblock(mc, block); /* erase (and check) old copy */
  163. }
  164. /* initialize new part of the block with something weird */
  165. fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize);
  166. /* initialize marks after block */
  167. for (i = 0; i < MARKSIZE; i++)
  168. *(cast_charp(newblock + 1) + size + i) = MARK;
  169. newblock->d.size = size;
  170. newblock->d.type = type;
  171. mc->total += size;
  172. if (mc->total > mc->maxmem)
  173. mc->maxmem = mc->total;
  174. mc->numblocks++;
  175. mc->objcount[type]++;
  176. return newblock + 1;
  177. }
  178. }
  179. /* }====================================================================== */
  180. /*
  181. ** {======================================================
  182. ** Functions to check memory consistency
  183. ** =======================================================
  184. */
  185. /*
  186. ** Check GC invariants. For incremental mode, a black object cannot
  187. ** point to a white one. For generational mode, really old objects
  188. ** cannot point to young objects. Both old1 and touched2 objects
  189. ** cannot point to new objects (but can point to survivals).
  190. ** (Threads and open upvalues, despite being marked "really old",
  191. ** continue to be visited in all collections, and therefore can point to
  192. ** new objects. They, and only they, are old but gray.)
  193. */
  194. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  195. if (isdead(g,t)) return 0;
  196. if (issweepphase(g))
  197. return 1; /* no invariants */
  198. else if (g->gckind == KGC_INC)
  199. return !(isblack(f) && iswhite(t)); /* basic incremental invariant */
  200. else { /* generational mode */
  201. if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
  202. return 0;
  203. if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) &&
  204. getage(t) == G_NEW)
  205. return 0;
  206. return 1;
  207. }
  208. }
  209. static void printobj (global_State *g, GCObject *o) {
  210. printf("||%s(%p)-%c%c(%02X)||",
  211. ttypename(novariant(o->tt)), (void *)o,
  212. isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
  213. "ns01oTt"[getage(o)], o->marked);
  214. if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR)
  215. printf(" '%s'", getstr(gco2ts(o)));
  216. }
  217. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  218. int r1 = testobjref1(g, f, t);
  219. if (!r1) {
  220. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  221. printobj(g, f);
  222. printf(" -> ");
  223. printobj(g, t);
  224. printf("\n");
  225. }
  226. return r1;
  227. }
  228. #define checkobjref(g,f,t) \
  229. { if (t) lua_longassert(testobjref(g,f,obj2gco(t))); }
  230. static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
  231. lua_assert(!iscollectable(t) ||
  232. (righttt(t) && testobjref(g, f, gcvalue(t))));
  233. }
  234. static void checktable (global_State *g, Table *h) {
  235. unsigned int i;
  236. unsigned int asize = luaH_realasize(h);
  237. Node *n, *limit = gnode(h, sizenode(h));
  238. GCObject *hgc = obj2gco(h);
  239. checkobjref(g, hgc, h->metatable);
  240. for (i = 0; i < asize; i++)
  241. checkvalref(g, hgc, &h->array[i]);
  242. for (n = gnode(h, 0); n < limit; n++) {
  243. if (!isempty(gval(n))) {
  244. TValue k;
  245. getnodekey(g->mainthread, &k, n);
  246. lua_assert(!keyisnil(n));
  247. checkvalref(g, hgc, &k);
  248. checkvalref(g, hgc, gval(n));
  249. }
  250. }
  251. }
  252. static void checkudata (global_State *g, Udata *u) {
  253. int i;
  254. GCObject *hgc = obj2gco(u);
  255. checkobjref(g, hgc, u->metatable);
  256. for (i = 0; i < u->nuvalue; i++)
  257. checkvalref(g, hgc, &u->uv[i].uv);
  258. }
  259. /*
  260. ** All marks are conditional because a GC may happen while the
  261. ** prototype is still being created
  262. */
  263. static void checkproto (global_State *g, Proto *f) {
  264. int i;
  265. GCObject *fgc = obj2gco(f);
  266. checkobjref(g, fgc, f->source);
  267. for (i=0; i<f->sizek; i++) {
  268. if (ttisstring(f->k + i))
  269. checkobjref(g, fgc, tsvalue(f->k + i));
  270. }
  271. for (i=0; i<f->sizeupvalues; i++)
  272. checkobjref(g, fgc, f->upvalues[i].name);
  273. for (i=0; i<f->sizep; i++)
  274. checkobjref(g, fgc, f->p[i]);
  275. for (i=0; i<f->sizelocvars; i++)
  276. checkobjref(g, fgc, f->locvars[i].varname);
  277. }
  278. static void checkCclosure (global_State *g, CClosure *cl) {
  279. GCObject *clgc = obj2gco(cl);
  280. int i;
  281. for (i = 0; i < cl->nupvalues; i++)
  282. checkvalref(g, clgc, &cl->upvalue[i]);
  283. }
  284. static void checkLclosure (global_State *g, LClosure *cl) {
  285. GCObject *clgc = obj2gco(cl);
  286. int i;
  287. checkobjref(g, clgc, cl->p);
  288. for (i=0; i<cl->nupvalues; i++) {
  289. UpVal *uv = cl->upvals[i];
  290. if (uv) {
  291. checkobjref(g, clgc, uv);
  292. if (!upisopen(uv))
  293. checkvalref(g, obj2gco(uv), uv->v);
  294. }
  295. }
  296. }
  297. static int lua_checkpc (CallInfo *ci) {
  298. if (!isLua(ci)) return 1;
  299. else {
  300. StkId f = ci->func;
  301. Proto *p = clLvalue(s2v(f))->p;
  302. return p->code <= ci->u.l.savedpc &&
  303. ci->u.l.savedpc <= p->code + p->sizecode;
  304. }
  305. }
  306. static void checkstack (global_State *g, lua_State *L1) {
  307. StkId o;
  308. CallInfo *ci;
  309. UpVal *uv;
  310. lua_assert(!isdead(g, L1));
  311. for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
  312. lua_assert(upisopen(uv)); /* must be open */
  313. for (ci = L1->ci; ci != NULL; ci = ci->previous) {
  314. lua_assert(ci->top <= L1->stack_last);
  315. lua_assert(lua_checkpc(ci));
  316. }
  317. if (L1->stack) { /* complete thread? */
  318. for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
  319. checkliveness(L1, s2v(o)); /* entire stack must have valid values */
  320. }
  321. else lua_assert(L1->stacksize == 0);
  322. }
  323. static void checkrefs (global_State *g, GCObject *o) {
  324. switch (o->tt) {
  325. case LUA_TUSERDATA: {
  326. checkudata(g, gco2u(o));
  327. break;
  328. }
  329. case LUA_TUPVAL: {
  330. checkvalref(g, o, gco2upv(o)->v);
  331. break;
  332. }
  333. case LUA_TTABLE: {
  334. checktable(g, gco2t(o));
  335. break;
  336. }
  337. case LUA_TTHREAD: {
  338. checkstack(g, gco2th(o));
  339. break;
  340. }
  341. case LUA_TLCL: {
  342. checkLclosure(g, gco2lcl(o));
  343. break;
  344. }
  345. case LUA_TCCL: {
  346. checkCclosure(g, gco2ccl(o));
  347. break;
  348. }
  349. case LUA_TPROTO: {
  350. checkproto(g, gco2p(o));
  351. break;
  352. }
  353. case LUA_TSHRSTR:
  354. case LUA_TLNGSTR: {
  355. lua_assert(!isgray(o)); /* strings are never gray */
  356. break;
  357. }
  358. default: lua_assert(0);
  359. }
  360. }
  361. /*
  362. ** Check consistency of an object:
  363. ** - Dead objects can only happen in the 'allgc' list during a sweep
  364. ** phase (controled by the caller through 'maybedead').
  365. ** - During pause, all objects must be white.
  366. ** - In generational mode:
  367. ** * objects must be old enough for their lists ('listage').
  368. ** * old objects cannot be white.
  369. ** * old objects must be black, except for 'touched1', 'old0',
  370. ** threads, and open upvalues.
  371. */
  372. static void checkobject (global_State *g, GCObject *o, int maybedead,
  373. int listage) {
  374. if (isdead(g, o))
  375. lua_assert(maybedead);
  376. else {
  377. lua_assert(g->gcstate != GCSpause || iswhite(o));
  378. if (g->gckind == KGC_GEN) { /* generational mode? */
  379. lua_assert(getage(o) >= listage);
  380. lua_assert(!iswhite(o) || !isold(o));
  381. if (isold(o)) {
  382. lua_assert(isblack(o) ||
  383. getage(o) == G_TOUCHED1 ||
  384. getage(o) == G_OLD0 ||
  385. o->tt == LUA_TTHREAD ||
  386. (o->tt == LUA_TUPVAL && upisopen(gco2upv(o))));
  387. }
  388. }
  389. checkrefs(g, o);
  390. }
  391. }
  392. static void checkgraylist (global_State *g, GCObject *o) {
  393. ((void)g); /* better to keep it available if we need to print an object */
  394. while (o) {
  395. lua_assert(isgray(o) || getage(o) == G_TOUCHED2);
  396. switch (o->tt) {
  397. case LUA_TTABLE: o = gco2t(o)->gclist; break;
  398. case LUA_TLCL: o = gco2lcl(o)->gclist; break;
  399. case LUA_TCCL: o = gco2ccl(o)->gclist; break;
  400. case LUA_TTHREAD: o = gco2th(o)->gclist; break;
  401. case LUA_TPROTO: o = gco2p(o)->gclist; break;
  402. default: lua_assert(0); /* other objects cannot be in a gray list */
  403. }
  404. }
  405. }
  406. /*
  407. ** Check objects in gray lists.
  408. */
  409. static void checkgrays (global_State *g) {
  410. if (!keepinvariant(g)) return;
  411. checkgraylist(g, g->gray);
  412. checkgraylist(g, g->grayagain);
  413. checkgraylist(g, g->weak);
  414. checkgraylist(g, g->ephemeron);
  415. }
  416. static void checklist (global_State *g, int maybedead, int tof,
  417. GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
  418. GCObject *o;
  419. for (o = newl; o != survival; o = o->next) {
  420. checkobject(g, o, maybedead, G_NEW);
  421. lua_assert(!tof == !tofinalize(o));
  422. }
  423. for (o = survival; o != old; o = o->next) {
  424. checkobject(g, o, 0, G_SURVIVAL);
  425. lua_assert(!tof == !tofinalize(o));
  426. }
  427. for (o = old; o != reallyold; o = o->next) {
  428. checkobject(g, o, 0, G_OLD1);
  429. lua_assert(!tof == !tofinalize(o));
  430. }
  431. for (o = reallyold; o != NULL; o = o->next) {
  432. checkobject(g, o, 0, G_OLD);
  433. lua_assert(!tof == !tofinalize(o));
  434. }
  435. }
  436. int lua_checkmemory (lua_State *L) {
  437. global_State *g = G(L);
  438. GCObject *o;
  439. int maybedead;
  440. if (keepinvariant(g)) {
  441. lua_assert(!iswhite(g->mainthread));
  442. lua_assert(!iswhite(gcvalue(&g->l_registry)));
  443. }
  444. lua_assert(!isdead(g, gcvalue(&g->l_registry)));
  445. lua_assert(g->sweepgc == NULL || issweepphase(g));
  446. checkgrays(g);
  447. /* check 'fixedgc' list */
  448. for (o = g->fixedgc; o != NULL; o = o->next) {
  449. lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD);
  450. }
  451. /* check 'allgc' list */
  452. maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
  453. checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold);
  454. /* check 'finobj' list */
  455. checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold);
  456. /* check 'tobefnz' list */
  457. for (o = g->tobefnz; o != NULL; o = o->next) {
  458. checkobject(g, o, 0, G_NEW);
  459. lua_assert(tofinalize(o));
  460. lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE);
  461. }
  462. return 0;
  463. }
  464. /* }====================================================== */
  465. /*
  466. ** {======================================================
  467. ** Disassembler
  468. ** =======================================================
  469. */
  470. static char *buildop (Proto *p, int pc, char *buff) {
  471. char *obuff = buff;
  472. Instruction i = p->code[pc];
  473. OpCode o = GET_OPCODE(i);
  474. const char *name = opnames[o];
  475. int line = luaG_getfuncline(p, pc);
  476. int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0;
  477. if (lineinfo == ABSLINEINFO)
  478. buff += sprintf(buff, "(__");
  479. else
  480. buff += sprintf(buff, "(%2d", lineinfo);
  481. buff += sprintf(buff, " - %4d) %4d - ", line, pc);
  482. switch (getOpMode(o)) {
  483. case iABC:
  484. sprintf(buff, "%-12s%4d %4d %4d%s", name,
  485. GETARG_A(i), GETARG_B(i), GETARG_C(i),
  486. GETARG_k(i) ? " (k)" : "");
  487. break;
  488. case iABx:
  489. sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  490. break;
  491. case iAsBx:
  492. sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  493. break;
  494. case iAx:
  495. sprintf(buff, "%-12s%4d", name, GETARG_Ax(i));
  496. break;
  497. case isJ:
  498. sprintf(buff, "%-12s%4d", name, GETARG_sJ(i));
  499. break;
  500. }
  501. return obuff;
  502. }
  503. #if 0
  504. void luaI_printcode (Proto *pt, int size) {
  505. int pc;
  506. for (pc=0; pc<size; pc++) {
  507. char buff[100];
  508. printf("%s\n", buildop(pt, pc, buff));
  509. }
  510. printf("-------\n");
  511. }
  512. void luaI_printinst (Proto *pt, int pc) {
  513. char buff[100];
  514. printf("%s\n", buildop(pt, pc, buff));
  515. }
  516. #endif
  517. static int listcode (lua_State *L) {
  518. int pc;
  519. Proto *p;
  520. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  521. 1, "Lua function expected");
  522. p = getproto(obj_at(L, 1));
  523. lua_newtable(L);
  524. setnameval(L, "maxstack", p->maxstacksize);
  525. setnameval(L, "numparams", p->numparams);
  526. for (pc=0; pc<p->sizecode; pc++) {
  527. char buff[100];
  528. lua_pushinteger(L, pc+1);
  529. lua_pushstring(L, buildop(p, pc, buff));
  530. lua_settable(L, -3);
  531. }
  532. return 1;
  533. }
  534. static int printcode (lua_State *L) {
  535. int pc;
  536. Proto *p;
  537. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  538. 1, "Lua function expected");
  539. p = getproto(obj_at(L, 1));
  540. printf("maxstack: %d\n", p->maxstacksize);
  541. printf("numparams: %d\n", p->numparams);
  542. for (pc=0; pc<p->sizecode; pc++) {
  543. char buff[100];
  544. printf("%s\n", buildop(p, pc, buff));
  545. }
  546. return 0;
  547. }
  548. static int listk (lua_State *L) {
  549. Proto *p;
  550. int i;
  551. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  552. 1, "Lua function expected");
  553. p = getproto(obj_at(L, 1));
  554. lua_createtable(L, p->sizek, 0);
  555. for (i=0; i<p->sizek; i++) {
  556. pushobject(L, p->k+i);
  557. lua_rawseti(L, -2, i+1);
  558. }
  559. return 1;
  560. }
  561. static int listabslineinfo (lua_State *L) {
  562. Proto *p;
  563. int i;
  564. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  565. 1, "Lua function expected");
  566. p = getproto(obj_at(L, 1));
  567. luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
  568. lua_createtable(L, 2 * p->sizeabslineinfo, 0);
  569. for (i=0; i < p->sizeabslineinfo; i++) {
  570. lua_pushinteger(L, p->abslineinfo[i].pc);
  571. lua_rawseti(L, -2, 2 * i + 1);
  572. lua_pushinteger(L, p->abslineinfo[i].line);
  573. lua_rawseti(L, -2, 2 * i + 2);
  574. }
  575. return 1;
  576. }
  577. static int listlocals (lua_State *L) {
  578. Proto *p;
  579. int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
  580. int i = 0;
  581. const char *name;
  582. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  583. 1, "Lua function expected");
  584. p = getproto(obj_at(L, 1));
  585. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  586. lua_pushstring(L, name);
  587. return i-1;
  588. }
  589. /* }====================================================== */
  590. static void printstack (lua_State *L) {
  591. int i;
  592. int n = lua_gettop(L);
  593. for (i = 1; i <= n; i++) {
  594. printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
  595. lua_pop(L, 1);
  596. }
  597. printf("\n");
  598. }
  599. static int get_limits (lua_State *L) {
  600. lua_createtable(L, 0, 6);
  601. setnameval(L, "IS32INT", LUAI_IS32INT);
  602. setnameval(L, "MAXARG_Ax", MAXARG_Ax);
  603. setnameval(L, "MAXARG_Bx", MAXARG_Bx);
  604. setnameval(L, "OFFSET_sBx", OFFSET_sBx);
  605. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  606. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  607. return 1;
  608. }
  609. static int mem_query (lua_State *L) {
  610. if (lua_isnone(L, 1)) {
  611. lua_pushinteger(L, l_memcontrol.total);
  612. lua_pushinteger(L, l_memcontrol.numblocks);
  613. lua_pushinteger(L, l_memcontrol.maxmem);
  614. return 3;
  615. }
  616. else if (lua_isnumber(L, 1)) {
  617. unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1));
  618. if (limit == 0) limit = ULONG_MAX;
  619. l_memcontrol.memlimit = limit;
  620. return 0;
  621. }
  622. else {
  623. const char *t = luaL_checkstring(L, 1);
  624. int i;
  625. for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
  626. if (strcmp(t, ttypename(i)) == 0) {
  627. lua_pushinteger(L, l_memcontrol.objcount[i]);
  628. return 1;
  629. }
  630. }
  631. return luaL_error(L, "unkown type '%s'", t);
  632. }
  633. }
  634. static int alloc_count (lua_State *L) {
  635. if (lua_isnone(L, 1))
  636. l_memcontrol.countlimit = ~0L;
  637. else
  638. l_memcontrol.countlimit = luaL_checkinteger(L, 1);
  639. return 0;
  640. }
  641. static int settrick (lua_State *L) {
  642. if (ttisnil(obj_at(L, 1)))
  643. l_Trick = NULL;
  644. else
  645. l_Trick = gcvalue(obj_at(L, 1));
  646. return 0;
  647. }
  648. static int gc_color (lua_State *L) {
  649. TValue *o;
  650. luaL_checkany(L, 1);
  651. o = obj_at(L, 1);
  652. if (!iscollectable(o))
  653. lua_pushstring(L, "no collectable");
  654. else {
  655. GCObject *obj = gcvalue(o);
  656. lua_pushstring(L, isdead(G(L), obj) ? "dead" :
  657. iswhite(obj) ? "white" :
  658. isblack(obj) ? "black" : "gray");
  659. }
  660. return 1;
  661. }
  662. static int gc_age (lua_State *L) {
  663. TValue *o;
  664. luaL_checkany(L, 1);
  665. o = obj_at(L, 1);
  666. if (!iscollectable(o))
  667. lua_pushstring(L, "no collectable");
  668. else {
  669. static const char *gennames[] = {"new", "survival", "old0", "old1",
  670. "old", "touched1", "touched2"};
  671. GCObject *obj = gcvalue(o);
  672. lua_pushstring(L, gennames[getage(obj)]);
  673. }
  674. return 1;
  675. }
  676. static int gc_printobj (lua_State *L) {
  677. TValue *o;
  678. luaL_checkany(L, 1);
  679. o = obj_at(L, 1);
  680. if (!iscollectable(o))
  681. printf("no collectable\n");
  682. else {
  683. GCObject *obj = gcvalue(o);
  684. printobj(G(L), obj);
  685. printf("\n");
  686. }
  687. return 0;
  688. }
  689. static int gc_state (lua_State *L) {
  690. static const char *statenames[] = {
  691. "propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj",
  692. "sweeptobefnz", "sweepend", "callfin", "pause", ""};
  693. static const int states[] = {
  694. GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj,
  695. GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1};
  696. int option = states[luaL_checkoption(L, 1, "", statenames)];
  697. if (option == -1) {
  698. lua_pushstring(L, statenames[G(L)->gcstate]);
  699. return 1;
  700. }
  701. else {
  702. global_State *g = G(L);
  703. if (G(L)->gckind == KGC_GEN)
  704. luaL_error(L, "cannot change states in generational mode");
  705. lua_lock(L);
  706. if (option < g->gcstate) { /* must cross 'pause'? */
  707. luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */
  708. }
  709. luaC_runtilstate(L, bitmask(option));
  710. lua_assert(G(L)->gcstate == option);
  711. lua_unlock(L);
  712. return 0;
  713. }
  714. }
  715. static int hash_query (lua_State *L) {
  716. if (lua_isnone(L, 2)) {
  717. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  718. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  719. }
  720. else {
  721. TValue *o = obj_at(L, 1);
  722. Table *t;
  723. luaL_checktype(L, 2, LUA_TTABLE);
  724. t = hvalue(obj_at(L, 2));
  725. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  726. }
  727. return 1;
  728. }
  729. static int stacklevel (lua_State *L) {
  730. unsigned long a = 0;
  731. lua_pushinteger(L, (L->top - L->stack));
  732. lua_pushinteger(L, (L->stack_last - L->stack));
  733. lua_pushinteger(L, L->nCcalls);
  734. lua_pushinteger(L, L->nci);
  735. lua_pushinteger(L, (unsigned long)&a);
  736. return 5;
  737. }
  738. static int table_query (lua_State *L) {
  739. const Table *t;
  740. int i = cast_int(luaL_optinteger(L, 2, -1));
  741. unsigned int asize;
  742. luaL_checktype(L, 1, LUA_TTABLE);
  743. t = hvalue(obj_at(L, 1));
  744. asize = luaH_realasize(t);
  745. if (i == -1) {
  746. lua_pushinteger(L, asize);
  747. lua_pushinteger(L, allocsizenode(t));
  748. lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node);
  749. lua_pushinteger(L, t->alimit);
  750. return 4;
  751. }
  752. else if ((unsigned int)i < asize) {
  753. lua_pushinteger(L, i);
  754. pushobject(L, &t->array[i]);
  755. lua_pushnil(L);
  756. }
  757. else if ((i -= asize) < sizenode(t)) {
  758. TValue k;
  759. getnodekey(L, &k, gnode(t, i));
  760. if (!isempty(gval(gnode(t, i))) ||
  761. ttisnil(&k) ||
  762. ttisnumber(&k)) {
  763. pushobject(L, &k);
  764. }
  765. else
  766. lua_pushliteral(L, "<undef>");
  767. pushobject(L, gval(gnode(t, i)));
  768. if (gnext(&t->node[i]) != 0)
  769. lua_pushinteger(L, gnext(&t->node[i]));
  770. else
  771. lua_pushnil(L);
  772. }
  773. return 3;
  774. }
  775. static int string_query (lua_State *L) {
  776. stringtable *tb = &G(L)->strt;
  777. int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
  778. if (s == -1) {
  779. lua_pushinteger(L ,tb->size);
  780. lua_pushinteger(L ,tb->nuse);
  781. return 2;
  782. }
  783. else if (s < tb->size) {
  784. TString *ts;
  785. int n = 0;
  786. for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
  787. setsvalue2s(L, L->top, ts);
  788. api_incr_top(L);
  789. n++;
  790. }
  791. return n;
  792. }
  793. else return 0;
  794. }
  795. static int tref (lua_State *L) {
  796. int level = lua_gettop(L);
  797. luaL_checkany(L, 1);
  798. lua_pushvalue(L, 1);
  799. lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
  800. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  801. return 1;
  802. }
  803. static int getref (lua_State *L) {
  804. int level = lua_gettop(L);
  805. lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
  806. lua_assert(lua_gettop(L) == level+1);
  807. return 1;
  808. }
  809. static int unref (lua_State *L) {
  810. int level = lua_gettop(L);
  811. luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
  812. lua_assert(lua_gettop(L) == level);
  813. return 0;
  814. }
  815. static int upvalue (lua_State *L) {
  816. int n = cast_int(luaL_checkinteger(L, 2));
  817. luaL_checktype(L, 1, LUA_TFUNCTION);
  818. if (lua_isnone(L, 3)) {
  819. const char *name = lua_getupvalue(L, 1, n);
  820. if (name == NULL) return 0;
  821. lua_pushstring(L, name);
  822. return 2;
  823. }
  824. else {
  825. const char *name = lua_setupvalue(L, 1, n);
  826. lua_pushstring(L, name);
  827. return 1;
  828. }
  829. }
  830. static int newuserdata (lua_State *L) {
  831. size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
  832. int nuv = luaL_optinteger(L, 2, 0);
  833. char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
  834. while (size--) *p++ = '\0';
  835. return 1;
  836. }
  837. static int pushuserdata (lua_State *L) {
  838. lua_Integer u = luaL_checkinteger(L, 1);
  839. lua_pushlightuserdata(L, cast_voidp(cast_sizet(u)));
  840. return 1;
  841. }
  842. static int udataval (lua_State *L) {
  843. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  844. return 1;
  845. }
  846. static int doonnewstack (lua_State *L) {
  847. lua_State *L1 = lua_newthread(L);
  848. size_t l;
  849. const char *s = luaL_checklstring(L, 1, &l);
  850. int status = luaL_loadbuffer(L1, s, l, s);
  851. if (status == LUA_OK)
  852. status = lua_pcall(L1, 0, 0, 0);
  853. lua_pushinteger(L, status);
  854. return 1;
  855. }
  856. static int s2d (lua_State *L) {
  857. lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1))));
  858. return 1;
  859. }
  860. static int d2s (lua_State *L) {
  861. double d = cast(double, luaL_checknumber(L, 1));
  862. lua_pushlstring(L, cast_charp(&d), sizeof(d));
  863. return 1;
  864. }
  865. static int num2int (lua_State *L) {
  866. lua_pushinteger(L, lua_tointeger(L, 1));
  867. return 1;
  868. }
  869. static int newstate (lua_State *L) {
  870. void *ud;
  871. lua_Alloc f = lua_getallocf(L, &ud);
  872. lua_State *L1 = lua_newstate(f, ud);
  873. if (L1) {
  874. lua_atpanic(L1, tpanic);
  875. lua_pushlightuserdata(L, L1);
  876. }
  877. else
  878. lua_pushnil(L);
  879. return 1;
  880. }
  881. static lua_State *getstate (lua_State *L) {
  882. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  883. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  884. return L1;
  885. }
  886. static int loadlib (lua_State *L) {
  887. static const luaL_Reg libs[] = {
  888. {LUA_GNAME, luaopen_base},
  889. {"coroutine", luaopen_coroutine},
  890. {"debug", luaopen_debug},
  891. {"io", luaopen_io},
  892. {"os", luaopen_os},
  893. {"math", luaopen_math},
  894. {"string", luaopen_string},
  895. {"table", luaopen_table},
  896. {"T", luaB_opentests},
  897. {NULL, NULL}
  898. };
  899. lua_State *L1 = getstate(L);
  900. int i;
  901. luaL_requiref(L1, "package", luaopen_package, 0);
  902. lua_assert(lua_type(L1, -1) == LUA_TTABLE);
  903. /* 'requiref' should not reload module already loaded... */
  904. luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */
  905. /* ...but should return the same module */
  906. lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
  907. luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  908. for (i = 0; libs[i].name; i++) {
  909. lua_pushcfunction(L1, libs[i].func);
  910. lua_setfield(L1, -2, libs[i].name);
  911. }
  912. return 0;
  913. }
  914. static int closestate (lua_State *L) {
  915. lua_State *L1 = getstate(L);
  916. lua_close(L1);
  917. return 0;
  918. }
  919. static int doremote (lua_State *L) {
  920. lua_State *L1 = getstate(L);
  921. size_t lcode;
  922. const char *code = luaL_checklstring(L, 2, &lcode);
  923. int status;
  924. lua_settop(L1, 0);
  925. status = luaL_loadbuffer(L1, code, lcode, code);
  926. if (status == LUA_OK)
  927. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  928. if (status != LUA_OK) {
  929. lua_pushnil(L);
  930. lua_pushstring(L, lua_tostring(L1, -1));
  931. lua_pushinteger(L, status);
  932. return 3;
  933. }
  934. else {
  935. int i = 0;
  936. while (!lua_isnone(L1, ++i))
  937. lua_pushstring(L, lua_tostring(L1, i));
  938. lua_pop(L1, i-1);
  939. return i-1;
  940. }
  941. }
  942. static int log2_aux (lua_State *L) {
  943. unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
  944. lua_pushinteger(L, luaO_ceillog2(x));
  945. return 1;
  946. }
  947. struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
  948. /*
  949. ** does a long-jump back to "main program".
  950. */
  951. static int panicback (lua_State *L) {
  952. struct Aux *b;
  953. lua_checkstack(L, 1); /* open space for 'Aux' struct */
  954. lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
  955. b = (struct Aux *)lua_touserdata(L, -1);
  956. lua_pop(L, 1); /* remove 'Aux' struct */
  957. runC(b->L, L, b->paniccode); /* run optional panic code */
  958. longjmp(b->jb, 1);
  959. return 1; /* to avoid warnings */
  960. }
  961. static int checkpanic (lua_State *L) {
  962. struct Aux b;
  963. void *ud;
  964. lua_State *L1;
  965. const char *code = luaL_checkstring(L, 1);
  966. lua_Alloc f = lua_getallocf(L, &ud);
  967. b.paniccode = luaL_optstring(L, 2, "");
  968. b.L = L;
  969. L1 = lua_newstate(f, ud); /* create new state */
  970. if (L1 == NULL) { /* error? */
  971. lua_pushnil(L);
  972. return 1;
  973. }
  974. lua_atpanic(L1, panicback); /* set its panic function */
  975. lua_pushlightuserdata(L1, &b);
  976. lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
  977. if (setjmp(b.jb) == 0) { /* set jump buffer */
  978. runC(L, L1, code); /* run code unprotected */
  979. lua_pushliteral(L, "no errors");
  980. }
  981. else { /* error handling */
  982. /* move error message to original state */
  983. lua_pushstring(L, lua_tostring(L1, -1));
  984. }
  985. lua_close(L1);
  986. return 1;
  987. }
  988. /*
  989. ** {====================================================================
  990. ** function to test the API with C. It interprets a kind of assembler
  991. ** language with calls to the API, so the test can be driven by Lua code
  992. ** =====================================================================
  993. */
  994. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  995. static const char *const delimits = " \t\n,;";
  996. static void skip (const char **pc) {
  997. for (;;) {
  998. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  999. else if (**pc == '#') { /* comment? */
  1000. while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */
  1001. }
  1002. else break;
  1003. }
  1004. }
  1005. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  1006. int res = 0;
  1007. int sig = 1;
  1008. skip(pc);
  1009. if (**pc == '.') {
  1010. res = cast_int(lua_tointeger(L1, -1));
  1011. lua_pop(L1, 1);
  1012. (*pc)++;
  1013. return res;
  1014. }
  1015. else if (**pc == '*') {
  1016. res = lua_gettop(L1);
  1017. (*pc)++;
  1018. return res;
  1019. }
  1020. else if (**pc == '-') {
  1021. sig = -1;
  1022. (*pc)++;
  1023. }
  1024. if (!lisdigit(cast_uchar(**pc)))
  1025. luaL_error(L, "number expected (%s)", *pc);
  1026. while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
  1027. return sig*res;
  1028. }
  1029. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  1030. int i = 0;
  1031. skip(pc);
  1032. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  1033. int quote = *(*pc)++;
  1034. while (**pc != quote) {
  1035. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  1036. buff[i++] = *(*pc)++;
  1037. }
  1038. (*pc)++;
  1039. }
  1040. else {
  1041. while (**pc != '\0' && !strchr(delimits, **pc))
  1042. buff[i++] = *(*pc)++;
  1043. }
  1044. buff[i] = '\0';
  1045. return buff;
  1046. }
  1047. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  1048. skip(pc);
  1049. switch (*(*pc)++) {
  1050. case 'R': return LUA_REGISTRYINDEX;
  1051. case 'G': return luaL_error(L, "deprecated index 'G'");
  1052. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  1053. default: (*pc)--; return getnum_aux(L, L1, pc);
  1054. }
  1055. }
  1056. static void pushcode (lua_State *L, int code) {
  1057. static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
  1058. "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
  1059. lua_pushstring(L, codes[code]);
  1060. }
  1061. #define EQ(s1) (strcmp(s1, inst) == 0)
  1062. #define getnum (getnum_aux(L, L1, &pc))
  1063. #define getstring (getstring_aux(L, buff, &pc))
  1064. #define getindex (getindex_aux(L, L1, &pc))
  1065. static int testC (lua_State *L);
  1066. static int Cfunck (lua_State *L, int status, lua_KContext ctx);
  1067. /*
  1068. ** arithmetic operation encoding for 'arith' instruction
  1069. ** LUA_OPIDIV -> \
  1070. ** LUA_OPSHL -> <
  1071. ** LUA_OPSHR -> >
  1072. ** LUA_OPUNM -> _
  1073. ** LUA_OPBNOT -> !
  1074. */
  1075. static const char ops[] = "+-*%^/\\&|~<>_!";
  1076. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  1077. char buff[300];
  1078. int status = 0;
  1079. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  1080. for (;;) {
  1081. const char *inst = getstring;
  1082. if EQ("") return 0;
  1083. else if EQ("absindex") {
  1084. lua_pushnumber(L1, lua_absindex(L1, getindex));
  1085. }
  1086. else if EQ("append") {
  1087. int t = getindex;
  1088. int i = lua_rawlen(L1, t);
  1089. lua_rawseti(L1, t, i + 1);
  1090. }
  1091. else if EQ("arith") {
  1092. int op;
  1093. skip(&pc);
  1094. op = strchr(ops, *pc++) - ops;
  1095. lua_arith(L1, op);
  1096. }
  1097. else if EQ("call") {
  1098. int narg = getnum;
  1099. int nres = getnum;
  1100. lua_call(L1, narg, nres);
  1101. }
  1102. else if EQ("callk") {
  1103. int narg = getnum;
  1104. int nres = getnum;
  1105. int i = getindex;
  1106. lua_callk(L1, narg, nres, i, Cfunck);
  1107. }
  1108. else if EQ("checkstack") {
  1109. int sz = getnum;
  1110. const char *msg = getstring;
  1111. if (*msg == '\0')
  1112. msg = NULL; /* to test 'luaL_checkstack' with no message */
  1113. luaL_checkstack(L1, sz, msg);
  1114. }
  1115. else if EQ("rawcheckstack") {
  1116. int sz = getnum;
  1117. lua_pushboolean(L1, lua_checkstack(L1, sz));
  1118. }
  1119. else if EQ("compare") {
  1120. const char *opt = getstring; /* EQ, LT, or LE */
  1121. int op = (opt[0] == 'E') ? LUA_OPEQ
  1122. : (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
  1123. int a = getindex;
  1124. int b = getindex;
  1125. lua_pushboolean(L1, lua_compare(L1, a, b, op));
  1126. }
  1127. else if EQ("concat") {
  1128. lua_concat(L1, getnum);
  1129. }
  1130. else if EQ("copy") {
  1131. int f = getindex;
  1132. lua_copy(L1, f, getindex);
  1133. }
  1134. else if EQ("func2num") {
  1135. lua_CFunction func = lua_tocfunction(L1, getindex);
  1136. lua_pushnumber(L1, cast_sizet(func));
  1137. }
  1138. else if EQ("getfield") {
  1139. int t = getindex;
  1140. lua_getfield(L1, t, getstring);
  1141. }
  1142. else if EQ("getglobal") {
  1143. lua_getglobal(L1, getstring);
  1144. }
  1145. else if EQ("getmetatable") {
  1146. if (lua_getmetatable(L1, getindex) == 0)
  1147. lua_pushnil(L1);
  1148. }
  1149. else if EQ("gettable") {
  1150. lua_gettable(L1, getindex);
  1151. }
  1152. else if EQ("gettop") {
  1153. lua_pushinteger(L1, lua_gettop(L1));
  1154. }
  1155. else if EQ("gsub") {
  1156. int a = getnum; int b = getnum; int c = getnum;
  1157. luaL_gsub(L1, lua_tostring(L1, a),
  1158. lua_tostring(L1, b),
  1159. lua_tostring(L1, c));
  1160. }
  1161. else if EQ("insert") {
  1162. lua_insert(L1, getnum);
  1163. }
  1164. else if EQ("iscfunction") {
  1165. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  1166. }
  1167. else if EQ("isfunction") {
  1168. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  1169. }
  1170. else if EQ("isnil") {
  1171. lua_pushboolean(L1, lua_isnil(L1, getindex));
  1172. }
  1173. else if EQ("isnull") {
  1174. lua_pushboolean(L1, lua_isnone(L1, getindex));
  1175. }
  1176. else if EQ("isnumber") {
  1177. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  1178. }
  1179. else if EQ("isstring") {
  1180. lua_pushboolean(L1, lua_isstring(L1, getindex));
  1181. }
  1182. else if EQ("istable") {
  1183. lua_pushboolean(L1, lua_istable(L1, getindex));
  1184. }
  1185. else if EQ("isudataval") {
  1186. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  1187. }
  1188. else if EQ("isuserdata") {
  1189. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  1190. }
  1191. else if EQ("len") {
  1192. lua_len(L1, getindex);
  1193. }
  1194. else if EQ("Llen") {
  1195. lua_pushinteger(L1, luaL_len(L1, getindex));
  1196. }
  1197. else if EQ("loadfile") {
  1198. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  1199. }
  1200. else if EQ("loadstring") {
  1201. const char *s = luaL_checkstring(L1, getnum);
  1202. luaL_loadstring(L1, s);
  1203. }
  1204. else if EQ("newmetatable") {
  1205. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1206. }
  1207. else if EQ("newtable") {
  1208. lua_newtable(L1);
  1209. }
  1210. else if EQ("newthread") {
  1211. lua_newthread(L1);
  1212. }
  1213. else if EQ("resetthread") {
  1214. lua_pushinteger(L1, lua_resetthread(L1));
  1215. }
  1216. else if EQ("newuserdata") {
  1217. lua_newuserdata(L1, getnum);
  1218. }
  1219. else if EQ("next") {
  1220. lua_next(L1, -2);
  1221. }
  1222. else if EQ("objsize") {
  1223. lua_pushinteger(L1, lua_rawlen(L1, getindex));
  1224. }
  1225. else if EQ("pcall") {
  1226. int narg = getnum;
  1227. int nres = getnum;
  1228. status = lua_pcall(L1, narg, nres, getnum);
  1229. }
  1230. else if EQ("pcallk") {
  1231. int narg = getnum;
  1232. int nres = getnum;
  1233. int i = getindex;
  1234. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  1235. }
  1236. else if EQ("pop") {
  1237. lua_pop(L1, getnum);
  1238. }
  1239. else if EQ("printstack") {
  1240. int n = getnum;
  1241. if (n != 0) {
  1242. printf("%s\n", luaL_tolstring(L1, n, NULL));
  1243. lua_pop(L1, 1);
  1244. }
  1245. else printstack(L1);
  1246. }
  1247. else if EQ("print") {
  1248. const char *msg = getstring;
  1249. printf("%s\n", msg);
  1250. }
  1251. else if EQ("warningC") {
  1252. const char *msg = getstring;
  1253. lua_warning(L1, msg, 1);
  1254. }
  1255. else if EQ("warning") {
  1256. const char *msg = getstring;
  1257. lua_warning(L1, msg, 0);
  1258. }
  1259. else if EQ("pushbool") {
  1260. lua_pushboolean(L1, getnum);
  1261. }
  1262. else if EQ("pushcclosure") {
  1263. lua_pushcclosure(L1, testC, getnum);
  1264. }
  1265. else if EQ("pushint") {
  1266. lua_pushinteger(L1, getnum);
  1267. }
  1268. else if EQ("pushnil") {
  1269. lua_pushnil(L1);
  1270. }
  1271. else if EQ("pushnum") {
  1272. lua_pushnumber(L1, (lua_Number)getnum);
  1273. }
  1274. else if EQ("pushstatus") {
  1275. pushcode(L1, status);
  1276. }
  1277. else if EQ("pushstring") {
  1278. lua_pushstring(L1, getstring);
  1279. }
  1280. else if EQ("pushupvalueindex") {
  1281. lua_pushinteger(L1, lua_upvalueindex(getnum));
  1282. }
  1283. else if EQ("pushvalue") {
  1284. lua_pushvalue(L1, getindex);
  1285. }
  1286. else if EQ("pushfstringI") {
  1287. lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1));
  1288. }
  1289. else if EQ("pushfstringS") {
  1290. lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1));
  1291. }
  1292. else if EQ("pushfstringP") {
  1293. lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
  1294. }
  1295. else if EQ("rawget") {
  1296. int t = getindex;
  1297. lua_rawget(L1, t);
  1298. }
  1299. else if EQ("rawgeti") {
  1300. int t = getindex;
  1301. lua_rawgeti(L1, t, getnum);
  1302. }
  1303. else if EQ("rawgetp") {
  1304. int t = getindex;
  1305. lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
  1306. }
  1307. else if EQ("rawset") {
  1308. int t = getindex;
  1309. lua_rawset(L1, t);
  1310. }
  1311. else if EQ("rawseti") {
  1312. int t = getindex;
  1313. lua_rawseti(L1, t, getnum);
  1314. }
  1315. else if EQ("rawsetp") {
  1316. int t = getindex;
  1317. lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
  1318. }
  1319. else if EQ("remove") {
  1320. lua_remove(L1, getnum);
  1321. }
  1322. else if EQ("replace") {
  1323. lua_replace(L1, getindex);
  1324. }
  1325. else if EQ("resume") {
  1326. int i = getindex;
  1327. int nres;
  1328. status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
  1329. }
  1330. else if EQ("return") {
  1331. int n = getnum;
  1332. if (L1 != L) {
  1333. int i;
  1334. for (i = 0; i < n; i++) {
  1335. int idx = -(n - i);
  1336. switch (lua_type(L1, idx)) {
  1337. case LUA_TBOOLEAN:
  1338. lua_pushboolean(L, lua_toboolean(L1, idx));
  1339. break;
  1340. default:
  1341. lua_pushstring(L, lua_tostring(L1, idx));
  1342. break;
  1343. }
  1344. }
  1345. }
  1346. return n;
  1347. }
  1348. else if EQ("rotate") {
  1349. int i = getindex;
  1350. lua_rotate(L1, i, getnum);
  1351. }
  1352. else if EQ("setfield") {
  1353. int t = getindex;
  1354. const char *s = getstring;
  1355. lua_setfield(L1, t, s);
  1356. }
  1357. else if EQ("seti") {
  1358. int t = getindex;
  1359. lua_seti(L1, t, getnum);
  1360. }
  1361. else if EQ("setglobal") {
  1362. const char *s = getstring;
  1363. lua_setglobal(L1, s);
  1364. }
  1365. else if EQ("sethook") {
  1366. int mask = getnum;
  1367. int count = getnum;
  1368. const char *s = getstring;
  1369. sethookaux(L1, mask, count, s);
  1370. }
  1371. else if EQ("setmetatable") {
  1372. int idx = getindex;
  1373. lua_setmetatable(L1, idx);
  1374. }
  1375. else if EQ("settable") {
  1376. lua_settable(L1, getindex);
  1377. }
  1378. else if EQ("settop") {
  1379. lua_settop(L1, getnum);
  1380. }
  1381. else if EQ("testudata") {
  1382. int i = getindex;
  1383. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1384. }
  1385. else if EQ("error") {
  1386. lua_error(L1);
  1387. }
  1388. else if EQ("abort") {
  1389. abort();
  1390. }
  1391. else if EQ("throw") {
  1392. #if defined(__cplusplus)
  1393. static struct X { int x; } x;
  1394. throw x;
  1395. #else
  1396. luaL_error(L1, "C++");
  1397. #endif
  1398. break;
  1399. }
  1400. else if EQ("tobool") {
  1401. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  1402. }
  1403. else if EQ("tocfunction") {
  1404. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  1405. }
  1406. else if EQ("tointeger") {
  1407. lua_pushinteger(L1, lua_tointeger(L1, getindex));
  1408. }
  1409. else if EQ("tonumber") {
  1410. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  1411. }
  1412. else if EQ("topointer") {
  1413. lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex)));
  1414. }
  1415. else if EQ("touserdata") {
  1416. lua_pushlightuserdata(L1, lua_touserdata(L1, getindex));
  1417. }
  1418. else if EQ("tostring") {
  1419. const char *s = lua_tostring(L1, getindex);
  1420. const char *s1 = lua_pushstring(L1, s);
  1421. lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
  1422. }
  1423. else if EQ("type") {
  1424. lua_pushstring(L1, luaL_typename(L1, getnum));
  1425. }
  1426. else if EQ("xmove") {
  1427. int f = getindex;
  1428. int t = getindex;
  1429. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  1430. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  1431. int n = getnum;
  1432. if (n == 0) n = lua_gettop(fs);
  1433. lua_xmove(fs, ts, n);
  1434. }
  1435. else if EQ("yield") {
  1436. return lua_yield(L1, getnum);
  1437. }
  1438. else if EQ("yieldk") {
  1439. int nres = getnum;
  1440. int i = getindex;
  1441. return lua_yieldk(L1, nres, i, Cfunck);
  1442. }
  1443. else if EQ("toclose") {
  1444. lua_toclose(L1, getnum);
  1445. }
  1446. else luaL_error(L, "unknown instruction %s", buff);
  1447. }
  1448. return 0;
  1449. }
  1450. static int testC (lua_State *L) {
  1451. lua_State *L1;
  1452. const char *pc;
  1453. if (lua_isuserdata(L, 1)) {
  1454. L1 = getstate(L);
  1455. pc = luaL_checkstring(L, 2);
  1456. }
  1457. else if (lua_isthread(L, 1)) {
  1458. L1 = lua_tothread(L, 1);
  1459. pc = luaL_checkstring(L, 2);
  1460. }
  1461. else {
  1462. L1 = L;
  1463. pc = luaL_checkstring(L, 1);
  1464. }
  1465. return runC(L, L1, pc);
  1466. }
  1467. static int Cfunc (lua_State *L) {
  1468. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1469. }
  1470. static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
  1471. pushcode(L, status);
  1472. lua_setglobal(L, "status");
  1473. lua_pushinteger(L, ctx);
  1474. lua_setglobal(L, "ctx");
  1475. return runC(L, L, lua_tostring(L, ctx));
  1476. }
  1477. static int makeCfunc (lua_State *L) {
  1478. luaL_checkstring(L, 1);
  1479. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1480. return 1;
  1481. }
  1482. /* }====================================================== */
  1483. /*
  1484. ** {======================================================
  1485. ** tests for C hooks
  1486. ** =======================================================
  1487. */
  1488. /*
  1489. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1490. */
  1491. static void Chook (lua_State *L, lua_Debug *ar) {
  1492. const char *scpt;
  1493. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1494. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1495. lua_pushlightuserdata(L, L);
  1496. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1497. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1498. lua_pop(L, 2); /* remove C_HOOK and script */
  1499. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1500. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1501. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1502. }
  1503. /*
  1504. ** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook
  1505. */
  1506. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1507. if (*scpt == '\0') { /* no script? */
  1508. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1509. return;
  1510. }
  1511. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1512. if (!lua_istable(L, -1)) { /* no hook table? */
  1513. lua_pop(L, 1); /* remove previous value */
  1514. lua_newtable(L); /* create new C_HOOK table */
  1515. lua_pushvalue(L, -1);
  1516. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1517. }
  1518. lua_pushlightuserdata(L, L);
  1519. lua_pushstring(L, scpt);
  1520. lua_settable(L, -3); /* C_HOOK[L] = script */
  1521. lua_sethook(L, Chook, mask, count);
  1522. }
  1523. static int sethook (lua_State *L) {
  1524. if (lua_isnoneornil(L, 1))
  1525. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1526. else {
  1527. const char *scpt = luaL_checkstring(L, 1);
  1528. const char *smask = luaL_checkstring(L, 2);
  1529. int count = cast_int(luaL_optinteger(L, 3, 0));
  1530. int mask = 0;
  1531. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1532. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1533. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1534. if (count > 0) mask |= LUA_MASKCOUNT;
  1535. sethookaux(L, mask, count, scpt);
  1536. }
  1537. return 0;
  1538. }
  1539. static int coresume (lua_State *L) {
  1540. int status, nres;
  1541. lua_State *co = lua_tothread(L, 1);
  1542. luaL_argcheck(L, co, 1, "coroutine expected");
  1543. status = lua_resume(co, L, 0, &nres);
  1544. if (status != LUA_OK && status != LUA_YIELD) {
  1545. lua_pushboolean(L, 0);
  1546. lua_insert(L, -2);
  1547. return 2; /* return false + error message */
  1548. }
  1549. else {
  1550. lua_pushboolean(L, 1);
  1551. return 1;
  1552. }
  1553. }
  1554. /* }====================================================== */
  1555. static const struct luaL_Reg tests_funcs[] = {
  1556. {"checkmemory", lua_checkmemory},
  1557. {"closestate", closestate},
  1558. {"d2s", d2s},
  1559. {"doonnewstack", doonnewstack},
  1560. {"doremote", doremote},
  1561. {"gccolor", gc_color},
  1562. {"gcage", gc_age},
  1563. {"gcstate", gc_state},
  1564. {"pobj", gc_printobj},
  1565. {"getref", getref},
  1566. {"hash", hash_query},
  1567. {"log2", log2_aux},
  1568. {"limits", get_limits},
  1569. {"listcode", listcode},
  1570. {"printcode", printcode},
  1571. {"listk", listk},
  1572. {"listabslineinfo", listabslineinfo},
  1573. {"listlocals", listlocals},
  1574. {"loadlib", loadlib},
  1575. {"checkpanic", checkpanic},
  1576. {"newstate", newstate},
  1577. {"newuserdata", newuserdata},
  1578. {"num2int", num2int},
  1579. {"pushuserdata", pushuserdata},
  1580. {"querystr", string_query},
  1581. {"querytab", table_query},
  1582. {"ref", tref},
  1583. {"resume", coresume},
  1584. {"s2d", s2d},
  1585. {"sethook", sethook},
  1586. {"stacklevel", stacklevel},
  1587. {"testC", testC},
  1588. {"makeCfunc", makeCfunc},
  1589. {"totalmem", mem_query},
  1590. {"alloccount", alloc_count},
  1591. {"trick", settrick},
  1592. {"udataval", udataval},
  1593. {"unref", unref},
  1594. {"upvalue", upvalue},
  1595. {NULL, NULL}
  1596. };
  1597. static void checkfinalmem (void) {
  1598. lua_assert(l_memcontrol.numblocks == 0);
  1599. lua_assert(l_memcontrol.total == 0);
  1600. }
  1601. int luaB_opentests (lua_State *L) {
  1602. void *ud;
  1603. lua_atpanic(L, &tpanic);
  1604. lua_setwarnf(L, &warnf, L);
  1605. atexit(checkfinalmem);
  1606. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  1607. lua_assert(ud == cast_voidp(&l_memcontrol));
  1608. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  1609. luaL_newlib(L, tests_funcs);
  1610. return 1;
  1611. }
  1612. #endif