ltests.c 48 KB

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