ltests.c 17 KB

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