ltests.c 33 KB

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