ltests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. ** $Id: ltests.c,v 1.121 2002/05/13 13:10:04 roberto Exp roberto $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "lauxlib.h"
  14. #include "lcode.h"
  15. #include "ldebug.h"
  16. #include "ldo.h"
  17. #include "lfunc.h"
  18. #include "lmem.h"
  19. #include "lopcodes.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "luadebug.h"
  24. #include "lualib.h"
  25. /*
  26. ** The whole module only makes sense with LUA_DEBUG on
  27. */
  28. #ifdef LUA_DEBUG
  29. static lua_State *lua_state = NULL;
  30. int islocked = 0;
  31. #define index(L,k) (L->ci->base+(k) - 1)
  32. static void setnameval (lua_State *L, const char *name, int val) {
  33. lua_pushstring(L, name);
  34. lua_pushnumber(L, val);
  35. lua_settable(L, -3);
  36. }
  37. /*
  38. ** {======================================================================
  39. ** Controlled version for realloc.
  40. ** =======================================================================
  41. */
  42. /* ensures maximum alignment for HEADER */
  43. #define HEADER (sizeof(union L_Umaxalign))
  44. #define MARKSIZE 32
  45. #define MARK 0x55 /* 01010101 (a nice pattern) */
  46. #define blocksize(b) (cast(size_t *, b) - HEADER/sizeof(size_t))
  47. unsigned long memdebug_numblocks = 0;
  48. unsigned long memdebug_total = 0;
  49. unsigned long memdebug_maxmem = 0;
  50. unsigned long memdebug_memlimit = ULONG_MAX;
  51. static void *checkblock (void *block) {
  52. size_t *b = blocksize(block);
  53. size_t size = *b;
  54. int i;
  55. for (i=0;i<MARKSIZE;i++)
  56. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  57. return b;
  58. }
  59. static void freeblock (void *block) {
  60. if (block) {
  61. size_t size = *blocksize(block);
  62. block = checkblock(block);
  63. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  64. free(block); /* free original block */
  65. memdebug_numblocks--;
  66. memdebug_total -= size;
  67. }
  68. }
  69. void *debug_realloc (void *block, size_t oldsize, size_t size) {
  70. lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  71. if (size == 0) {
  72. freeblock(block);
  73. return NULL;
  74. }
  75. else if (memdebug_total+size-oldsize > memdebug_memlimit)
  76. return NULL; /* to test memory allocation errors */
  77. else {
  78. void *newblock;
  79. int i;
  80. size_t realsize = HEADER+size+MARKSIZE;
  81. if (realsize < size) return NULL; /* overflow! */
  82. newblock = malloc(realsize); /* alloc a new block */
  83. if (newblock == NULL) return NULL;
  84. if (oldsize > size) oldsize = size;
  85. if (block) {
  86. memcpy(cast(char *, newblock)+HEADER, block, oldsize);
  87. freeblock(block); /* erase (and check) old copy */
  88. }
  89. /* initialize new part of the block with something `weird' */
  90. memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
  91. memdebug_total += size;
  92. if (memdebug_total > memdebug_maxmem)
  93. memdebug_maxmem = memdebug_total;
  94. memdebug_numblocks++;
  95. *cast(size_t *, newblock) = size;
  96. for (i=0;i<MARKSIZE;i++)
  97. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  98. return cast(char *, newblock)+HEADER;
  99. }
  100. }
  101. /* }====================================================================== */
  102. /*
  103. ** {======================================================
  104. ** Disassembler
  105. ** =======================================================
  106. */
  107. static char *buildop (Proto *p, int pc, char *buff) {
  108. Instruction i = p->code[pc];
  109. OpCode o = GET_OPCODE(i);
  110. const char *name = luaP_opnames[o];
  111. int line = getline(p, pc);
  112. sprintf(buff, "(%4d) %4d - ", line, pc);
  113. switch (getOpMode(o)) {
  114. case iABC:
  115. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  116. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  117. break;
  118. case iABx:
  119. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  120. break;
  121. case iAsBx:
  122. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  123. break;
  124. }
  125. return buff;
  126. }
  127. #if 0
  128. void luaI_printcode (Proto *pt, int size) {
  129. int pc;
  130. for (pc=0; pc<size; pc++) {
  131. char buff[100];
  132. printf("%s\n", buildop(pt, pc, buff));
  133. }
  134. printf("-------\n");
  135. }
  136. #endif
  137. static int listcode (lua_State *L) {
  138. int pc;
  139. Proto *p;
  140. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  141. 1, "Lua function expected");
  142. p = clvalue(index(L, 1))->l.p;
  143. lua_newtable(L);
  144. setnameval(L, "maxstack", p->maxstacksize);
  145. setnameval(L, "numparams", p->numparams);
  146. for (pc=0; pc<p->sizecode; pc++) {
  147. char buff[100];
  148. lua_pushnumber(L, pc+1);
  149. lua_pushstring(L, buildop(p, pc, buff));
  150. lua_settable(L, -3);
  151. }
  152. return 1;
  153. }
  154. static int listk (lua_State *L) {
  155. Proto *p;
  156. int i;
  157. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  158. 1, "Lua function expected");
  159. p = clvalue(index(L, 1))->l.p;
  160. lua_newtable(L);
  161. for (i=0; i<p->sizek; i++) {
  162. lua_pushnumber(L, i+1);
  163. luaA_pushobject(L, p->k+i);
  164. lua_settable(L, -3);
  165. }
  166. return 1;
  167. }
  168. static int listlocals (lua_State *L) {
  169. Proto *p;
  170. int pc = luaL_check_int(L, 2) - 1;
  171. int i = 0;
  172. const char *name;
  173. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  174. 1, "Lua function expected");
  175. p = clvalue(index(L, 1))->l.p;
  176. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  177. lua_pushstring(L, name);
  178. return i-1;
  179. }
  180. /* }====================================================== */
  181. static int get_limits (lua_State *L) {
  182. lua_newtable(L);
  183. setnameval(L, "BITS_INT", BITS_INT);
  184. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  185. setnameval(L, "MAXVARS", MAXVARS);
  186. setnameval(L, "MAXPARAMS", MAXPARAMS);
  187. setnameval(L, "MAXSTACK", MAXSTACK);
  188. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  189. return 1;
  190. }
  191. static int mem_query (lua_State *L) {
  192. if (lua_isnone(L, 1)) {
  193. lua_pushnumber(L, memdebug_total);
  194. lua_pushnumber(L, memdebug_numblocks);
  195. lua_pushnumber(L, memdebug_maxmem);
  196. return 3;
  197. }
  198. else {
  199. memdebug_memlimit = luaL_check_int(L, 1);
  200. return 0;
  201. }
  202. }
  203. static int hash_query (lua_State *L) {
  204. if (lua_isnone(L, 2)) {
  205. luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  206. lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
  207. }
  208. else {
  209. TObject *o = index(L, 1);
  210. Table *t;
  211. luaL_check_type(L, 2, LUA_TTABLE);
  212. t = hvalue(index(L, 2));
  213. lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
  214. }
  215. return 1;
  216. }
  217. static int stacklevel (lua_State *L) {
  218. unsigned long a = 0;
  219. lua_pushnumber(L, (int)(L->top - L->stack));
  220. lua_pushnumber(L, (int)(L->stack_last - L->stack));
  221. lua_pushnumber(L, (int)(L->ci - L->base_ci));
  222. lua_pushnumber(L, (int)(L->end_ci - L->base_ci));
  223. lua_pushnumber(L, (unsigned long)&a);
  224. return 5;
  225. }
  226. static int table_query (lua_State *L) {
  227. const Table *t;
  228. int i = luaL_opt_int(L, 2, -1);
  229. luaL_check_type(L, 1, LUA_TTABLE);
  230. t = hvalue(index(L, 1));
  231. if (i == -1) {
  232. lua_pushnumber(L, t->sizearray);
  233. lua_pushnumber(L, sizenode(t));
  234. lua_pushnumber(L, t->firstfree - t->node);
  235. }
  236. else if (i < t->sizearray) {
  237. lua_pushnumber(L, i);
  238. luaA_pushobject(L, &t->array[i]);
  239. lua_pushnil(L);
  240. }
  241. else if ((i -= t->sizearray) < sizenode(t)) {
  242. if (ttype(val(node(t, i))) != LUA_TNIL ||
  243. ttype(key(node(t, i))) == LUA_TNIL ||
  244. ttype(key(node(t, i))) == LUA_TNUMBER) {
  245. luaA_pushobject(L, key(node(t, i)));
  246. }
  247. else
  248. lua_pushstring(L, "<undef>");
  249. luaA_pushobject(L, val(&t->node[i]));
  250. if (t->node[i].next)
  251. lua_pushnumber(L, t->node[i].next - t->node);
  252. else
  253. lua_pushnil(L);
  254. }
  255. return 3;
  256. }
  257. static int string_query (lua_State *L) {
  258. stringtable *tb = &G(L)->strt;
  259. int s = luaL_opt_int(L, 2, 0) - 1;
  260. if (s==-1) {
  261. lua_pushnumber(L ,tb->nuse);
  262. lua_pushnumber(L ,tb->size);
  263. return 2;
  264. }
  265. else if (s < tb->size) {
  266. TString *ts;
  267. int n = 0;
  268. for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
  269. setsvalue(L->top, ts);
  270. incr_top(L);
  271. n++;
  272. }
  273. return n;
  274. }
  275. return 0;
  276. }
  277. static int tref (lua_State *L) {
  278. int level = lua_gettop(L);
  279. int lock = luaL_opt_int(L, 2, 1);
  280. luaL_check_any(L, 1);
  281. lua_pushvalue(L, 1);
  282. lua_pushnumber(L, lua_ref(L, lock));
  283. assert(lua_gettop(L) == level+1); /* +1 for result */
  284. return 1;
  285. }
  286. static int getref (lua_State *L) {
  287. int level = lua_gettop(L);
  288. lua_getref(L, luaL_check_int(L, 1));
  289. assert(lua_gettop(L) == level+1);
  290. return 1;
  291. }
  292. static int unref (lua_State *L) {
  293. int level = lua_gettop(L);
  294. lua_unref(L, luaL_check_int(L, 1));
  295. assert(lua_gettop(L) == level);
  296. return 0;
  297. }
  298. static int metatable (lua_State *L) {
  299. luaL_check_any(L, 1);
  300. if (lua_isnone(L, 2)) {
  301. if (lua_getmetatable(L, 1) == 0)
  302. lua_pushnil(L);
  303. }
  304. else {
  305. lua_settop(L, 2);
  306. luaL_check_type(L, 2, LUA_TTABLE);
  307. lua_setmetatable(L, 1);
  308. }
  309. return 1;
  310. }
  311. static int newuserdata (lua_State *L) {
  312. size_t size = luaL_check_int(L, 1);
  313. char *p = cast(char *, lua_newuserdata(L, size));
  314. while (size--) *p++ = '\0';
  315. return 1;
  316. }
  317. static int pushuserdata (lua_State *L) {
  318. lua_pushudataval(L, cast(void *, luaL_check_int(L, 1)));
  319. return 1;
  320. }
  321. static int udataval (lua_State *L) {
  322. lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
  323. return 1;
  324. }
  325. static int doonnewstack (lua_State *L) {
  326. lua_State *L1 = lua_newthread(L);
  327. size_t l;
  328. const char *s = luaL_check_lstr(L, 1, &l);
  329. int status = lua_loadbuffer(L1, s, l, s);
  330. if (status == 0)
  331. status = lua_pcall(L1, 0, 0, 0);
  332. lua_pushnumber(L, status);
  333. lua_closethread(L, L1);
  334. return 1;
  335. }
  336. static int s2d (lua_State *L) {
  337. lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
  338. return 1;
  339. }
  340. static int d2s (lua_State *L) {
  341. double d = luaL_check_number(L, 1);
  342. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  343. return 1;
  344. }
  345. static int newstate (lua_State *L) {
  346. lua_State *L1 = lua_open();
  347. if (L1) {
  348. *cast(int **, L1) = &islocked; /* initialize the lock */
  349. lua_pushnumber(L, (unsigned long)L1);
  350. }
  351. else
  352. lua_pushnil(L);
  353. return 1;
  354. }
  355. static int loadlib (lua_State *L) {
  356. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  357. lua_register(L1, "mathlibopen", lua_mathlibopen);
  358. lua_register(L1, "strlibopen", lua_strlibopen);
  359. lua_register(L1, "iolibopen", lua_iolibopen);
  360. lua_register(L1, "dblibopen", lua_dblibopen);
  361. lua_register(L1, "baselibopen", lua_baselibopen);
  362. return 0;
  363. }
  364. static int closestate (lua_State *L) {
  365. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  366. lua_close(L1);
  367. lua_unlock(L); /* close cannot unlock that */
  368. return 0;
  369. }
  370. static int doremote (lua_State *L) {
  371. lua_State *L1;
  372. const char *code = luaL_check_string(L, 2);
  373. int status;
  374. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  375. status = lua_dostring(L1, code);
  376. if (status != 0) {
  377. lua_pushnil(L);
  378. lua_pushnumber(L, status);
  379. return 2;
  380. }
  381. else {
  382. int i = 0;
  383. while (!lua_isnone(L1, ++i))
  384. lua_pushstring(L, lua_tostring(L1, i));
  385. lua_pop(L1, i-1);
  386. return i-1;
  387. }
  388. }
  389. static int log2_aux (lua_State *L) {
  390. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  391. return 1;
  392. }
  393. /*
  394. ** {======================================================
  395. ** function to test the API with C. It interprets a kind of assembler
  396. ** language with calls to the API, so the test can be driven by Lua code
  397. ** =======================================================
  398. */
  399. static const char *const delimits = " \t\n,;";
  400. static void skip (const char **pc) {
  401. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  402. }
  403. static int getnum_aux (lua_State *L, const char **pc) {
  404. int res = 0;
  405. int sig = 1;
  406. skip(pc);
  407. if (**pc == '.') {
  408. res = cast(int, lua_tonumber(L, -1));
  409. lua_pop(L, 1);
  410. (*pc)++;
  411. return res;
  412. }
  413. else if (**pc == '-') {
  414. sig = -1;
  415. (*pc)++;
  416. }
  417. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  418. return sig*res;
  419. }
  420. static const char *getname_aux (char *buff, const char **pc) {
  421. int i = 0;
  422. skip(pc);
  423. while (**pc != '\0' && !strchr(delimits, **pc))
  424. buff[i++] = *(*pc)++;
  425. buff[i] = '\0';
  426. return buff;
  427. }
  428. #define EQ(s1) (strcmp(s1, inst) == 0)
  429. #define getnum (getnum_aux(L, &pc))
  430. #define getname (getname_aux(buff, &pc))
  431. static int testC (lua_State *L) {
  432. char buff[30];
  433. const char *pc = luaL_check_string(L, 1);
  434. for (;;) {
  435. const char *inst = getname;
  436. if EQ("") return 0;
  437. else if EQ("isnumber") {
  438. lua_pushnumber(L, lua_isnumber(L, getnum));
  439. }
  440. else if EQ("isstring") {
  441. lua_pushnumber(L, lua_isstring(L, getnum));
  442. }
  443. else if EQ("istable") {
  444. lua_pushnumber(L, lua_istable(L, getnum));
  445. }
  446. else if EQ("iscfunction") {
  447. lua_pushnumber(L, lua_iscfunction(L, getnum));
  448. }
  449. else if EQ("isfunction") {
  450. lua_pushnumber(L, lua_isfunction(L, getnum));
  451. }
  452. else if EQ("isuserdata") {
  453. lua_pushnumber(L, lua_isuserdata(L, getnum));
  454. }
  455. else if EQ("isnil") {
  456. lua_pushnumber(L, lua_isnil(L, getnum));
  457. }
  458. else if EQ("isnull") {
  459. lua_pushnumber(L, lua_isnone(L, getnum));
  460. }
  461. else if EQ("tonumber") {
  462. lua_pushnumber(L, lua_tonumber(L, getnum));
  463. }
  464. else if EQ("tostring") {
  465. const char *s = lua_tostring(L, getnum);
  466. lua_pushstring(L, s);
  467. }
  468. else if EQ("tonumber") {
  469. lua_pushnumber(L, lua_tonumber(L, getnum));
  470. }
  471. else if EQ("strlen") {
  472. lua_pushnumber(L, lua_strlen(L, getnum));
  473. }
  474. else if EQ("tocfunction") {
  475. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  476. }
  477. else if EQ("return") {
  478. return getnum;
  479. }
  480. else if EQ("gettop") {
  481. lua_pushnumber(L, lua_gettop(L));
  482. }
  483. else if EQ("settop") {
  484. lua_settop(L, getnum);
  485. }
  486. else if EQ("pop") {
  487. lua_pop(L, getnum);
  488. }
  489. else if EQ("pushnum") {
  490. lua_pushnumber(L, getnum);
  491. }
  492. else if EQ("pushnil") {
  493. lua_pushnil(L);
  494. }
  495. else if EQ("pushbool") {
  496. lua_pushboolean(L, getnum);
  497. }
  498. else if EQ("tobool") {
  499. lua_pushnumber(L, lua_toboolean(L, getnum));
  500. }
  501. else if EQ("pushvalue") {
  502. lua_pushvalue(L, getnum);
  503. }
  504. else if EQ("pushcclosure") {
  505. lua_pushcclosure(L, testC, getnum);
  506. }
  507. else if EQ("pushupvalues") {
  508. lua_pushupvalues(L);
  509. }
  510. else if EQ("remove") {
  511. lua_remove(L, getnum);
  512. }
  513. else if EQ("insert") {
  514. lua_insert(L, getnum);
  515. }
  516. else if EQ("replace") {
  517. lua_replace(L, getnum);
  518. }
  519. else if EQ("gettable") {
  520. lua_gettable(L, getnum);
  521. }
  522. else if EQ("settable") {
  523. lua_settable(L, getnum);
  524. }
  525. else if EQ("next") {
  526. lua_next(L, -2);
  527. }
  528. else if EQ("concat") {
  529. lua_concat(L, getnum);
  530. }
  531. else if EQ("lessthan") {
  532. int a = getnum;
  533. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  534. }
  535. else if EQ("equal") {
  536. int a = getnum;
  537. lua_pushboolean(L, lua_equal(L, a, getnum));
  538. }
  539. else if EQ("rawcall") {
  540. int narg = getnum;
  541. int nres = getnum;
  542. lua_rawcall(L, narg, nres);
  543. }
  544. else if EQ("call") {
  545. int narg = getnum;
  546. int nres = getnum;
  547. lua_call(L, narg, nres);
  548. }
  549. else if EQ("loadstring") {
  550. size_t sl;
  551. const char *s = luaL_check_lstr(L, getnum, &sl);
  552. lua_loadbuffer(L, s, sl, s);
  553. }
  554. else if EQ("loadfile") {
  555. lua_loadfile(L, luaL_check_string(L, getnum));
  556. }
  557. else if EQ("setmetatable") {
  558. lua_setmetatable(L, getnum);
  559. }
  560. else if EQ("getmetatable") {
  561. if (lua_getmetatable(L, getnum) == 0)
  562. lua_pushnil(L);
  563. }
  564. else if EQ("type") {
  565. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  566. }
  567. else luaL_verror(L, "unknown instruction %s", buff);
  568. }
  569. return 0;
  570. }
  571. /* }====================================================== */
  572. static const struct luaL_reg tests_funcs[] = {
  573. {"hash", hash_query},
  574. {"limits", get_limits},
  575. {"listcode", listcode},
  576. {"listk", listk},
  577. {"listlocals", listlocals},
  578. {"loadlib", loadlib},
  579. {"stacklevel", stacklevel},
  580. {"querystr", string_query},
  581. {"querytab", table_query},
  582. {"testC", testC},
  583. {"ref", tref},
  584. {"getref", getref},
  585. {"unref", unref},
  586. {"d2s", d2s},
  587. {"s2d", s2d},
  588. {"metatable", metatable},
  589. {"newuserdata", newuserdata},
  590. {"pushuserdata", pushuserdata},
  591. {"udataval", udataval},
  592. {"doonnewstack", doonnewstack},
  593. {"newstate", newstate},
  594. {"closestate", closestate},
  595. {"doremote", doremote},
  596. {"log2", log2_aux},
  597. {"totalmem", mem_query},
  598. {NULL, NULL}
  599. };
  600. static void fim (void) {
  601. if (!islocked)
  602. lua_close(lua_state);
  603. lua_assert(memdebug_numblocks == 0);
  604. lua_assert(memdebug_total == 0);
  605. }
  606. void luaB_opentests (lua_State *L) {
  607. *cast(int **, L) = &islocked; /* init lock */
  608. lua_state = L; /* keep first state to be opened */
  609. luaL_opennamedlib(L, "T", tests_funcs, 0);
  610. atexit(fim);
  611. }
  612. #endif