ltests.c 17 KB

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