ltests.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. ** $Id: ltests.c,v 2.86 2009/12/22 15:32:50 roberto Exp roberto $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define ltests_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "lauxlib.h"
  15. #include "lcode.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lfunc.h"
  20. #include "lmem.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "lualib.h"
  26. /*
  27. ** The whole module only makes sense with LUA_DEBUG on
  28. */
  29. #if defined(LUA_DEBUG)
  30. void *l_Trick = 0;
  31. int islocked = 0;
  32. #define obj_at(L,k) (L->ci->func + (k))
  33. static void setnameval (lua_State *L, const char *name, int val) {
  34. lua_pushstring(L, name);
  35. lua_pushinteger(L, val);
  36. lua_settable(L, -3);
  37. }
  38. static void pushobject (lua_State *L, const TValue *o) {
  39. setobj2s(L, L->top, o);
  40. api_incr_top(L);
  41. }
  42. static int tpanic (lua_State *L) {
  43. fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
  44. lua_tostring(L, -1));
  45. return (exit(EXIT_FAILURE), 0); /* do not return to Lua */
  46. }
  47. /*
  48. ** {======================================================================
  49. ** Controlled version for realloc.
  50. ** =======================================================================
  51. */
  52. #define MARK 0x55 /* 01010101 (a nice pattern) */
  53. #ifndef EXTERNMEMCHECK
  54. /* full memory check */
  55. #define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
  56. #define MARKSIZE 16 /* size of marks after each block */
  57. #define blockhead(b) (cast(char *, b) - HEADER)
  58. #define setsize(newblock, size) (*cast(size_t *, newblock) = size)
  59. #define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
  60. #define fillmem(mem,size) memset(mem, -MARK, size)
  61. #else
  62. /* external memory check: don't do it twice */
  63. #define HEADER 0
  64. #define MARKSIZE 0
  65. #define blockhead(b) (b)
  66. #define setsize(newblock, size) /* empty */
  67. #define checkblocksize(b,size) (1)
  68. #define fillmem(mem,size) /* empty */
  69. #endif
  70. Memcontrol l_memcontrol = {0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L}};
  71. static void *checkblock (void *block, size_t size) {
  72. void *b = blockhead(block);
  73. int i;
  74. for (i=0;i<MARKSIZE;i++)
  75. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  76. return b;
  77. }
  78. static void freeblock (Memcontrol *mc, void *block, size_t size) {
  79. if (block) {
  80. lua_assert(checkblocksize(block, size));
  81. block = checkblock(block, size);
  82. fillmem(block, size+HEADER+MARKSIZE); /* erase block */
  83. free(block); /* free original block */
  84. mc->numblocks--;
  85. mc->total -= size;
  86. }
  87. }
  88. void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
  89. Memcontrol *mc = cast(Memcontrol *, ud);
  90. if (block == NULL) {
  91. if (LUA_TSTRING <= oldsize && oldsize <= LUA_TTHREAD)
  92. mc->objcount[oldsize - LUA_TSTRING]++;
  93. oldsize = 0;
  94. }
  95. lua_assert((oldsize == 0) ? block == NULL :
  96. block && checkblocksize(block, oldsize));
  97. if (mc->memlimit == 0) { /* first time? */
  98. char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
  99. mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
  100. }
  101. if (size == 0) {
  102. freeblock(mc, block, oldsize);
  103. return NULL;
  104. }
  105. else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  106. return NULL; /* to test memory allocation errors */
  107. else {
  108. void *newblock;
  109. int i;
  110. size_t realsize = HEADER+size+MARKSIZE;
  111. size_t commonsize = (oldsize < size) ? oldsize : size;
  112. if (realsize < size) return NULL; /* overflow! */
  113. newblock = malloc(realsize); /* alloc a new block */
  114. if (newblock == NULL) return NULL;
  115. if (block) {
  116. memcpy(cast(char *, newblock)+HEADER, block, commonsize);
  117. freeblock(mc, block, oldsize); /* erase (and check) old copy */
  118. }
  119. /* initialize new part of the block with something `weird' */
  120. fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
  121. mc->total += size;
  122. if (mc->total > mc->maxmem)
  123. mc->maxmem = mc->total;
  124. mc->numblocks++;
  125. setsize(newblock, size);
  126. for (i=0;i<MARKSIZE;i++)
  127. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  128. return cast(char *, newblock)+HEADER;
  129. }
  130. }
  131. /* }====================================================================== */
  132. /*
  133. ** {======================================================
  134. ** Functions to check memory consistency
  135. ** =======================================================
  136. */
  137. #define issweep(g) (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  138. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  139. if (isdead(g,t)) return 0;
  140. if (g->gcstate == GCSpropagate)
  141. return !isblack(f) || !iswhite(t);
  142. else if (g->gcstate == GCSfinalize)
  143. return iswhite(f);
  144. else
  145. return 1;
  146. }
  147. static void printobj (global_State *g, GCObject *o) {
  148. int i = 0;
  149. GCObject *p;
  150. for (p = g->rootgc; p != o && p != NULL; p = gch(p)->next) i++;
  151. if (p == NULL) i = -1;
  152. printf("%d:%s(%p)-%c(%02X)", i, typename(gch(o)->tt), (void *)o,
  153. isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->marked);
  154. }
  155. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  156. int r = testobjref1(g,f,t);
  157. if (!r) {
  158. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  159. printobj(g, f);
  160. printf("\t-> ");
  161. printobj(g, t);
  162. printf("\n");
  163. }
  164. return r;
  165. }
  166. #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
  167. static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
  168. if (iscollectable(t)) {
  169. lua_assert(righttt(t) && testobjref(g, f, gcvalue(t)));
  170. }
  171. }
  172. static void checktable (global_State *g, Table *h) {
  173. int i;
  174. int weakkey = 0;
  175. int weakvalue = 0;
  176. const TValue *mode;
  177. GCObject *hgc = obj2gco(h);
  178. if (h->metatable)
  179. checkobjref(g, hgc, h->metatable);
  180. mode = gfasttm(g, h->metatable, TM_MODE);
  181. if (mode && ttisstring(mode)) { /* is there a weak mode? */
  182. weakkey = (strchr(svalue(mode), 'k') != NULL);
  183. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  184. }
  185. i = h->sizearray;
  186. while (i--)
  187. checkvalref(g, hgc, &h->array[i]);
  188. i = sizenode(h);
  189. while (i--) {
  190. Node *n = gnode(h, i);
  191. if (!ttisnil(gval(n))) {
  192. lua_assert(!ttisnil(gkey(n)));
  193. checkvalref(g, hgc, gkey(n));
  194. checkvalref(g, hgc, gval(n));
  195. }
  196. }
  197. }
  198. /*
  199. ** All marks are conditional because a GC may happen while the
  200. ** prototype is still being created
  201. */
  202. static void checkproto (global_State *g, Proto *f) {
  203. int i;
  204. GCObject *fgc = obj2gco(f);
  205. if (f->source) checkobjref(g, fgc, f->source);
  206. for (i=0; i<f->sizek; i++) {
  207. if (ttisstring(f->k+i))
  208. checkobjref(g, fgc, rawtsvalue(f->k+i));
  209. }
  210. for (i=0; i<f->sizeupvalues; i++) {
  211. if (f->upvalues[i].name)
  212. checkobjref(g, fgc, f->upvalues[i].name);
  213. }
  214. for (i=0; i<f->sizep; i++) {
  215. if (f->p[i])
  216. checkobjref(g, fgc, f->p[i]);
  217. }
  218. for (i=0; i<f->sizelocvars; i++) {
  219. if (f->locvars[i].varname)
  220. checkobjref(g, fgc, f->locvars[i].varname);
  221. }
  222. }
  223. static void checkclosure (global_State *g, Closure *cl) {
  224. GCObject *clgc = obj2gco(cl);
  225. checkobjref(g, clgc, cl->l.env);
  226. if (cl->c.isC) {
  227. int i;
  228. for (i=0; i<cl->c.nupvalues; i++)
  229. checkvalref(g, clgc, &cl->c.upvalue[i]);
  230. }
  231. else {
  232. int i;
  233. lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
  234. checkobjref(g, clgc, cl->l.p);
  235. for (i=0; i<cl->l.nupvalues; i++) {
  236. if (cl->l.upvals[i]) {
  237. lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
  238. checkobjref(g, clgc, cl->l.upvals[i]);
  239. }
  240. }
  241. }
  242. }
  243. static int lua_checkpc (pCallInfo ci) {
  244. if (!isLua(ci)) return 1;
  245. else {
  246. Proto *p = ci_func(ci)->l.p;
  247. return p->code <= ci->u.l.savedpc &&
  248. ci->u.l.savedpc <= p->code + p->sizecode;
  249. }
  250. }
  251. static void checkstack (global_State *g, lua_State *L1) {
  252. StkId o;
  253. CallInfo *ci;
  254. GCObject *uvo;
  255. lua_assert(!isdead(g, obj2gco(L1)));
  256. for (uvo = L1->openupval; uvo != NULL; uvo = gch(uvo)->next) {
  257. UpVal *uv = gco2uv(uvo);
  258. lua_assert(uv->v != &uv->u.value); /* must be open */
  259. lua_assert(!isblack(uvo)); /* open upvalues cannot be black */
  260. }
  261. for (ci = L1->ci; ci != NULL; ci = ci->previous) {
  262. lua_assert(ci->top <= L1->stack_last);
  263. lua_assert(lua_checkpc(ci));
  264. }
  265. if (L1->stack) {
  266. for (o = L1->stack; o < L1->top; o++)
  267. checkliveness(g, o);
  268. }
  269. else lua_assert(L1->stacksize == 0);
  270. }
  271. static void checkobject (global_State *g, GCObject *o) {
  272. if (isdead(g, o))
  273. lua_assert(issweep(g));
  274. else {
  275. if (g->gcstate == GCSfinalize)
  276. lua_assert(iswhite(o));
  277. switch (gch(o)->tt) {
  278. case LUA_TUPVAL: {
  279. UpVal *uv = gco2uv(o);
  280. lua_assert(uv->v == &uv->u.value); /* must be closed */
  281. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  282. checkvalref(g, o, uv->v);
  283. break;
  284. }
  285. case LUA_TUSERDATA: {
  286. Table *mt = gco2u(o)->metatable;
  287. if (mt) checkobjref(g, o, mt);
  288. break;
  289. }
  290. case LUA_TTABLE: {
  291. checktable(g, gco2t(o));
  292. break;
  293. }
  294. case LUA_TTHREAD: {
  295. checkstack(g, gco2th(o));
  296. break;
  297. }
  298. case LUA_TFUNCTION: {
  299. checkclosure(g, gco2cl(o));
  300. break;
  301. }
  302. case LUA_TPROTO: {
  303. checkproto(g, gco2p(o));
  304. break;
  305. }
  306. default: lua_assert(0);
  307. }
  308. }
  309. }
  310. int lua_checkmemory (lua_State *L) {
  311. global_State *g = G(L);
  312. GCObject *o;
  313. UpVal *uv;
  314. checkliveness(g, &g->l_registry);
  315. lua_assert(!isdead(g, obj2gco(g->l_gt)));
  316. checkstack(g, g->mainthread);
  317. for (o = g->rootgc; o != obj2gco(g->mainthread); o = gch(o)->next) {
  318. lua_assert(!testbits(o->gch.marked, bit2mask(SEPARATED, SFIXEDBIT)));
  319. checkobject(g, o);
  320. }
  321. lua_assert(testbit(o->gch.marked, SFIXEDBIT));
  322. for (o = gch(o)->next; o != NULL; o = gch(o)->next) {
  323. lua_assert(gch(o)->tt == LUA_TUSERDATA &&
  324. !isdead(g, o) &&
  325. testbit(o->gch.marked, SEPARATED));
  326. checkobject(g, o);
  327. }
  328. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  329. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  330. lua_assert(uv->v != &uv->u.value); /* must be open */
  331. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  332. checkvalref(g, obj2gco(uv), uv->v);
  333. }
  334. return 0;
  335. }
  336. /* }====================================================== */
  337. /*
  338. ** {======================================================
  339. ** Disassembler
  340. ** =======================================================
  341. */
  342. static char *buildop (Proto *p, int pc, char *buff) {
  343. Instruction i = p->code[pc];
  344. OpCode o = GET_OPCODE(i);
  345. const char *name = luaP_opnames[o];
  346. int line = getfuncline(p, pc);
  347. sprintf(buff, "(%4d) %4d - ", line, pc);
  348. switch (getOpMode(o)) {
  349. case iABC:
  350. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  351. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  352. break;
  353. case iABx:
  354. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  355. break;
  356. case iAsBx:
  357. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  358. break;
  359. case iAx:
  360. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  361. break;
  362. }
  363. return buff;
  364. }
  365. #if 0
  366. void luaI_printcode (Proto *pt, int size) {
  367. int pc;
  368. for (pc=0; pc<size; pc++) {
  369. char buff[100];
  370. printf("%s\n", buildop(pt, pc, buff));
  371. }
  372. printf("-------\n");
  373. }
  374. #endif
  375. static int listcode (lua_State *L) {
  376. int pc;
  377. Proto *p;
  378. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  379. 1, "Lua function expected");
  380. p = getproto(obj_at(L, 1));
  381. lua_newtable(L);
  382. setnameval(L, "maxstack", p->maxstacksize);
  383. setnameval(L, "numparams", p->numparams);
  384. for (pc=0; pc<p->sizecode; pc++) {
  385. char buff[100];
  386. lua_pushinteger(L, pc+1);
  387. lua_pushstring(L, buildop(p, pc, buff));
  388. lua_settable(L, -3);
  389. }
  390. return 1;
  391. }
  392. static int listk (lua_State *L) {
  393. Proto *p;
  394. int i;
  395. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  396. 1, "Lua function expected");
  397. p = getproto(obj_at(L, 1));
  398. lua_createtable(L, p->sizek, 0);
  399. for (i=0; i<p->sizek; i++) {
  400. pushobject(L, p->k+i);
  401. lua_rawseti(L, -2, i+1);
  402. }
  403. return 1;
  404. }
  405. static int listlocals (lua_State *L) {
  406. Proto *p;
  407. int pc = luaL_checkint(L, 2) - 1;
  408. int i = 0;
  409. const char *name;
  410. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  411. 1, "Lua function expected");
  412. p = getproto(obj_at(L, 1));
  413. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  414. lua_pushstring(L, name);
  415. return i-1;
  416. }
  417. /* }====================================================== */
  418. static int get_limits (lua_State *L) {
  419. lua_createtable(L, 0, 5);
  420. setnameval(L, "BITS_INT", LUAI_BITSINT);
  421. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  422. setnameval(L, "MAXSTACK", MAXSTACK);
  423. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  424. return 1;
  425. }
  426. static int mem_query (lua_State *L) {
  427. if (lua_isnone(L, 1)) {
  428. int i;
  429. lua_pushinteger(L, l_memcontrol.total);
  430. lua_pushinteger(L, l_memcontrol.numblocks);
  431. lua_pushinteger(L, l_memcontrol.maxmem);
  432. for (i = 0; i < 5; i++) lua_pushinteger(L, l_memcontrol.objcount[i]);
  433. return 3 + 5;
  434. }
  435. else {
  436. l_memcontrol.memlimit = luaL_checkint(L, 1);
  437. return 0;
  438. }
  439. }
  440. static int settrick (lua_State *L) {
  441. l_Trick = gcvalue(obj_at(L, 1));
  442. return 0;
  443. }
  444. /*static int set_gcstate (lua_State *L) {
  445. static const char *const state[] = {"propagate", "sweep", "finalize"};
  446. return 0;
  447. }*/
  448. static int get_gccolor (lua_State *L) {
  449. TValue *o;
  450. luaL_checkany(L, 1);
  451. o = obj_at(L, 1);
  452. if (!iscollectable(o))
  453. lua_pushstring(L, "no collectable");
  454. else
  455. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  456. isblack(gcvalue(o)) ? "black" : "grey");
  457. return 1;
  458. }
  459. static int gcstate (lua_State *L) {
  460. switch(G(L)->gcstate) {
  461. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  462. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  463. case GCSsweep: lua_pushstring(L, "sweep"); break;
  464. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  465. default: lua_assert(0);
  466. }
  467. return 1;
  468. }
  469. static int hash_query (lua_State *L) {
  470. if (lua_isnone(L, 2)) {
  471. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  472. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  473. }
  474. else {
  475. TValue *o = obj_at(L, 1);
  476. Table *t;
  477. luaL_checktype(L, 2, LUA_TTABLE);
  478. t = hvalue(obj_at(L, 2));
  479. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  480. }
  481. return 1;
  482. }
  483. static int stacklevel (lua_State *L) {
  484. unsigned long a = 0;
  485. lua_pushinteger(L, (L->top - L->stack));
  486. lua_pushinteger(L, (L->stack_last - L->stack));
  487. lua_pushinteger(L, (unsigned long)&a);
  488. return 5;
  489. }
  490. static int table_query (lua_State *L) {
  491. const Table *t;
  492. int i = luaL_optint(L, 2, -1);
  493. luaL_checktype(L, 1, LUA_TTABLE);
  494. t = hvalue(obj_at(L, 1));
  495. if (i == -1) {
  496. lua_pushinteger(L, t->sizearray);
  497. lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
  498. lua_pushinteger(L, t->lastfree - t->node);
  499. }
  500. else if (i < t->sizearray) {
  501. lua_pushinteger(L, i);
  502. pushobject(L, &t->array[i]);
  503. lua_pushnil(L);
  504. }
  505. else if ((i -= t->sizearray) < sizenode(t)) {
  506. if (!ttisnil(gval(gnode(t, i))) ||
  507. ttisnil(gkey(gnode(t, i))) ||
  508. ttisnumber(gkey(gnode(t, i)))) {
  509. pushobject(L, key2tval(gnode(t, i)));
  510. }
  511. else
  512. lua_pushliteral(L, "<undef>");
  513. pushobject(L, gval(gnode(t, i)));
  514. if (gnext(&t->node[i]))
  515. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  516. else
  517. lua_pushnil(L);
  518. }
  519. return 3;
  520. }
  521. static int string_query (lua_State *L) {
  522. stringtable *tb = &G(L)->strt;
  523. int s = luaL_optint(L, 2, 0) - 1;
  524. if (s==-1) {
  525. lua_pushinteger(L ,tb->nuse);
  526. lua_pushinteger(L ,tb->size);
  527. return 2;
  528. }
  529. else if (s < tb->size) {
  530. GCObject *ts;
  531. int n = 0;
  532. for (ts = tb->hash[s]; ts; ts = gch(ts)->next) {
  533. setsvalue2s(L, L->top, gco2ts(ts));
  534. incr_top(L);
  535. n++;
  536. }
  537. return n;
  538. }
  539. return 0;
  540. }
  541. static int tref (lua_State *L) {
  542. int level = lua_gettop(L);
  543. int lock = luaL_optint(L, 2, 1);
  544. luaL_checkany(L, 1);
  545. lua_pushvalue(L, 1);
  546. lua_pushinteger(L, lua_ref(L, lock));
  547. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  548. return 1;
  549. }
  550. static int getref (lua_State *L) {
  551. int level = lua_gettop(L);
  552. lua_getref(L, luaL_checkint(L, 1));
  553. lua_assert(lua_gettop(L) == level+1);
  554. return 1;
  555. }
  556. static int unref (lua_State *L) {
  557. int level = lua_gettop(L);
  558. lua_unref(L, luaL_checkint(L, 1));
  559. lua_assert(lua_gettop(L) == level);
  560. return 0;
  561. }
  562. static int upvalue (lua_State *L) {
  563. int n = luaL_checkint(L, 2);
  564. luaL_checktype(L, 1, LUA_TFUNCTION);
  565. if (lua_isnone(L, 3)) {
  566. const char *name = lua_getupvalue(L, 1, n);
  567. if (name == NULL) return 0;
  568. lua_pushstring(L, name);
  569. return 2;
  570. }
  571. else {
  572. const char *name = lua_setupvalue(L, 1, n);
  573. lua_pushstring(L, name);
  574. return 1;
  575. }
  576. }
  577. static int newuserdata (lua_State *L) {
  578. size_t size = luaL_checkint(L, 1);
  579. char *p = cast(char *, lua_newuserdata(L, size));
  580. while (size--) *p++ = '\0';
  581. return 1;
  582. }
  583. static int pushuserdata (lua_State *L) {
  584. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  585. return 1;
  586. }
  587. static int udataval (lua_State *L) {
  588. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  589. return 1;
  590. }
  591. static int doonnewstack (lua_State *L) {
  592. lua_State *L1 = lua_newthread(L);
  593. size_t l;
  594. const char *s = luaL_checklstring(L, 1, &l);
  595. int status = luaL_loadbuffer(L1, s, l, s);
  596. if (status == LUA_OK)
  597. status = lua_pcall(L1, 0, 0, 0);
  598. lua_pushinteger(L, status);
  599. return 1;
  600. }
  601. static int s2d (lua_State *L) {
  602. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  603. return 1;
  604. }
  605. static int d2s (lua_State *L) {
  606. double d = luaL_checknumber(L, 1);
  607. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  608. return 1;
  609. }
  610. static int num2int (lua_State *L) {
  611. lua_pushinteger(L, lua_tointeger(L, 1));
  612. return 1;
  613. }
  614. static int newstate (lua_State *L) {
  615. void *ud;
  616. lua_Alloc f = lua_getallocf(L, &ud);
  617. lua_State *L1 = lua_newstate(f, ud);
  618. if (L1) {
  619. lua_atpanic(L1, tpanic);
  620. lua_pushlightuserdata(L, L1);
  621. }
  622. else
  623. lua_pushnil(L);
  624. return 1;
  625. }
  626. static lua_State *getstate (lua_State *L) {
  627. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  628. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  629. return L1;
  630. }
  631. static int loadlib (lua_State *L) {
  632. static const luaL_Reg libs[] = {
  633. {"baselibopen", luaopen_base},
  634. {"dblibopen", luaopen_debug},
  635. {"iolibopen", luaopen_io},
  636. {"mathlibopen", luaopen_math},
  637. {"strlibopen", luaopen_string},
  638. {"tablibopen", luaopen_table},
  639. {"packageopen", luaopen_package},
  640. {NULL, NULL}
  641. };
  642. lua_State *L1 = getstate(L);
  643. lua_pushglobaltable(L1);
  644. luaL_register(L1, NULL, libs);
  645. return 0;
  646. }
  647. static int closestate (lua_State *L) {
  648. lua_State *L1 = getstate(L);
  649. lua_close(L1);
  650. return 0;
  651. }
  652. static int doremote (lua_State *L) {
  653. lua_State *L1 = getstate(L);
  654. size_t lcode;
  655. const char *code = luaL_checklstring(L, 2, &lcode);
  656. int status;
  657. lua_settop(L1, 0);
  658. status = luaL_loadbuffer(L1, code, lcode, code);
  659. if (status == LUA_OK)
  660. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  661. if (status != LUA_OK) {
  662. lua_pushnil(L);
  663. lua_pushinteger(L, status);
  664. lua_pushstring(L, lua_tostring(L1, -1));
  665. return 3;
  666. }
  667. else {
  668. int i = 0;
  669. while (!lua_isnone(L1, ++i))
  670. lua_pushstring(L, lua_tostring(L1, i));
  671. lua_pop(L1, i-1);
  672. return i-1;
  673. }
  674. }
  675. static int int2fb_aux (lua_State *L) {
  676. int b = luaO_int2fb(luaL_checkint(L, 1));
  677. lua_pushinteger(L, b);
  678. lua_pushinteger(L, luaO_fb2int(b));
  679. return 2;
  680. }
  681. /*
  682. ** {======================================================
  683. ** function to test the API with C. It interprets a kind of assembler
  684. ** language with calls to the API, so the test can be driven by Lua code
  685. ** =======================================================
  686. */
  687. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  688. static const char *const delimits = " \t\n,;";
  689. static void skip (const char **pc) {
  690. for (;;) {
  691. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  692. else if (**pc == '#') {
  693. while (**pc != '\n' && **pc != '\0') (*pc)++;
  694. }
  695. else break;
  696. }
  697. }
  698. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  699. int res = 0;
  700. int sig = 1;
  701. skip(pc);
  702. if (**pc == '.') {
  703. res = lua_tointeger(L1, -1);
  704. lua_pop(L1, 1);
  705. (*pc)++;
  706. return res;
  707. }
  708. else if (**pc == '-') {
  709. sig = -1;
  710. (*pc)++;
  711. }
  712. if (!lisdigit(cast(unsigned char, **pc)))
  713. luaL_error(L, "number expected (%s)", *pc);
  714. while (lisdigit(cast(unsigned char, **pc))) res = res*10 + (*(*pc)++) - '0';
  715. return sig*res;
  716. }
  717. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  718. int i = 0;
  719. skip(pc);
  720. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  721. int quote = *(*pc)++;
  722. while (**pc != quote) {
  723. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  724. buff[i++] = *(*pc)++;
  725. }
  726. (*pc)++;
  727. }
  728. else {
  729. while (**pc != '\0' && !strchr(delimits, **pc))
  730. buff[i++] = *(*pc)++;
  731. }
  732. buff[i] = '\0';
  733. return buff;
  734. }
  735. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  736. skip(pc);
  737. switch (*(*pc)++) {
  738. case 'R': return LUA_REGISTRYINDEX;
  739. case 'G': return luaL_error(L, "deprecated index 'G'");
  740. case 'E': return LUA_ENVIRONINDEX;
  741. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  742. default: (*pc)--; return getnum_aux(L, L1, pc);
  743. }
  744. }
  745. static void pushcode (lua_State *L, int code) {
  746. static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
  747. "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
  748. lua_pushstring(L, codes[code]);
  749. }
  750. #define EQ(s1) (strcmp(s1, inst) == 0)
  751. #define getnum (getnum_aux(L, L1, &pc))
  752. #define getstring (getstring_aux(L, buff, &pc))
  753. #define getindex (getindex_aux(L, L1, &pc))
  754. static int testC (lua_State *L);
  755. static int Cfunck (lua_State *L);
  756. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  757. char buff[300];
  758. int status = 0;
  759. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  760. for (;;) {
  761. const char *inst = getstring;
  762. if EQ("") return 0;
  763. else if EQ("isnumber") {
  764. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  765. }
  766. else if EQ("isstring") {
  767. lua_pushboolean(L1, lua_isstring(L1, getindex));
  768. }
  769. else if EQ("istable") {
  770. lua_pushboolean(L1, lua_istable(L1, getindex));
  771. }
  772. else if EQ("iscfunction") {
  773. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  774. }
  775. else if EQ("isfunction") {
  776. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  777. }
  778. else if EQ("isuserdata") {
  779. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  780. }
  781. else if EQ("isudataval") {
  782. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  783. }
  784. else if EQ("isnil") {
  785. lua_pushboolean(L1, lua_isnil(L1, getindex));
  786. }
  787. else if EQ("isnull") {
  788. lua_pushboolean(L1, lua_isnone(L1, getindex));
  789. }
  790. else if EQ("tonumber") {
  791. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  792. }
  793. else if EQ("tostring") {
  794. const char *s = lua_tostring(L1, getindex);
  795. const char *s1 = lua_pushstring(L1, s);
  796. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  797. }
  798. else if EQ("objsize") {
  799. lua_pushinteger(L1, lua_rawlen(L1, getindex));
  800. }
  801. else if EQ("len") {
  802. lua_len(L1, getindex);
  803. }
  804. else if EQ("Llen") {
  805. lua_pushinteger(L1, luaL_len(L1, getindex));
  806. }
  807. else if EQ("tocfunction") {
  808. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  809. }
  810. else if EQ("func2udata") {
  811. lua_CFunction func = lua_tocfunction(L1, getindex);
  812. lua_pushlightuserdata(L1, &func);
  813. }
  814. else if EQ("return") {
  815. int n = getnum;
  816. if (L1 != L) {
  817. int i;
  818. for (i = 0; i < n; i++)
  819. lua_pushstring(L, lua_tostring(L1, -(n - i)));
  820. }
  821. return n;
  822. }
  823. else if EQ("gettop") {
  824. lua_pushinteger(L1, lua_gettop(L1));
  825. }
  826. else if EQ("settop") {
  827. lua_settop(L1, getnum);
  828. }
  829. else if EQ("pop") {
  830. lua_pop(L1, getnum);
  831. }
  832. else if EQ("pushnum") {
  833. lua_pushinteger(L1, getnum);
  834. }
  835. else if EQ("pushstring") {
  836. lua_pushstring(L1, getstring);
  837. }
  838. else if EQ("pushnil") {
  839. lua_pushnil(L1);
  840. }
  841. else if EQ("pushbool") {
  842. lua_pushboolean(L1, getnum);
  843. }
  844. else if EQ("newtable") {
  845. lua_newtable(L1);
  846. }
  847. else if EQ("newuserdata") {
  848. lua_newuserdata(L1, getnum);
  849. }
  850. else if EQ("tobool") {
  851. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  852. }
  853. else if EQ("pushvalue") {
  854. lua_pushvalue(L1, getindex);
  855. }
  856. else if EQ("pushcclosure") {
  857. lua_pushcclosure(L1, testC, getnum);
  858. }
  859. else if EQ("pushupvalueindex") {
  860. lua_pushinteger(L1, lua_upvalueindex(getnum));
  861. }
  862. else if EQ("remove") {
  863. lua_remove(L1, getnum);
  864. }
  865. else if EQ("insert") {
  866. lua_insert(L1, getnum);
  867. }
  868. else if EQ("replace") {
  869. lua_replace(L1, getindex);
  870. }
  871. else if EQ("copy") {
  872. int f = getindex;
  873. lua_copy(L1, f, getindex);
  874. }
  875. else if EQ("gettable") {
  876. lua_gettable(L1, getindex);
  877. }
  878. else if EQ("getfield") {
  879. int t = getindex;
  880. lua_getfield(L1, t, getstring);
  881. }
  882. else if EQ("setfield") {
  883. int t = getindex;
  884. lua_setfield(L1, t, getstring);
  885. }
  886. else if EQ("rawgeti") {
  887. int t = getindex;
  888. lua_rawgeti(L1, t, getnum);
  889. }
  890. else if EQ("settable") {
  891. lua_settable(L1, getindex);
  892. }
  893. else if EQ("next") {
  894. lua_next(L1, -2);
  895. }
  896. else if EQ("concat") {
  897. lua_concat(L1, getnum);
  898. }
  899. else if EQ("print") {
  900. int n = getnum;
  901. if (n != 0) {
  902. printf("%s\n", luaL_tolstring(L1, n, NULL));
  903. lua_pop(L1, 1);
  904. }
  905. else {
  906. int i;
  907. n = lua_gettop(L1);
  908. for (i = 1; i <= n; i++) {
  909. printf("%s ", luaL_tolstring(L1, i, NULL));
  910. lua_pop(L1, 1);
  911. }
  912. printf("\n");
  913. }
  914. }
  915. else if EQ("arith") {
  916. static char ops[] = "+-*/%^_";
  917. int op;
  918. skip(&pc);
  919. op = strchr(ops, *pc++) - ops;
  920. lua_arith(L1, op);
  921. }
  922. else if EQ("compare") {
  923. int a = getindex;
  924. int b = getindex;
  925. lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
  926. }
  927. else if EQ("call") {
  928. int narg = getnum;
  929. int nres = getnum;
  930. lua_call(L1, narg, nres);
  931. }
  932. else if EQ("pcall") {
  933. int narg = getnum;
  934. int nres = getnum;
  935. status = lua_pcall(L1, narg, nres, 0);
  936. }
  937. else if EQ("pcallk") {
  938. int narg = getnum;
  939. int nres = getnum;
  940. int i = getindex;
  941. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  942. }
  943. else if EQ("callk") {
  944. int narg = getnum;
  945. int nres = getnum;
  946. int i = getindex;
  947. lua_callk(L1, narg, nres, i, Cfunck);
  948. }
  949. else if EQ("yield") {
  950. return lua_yield(L1, getnum);
  951. }
  952. else if EQ("yieldk") {
  953. int nres = getnum;
  954. int i = getindex;
  955. return lua_yieldk(L1, nres, i, Cfunck);
  956. }
  957. else if EQ("newthread") {
  958. lua_newthread(L1);
  959. }
  960. else if EQ("resume") {
  961. int i = getindex;
  962. status = lua_resume(lua_tothread(L1, i), getnum);
  963. }
  964. else if EQ("pushstatus") {
  965. pushcode(L1, status);
  966. }
  967. else if EQ("xmove") {
  968. int f = getindex;
  969. int t = getindex;
  970. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  971. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  972. int n = getnum;
  973. if (n == 0) n = lua_gettop(fs);
  974. lua_xmove(fs, ts, n);
  975. }
  976. else if EQ("loadstring") {
  977. size_t sl;
  978. const char *s = luaL_checklstring(L1, getnum, &sl);
  979. luaL_loadbuffer(L1, s, sl, s);
  980. }
  981. else if EQ("loadfile") {
  982. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  983. }
  984. else if EQ("setmetatable") {
  985. lua_setmetatable(L1, getindex);
  986. }
  987. else if EQ("getmetatable") {
  988. if (lua_getmetatable(L1, getindex) == 0)
  989. lua_pushnil(L1);
  990. }
  991. else if EQ("type") {
  992. lua_pushstring(L1, luaL_typename(L1, getnum));
  993. }
  994. else if EQ("append") {
  995. int t = getindex;
  996. int i = lua_rawlen(L1, t);
  997. lua_rawseti(L1, t, i + 1);
  998. }
  999. else if EQ("getctx") {
  1000. int i = 0;
  1001. int s = lua_getctx(L1, &i);
  1002. pushcode(L1, s);
  1003. lua_pushinteger(L1, i);
  1004. }
  1005. else if EQ("checkstack") {
  1006. if (!lua_checkstack(L1, getnum))
  1007. luaL_error(L, "C stack overflow");
  1008. }
  1009. else if EQ("newmetatable") {
  1010. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1011. }
  1012. else if EQ("testudata") {
  1013. int i = getindex;
  1014. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1015. }
  1016. else if EQ("gsub") {
  1017. int a = getnum; int b = getnum; int c = getnum;
  1018. luaL_gsub(L1, lua_tostring(L1, a),
  1019. lua_tostring(L1, b),
  1020. lua_tostring(L1, c));
  1021. }
  1022. else if EQ("sethook") {
  1023. int mask = getnum;
  1024. int count = getnum;
  1025. sethookaux(L1, mask, count, getstring);
  1026. }
  1027. else if EQ("throw") {
  1028. #if defined(__cplusplus)
  1029. static struct X { int x; } x;
  1030. throw x;
  1031. #else
  1032. luaL_error(L1, "C++");
  1033. #endif
  1034. break;
  1035. }
  1036. else luaL_error(L, "unknown instruction %s", buff);
  1037. }
  1038. return 0;
  1039. }
  1040. static int testC (lua_State *L) {
  1041. lua_State *L1;
  1042. const char *pc;
  1043. if (lua_isuserdata(L, 1)) {
  1044. L1 = getstate(L);
  1045. pc = luaL_checkstring(L, 2);
  1046. }
  1047. else if (lua_isthread(L, 1)) {
  1048. L1 = lua_tothread(L, 1);
  1049. pc = luaL_checkstring(L, 2);
  1050. }
  1051. else {
  1052. L1 = L;
  1053. pc = luaL_checkstring(L, 1);
  1054. }
  1055. return runC(L, L1, pc);
  1056. }
  1057. static int Cfunc (lua_State *L) {
  1058. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1059. }
  1060. static int Cfunck (lua_State *L) {
  1061. int i = 0;
  1062. lua_getctx(L, &i);
  1063. return runC(L, L, lua_tostring(L, i));
  1064. }
  1065. static int makeCfunc (lua_State *L) {
  1066. luaL_checkstring(L, 1);
  1067. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1068. return 1;
  1069. }
  1070. /* }====================================================== */
  1071. /*
  1072. ** {======================================================
  1073. ** tests for C hooks
  1074. ** =======================================================
  1075. */
  1076. /*
  1077. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1078. */
  1079. static void Chook (lua_State *L, lua_Debug *ar) {
  1080. const char *scpt;
  1081. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1082. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1083. lua_pushlightuserdata(L, L);
  1084. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1085. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1086. lua_pop(L, 2); /* remove C_HOOK and script */
  1087. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1088. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1089. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1090. }
  1091. /*
  1092. ** sets registry.C_HOOK[L] = scpt and sets Chook as a hook
  1093. */
  1094. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1095. if (*scpt == '\0') { /* no script? */
  1096. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1097. return;
  1098. }
  1099. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1100. if (!lua_istable(L, -1)) { /* no hook table? */
  1101. lua_pop(L, 1); /* remove previous value */
  1102. lua_newtable(L); /* create new C_HOOK table */
  1103. lua_pushvalue(L, -1);
  1104. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1105. }
  1106. lua_pushlightuserdata(L, L);
  1107. lua_pushstring(L, scpt);
  1108. lua_settable(L, -3); /* C_HOOK[L] = script */
  1109. lua_sethook(L, Chook, mask, count);
  1110. }
  1111. static int sethook (lua_State *L) {
  1112. if (lua_isnoneornil(L, 1))
  1113. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1114. else {
  1115. const char *scpt = luaL_checkstring(L, 1);
  1116. const char *smask = luaL_checkstring(L, 2);
  1117. int count = luaL_optint(L, 3, 0);
  1118. int mask = 0;
  1119. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1120. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1121. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1122. if (count > 0) mask |= LUA_MASKCOUNT;
  1123. sethookaux(L, mask, count, scpt);
  1124. }
  1125. return 0;
  1126. }
  1127. static int coresume (lua_State *L) {
  1128. int status;
  1129. lua_State *co = lua_tothread(L, 1);
  1130. luaL_argcheck(L, co, 1, "coroutine expected");
  1131. status = lua_resume(co, 0);
  1132. if (status != LUA_OK && status != LUA_YIELD) {
  1133. lua_pushboolean(L, 0);
  1134. lua_insert(L, -2);
  1135. return 2; /* return false + error message */
  1136. }
  1137. else {
  1138. lua_pushboolean(L, 1);
  1139. return 1;
  1140. }
  1141. }
  1142. /* }====================================================== */
  1143. static const struct luaL_Reg tests_funcs[] = {
  1144. {"checkmemory", lua_checkmemory},
  1145. {"closestate", closestate},
  1146. {"d2s", d2s},
  1147. {"doonnewstack", doonnewstack},
  1148. {"doremote", doremote},
  1149. {"gccolor", get_gccolor},
  1150. {"gcstate", gcstate},
  1151. {"getref", getref},
  1152. {"hash", hash_query},
  1153. {"int2fb", int2fb_aux},
  1154. {"limits", get_limits},
  1155. {"listcode", listcode},
  1156. {"listk", listk},
  1157. {"listlocals", listlocals},
  1158. {"loadlib", loadlib},
  1159. {"newstate", newstate},
  1160. {"newuserdata", newuserdata},
  1161. {"num2int", num2int},
  1162. {"pushuserdata", pushuserdata},
  1163. {"querystr", string_query},
  1164. {"querytab", table_query},
  1165. {"ref", tref},
  1166. {"resume", coresume},
  1167. {"s2d", s2d},
  1168. {"sethook", sethook},
  1169. {"stacklevel", stacklevel},
  1170. {"testC", testC},
  1171. {"makeCfunc", makeCfunc},
  1172. {"totalmem", mem_query},
  1173. {"trick", settrick},
  1174. {"udataval", udataval},
  1175. {"unref", unref},
  1176. {"upvalue", upvalue},
  1177. {NULL, NULL}
  1178. };
  1179. static void checkfinalmem (void) {
  1180. lua_assert(l_memcontrol.numblocks == 0);
  1181. lua_assert(l_memcontrol.total == 0);
  1182. }
  1183. int luaB_opentests (lua_State *L) {
  1184. void *ud;
  1185. lua_atpanic(L, &tpanic);
  1186. atexit(checkfinalmem);
  1187. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  1188. lua_assert(ud == cast(void *, &l_memcontrol));
  1189. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  1190. luaL_register(L, "T", tests_funcs);
  1191. return 0;
  1192. }
  1193. #endif