ltests.c 48 KB

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