ltests.c 17 KB

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