ltests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. ** $Id: ltests.c,v 1.125 2002/06/13 13:44:50 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 (ttype(val(node(t, i))) != LUA_TNIL ||
  246. ttype(key(node(t, i))) == LUA_TNIL ||
  247. ttype(key(node(t, i))) == LUA_TNUMBER) {
  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 xpcall (lua_State *L) {
  281. int status;
  282. luaL_check_type(L, 1, LUA_TFUNCTION);
  283. luaL_check_any(L, 2);
  284. lua_pushliteral(L, LUA_TRACEBACK);
  285. lua_gettable(L, LUA_REGISTRYINDEX);
  286. lua_pushliteral(L, LUA_TRACEBACK);
  287. lua_pushvalue(L, 1);
  288. lua_settable(L, LUA_REGISTRYINDEX);
  289. lua_replace(L, 1);
  290. status = lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET);
  291. lua_pushliteral(L, LUA_TRACEBACK);
  292. lua_pushvalue(L, 1);
  293. lua_settable(L, LUA_REGISTRYINDEX);
  294. if (status != 0) {
  295. int numres = (status == LUA_ERRRUN) ? 3 : 2;
  296. lua_pushnil(L);
  297. lua_insert(L, -numres);
  298. return numres;
  299. }
  300. else {
  301. lua_pushboolean(L, 1);
  302. lua_insert(L, 2);
  303. return lua_gettop(L) - 1; /* return `true' + all results */
  304. }
  305. }
  306. static int tref (lua_State *L) {
  307. int level = lua_gettop(L);
  308. int lock = luaL_opt_int(L, 2, 1);
  309. luaL_check_any(L, 1);
  310. lua_pushvalue(L, 1);
  311. lua_pushnumber(L, lua_ref(L, lock));
  312. assert(lua_gettop(L) == level+1); /* +1 for result */
  313. return 1;
  314. }
  315. static int getref (lua_State *L) {
  316. int level = lua_gettop(L);
  317. lua_getref(L, luaL_check_int(L, 1));
  318. assert(lua_gettop(L) == level+1);
  319. return 1;
  320. }
  321. static int unref (lua_State *L) {
  322. int level = lua_gettop(L);
  323. lua_unref(L, luaL_check_int(L, 1));
  324. assert(lua_gettop(L) == level);
  325. return 0;
  326. }
  327. static int metatable (lua_State *L) {
  328. luaL_check_any(L, 1);
  329. if (lua_isnone(L, 2)) {
  330. if (lua_getmetatable(L, 1) == 0)
  331. lua_pushnil(L);
  332. }
  333. else {
  334. lua_settop(L, 2);
  335. luaL_check_type(L, 2, LUA_TTABLE);
  336. lua_setmetatable(L, 1);
  337. }
  338. return 1;
  339. }
  340. static int newuserdata (lua_State *L) {
  341. size_t size = luaL_check_int(L, 1);
  342. char *p = cast(char *, lua_newuserdata(L, size));
  343. while (size--) *p++ = '\0';
  344. return 1;
  345. }
  346. static int pushuserdata (lua_State *L) {
  347. lua_pushudataval(L, cast(void *, luaL_check_int(L, 1)));
  348. return 1;
  349. }
  350. static int udataval (lua_State *L) {
  351. lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
  352. return 1;
  353. }
  354. static int doonnewstack (lua_State *L) {
  355. lua_State *L1 = lua_newthread(L);
  356. size_t l;
  357. const char *s = luaL_check_lstr(L, 1, &l);
  358. int status = luaL_loadbuffer(L1, s, l, s);
  359. if (status == 0)
  360. status = lua_pcall(L1, 0, 0);
  361. lua_pushnumber(L, status);
  362. lua_closethread(L, L1);
  363. return 1;
  364. }
  365. static int s2d (lua_State *L) {
  366. lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
  367. return 1;
  368. }
  369. static int d2s (lua_State *L) {
  370. double d = luaL_check_number(L, 1);
  371. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  372. return 1;
  373. }
  374. static int newstate (lua_State *L) {
  375. lua_State *L1 = lua_open();
  376. if (L1) {
  377. *cast(int **, L1) = &islocked; /* initialize the lock */
  378. lua_pushnumber(L, (unsigned long)L1);
  379. }
  380. else
  381. lua_pushnil(L);
  382. return 1;
  383. }
  384. static int loadlib (lua_State *L) {
  385. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  386. lua_register(L1, "mathlibopen", lua_mathlibopen);
  387. lua_register(L1, "strlibopen", lua_strlibopen);
  388. lua_register(L1, "iolibopen", lua_iolibopen);
  389. lua_register(L1, "dblibopen", lua_dblibopen);
  390. lua_register(L1, "baselibopen", lua_baselibopen);
  391. return 0;
  392. }
  393. static int closestate (lua_State *L) {
  394. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  395. lua_close(L1);
  396. lua_unlock(L); /* close cannot unlock that */
  397. return 0;
  398. }
  399. static int doremote (lua_State *L) {
  400. lua_State *L1;
  401. const char *code = luaL_check_string(L, 2);
  402. int status;
  403. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  404. status = lua_dostring(L1, code);
  405. if (status != 0) {
  406. lua_pushnil(L);
  407. lua_pushnumber(L, status);
  408. return 2;
  409. }
  410. else {
  411. int i = 0;
  412. while (!lua_isnone(L1, ++i))
  413. lua_pushstring(L, lua_tostring(L1, i));
  414. lua_pop(L1, i-1);
  415. return i-1;
  416. }
  417. }
  418. static int log2_aux (lua_State *L) {
  419. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  420. return 1;
  421. }
  422. /*
  423. ** {======================================================
  424. ** function to test the API with C. It interprets a kind of assembler
  425. ** language with calls to the API, so the test can be driven by Lua code
  426. ** =======================================================
  427. */
  428. static const char *const delimits = " \t\n,;";
  429. static void skip (const char **pc) {
  430. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  431. }
  432. static int getnum_aux (lua_State *L, const char **pc) {
  433. int res = 0;
  434. int sig = 1;
  435. skip(pc);
  436. if (**pc == '.') {
  437. res = cast(int, lua_tonumber(L, -1));
  438. lua_pop(L, 1);
  439. (*pc)++;
  440. return res;
  441. }
  442. else if (**pc == '-') {
  443. sig = -1;
  444. (*pc)++;
  445. }
  446. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  447. return sig*res;
  448. }
  449. static const char *getname_aux (char *buff, const char **pc) {
  450. int i = 0;
  451. skip(pc);
  452. while (**pc != '\0' && !strchr(delimits, **pc))
  453. buff[i++] = *(*pc)++;
  454. buff[i] = '\0';
  455. return buff;
  456. }
  457. #define EQ(s1) (strcmp(s1, inst) == 0)
  458. #define getnum (getnum_aux(L, &pc))
  459. #define getname (getname_aux(buff, &pc))
  460. static int testC (lua_State *L) {
  461. char buff[30];
  462. const char *pc = luaL_check_string(L, 1);
  463. for (;;) {
  464. const char *inst = getname;
  465. if EQ("") return 0;
  466. else if EQ("isnumber") {
  467. lua_pushnumber(L, lua_isnumber(L, getnum));
  468. }
  469. else if EQ("isstring") {
  470. lua_pushnumber(L, lua_isstring(L, getnum));
  471. }
  472. else if EQ("istable") {
  473. lua_pushnumber(L, lua_istable(L, getnum));
  474. }
  475. else if EQ("iscfunction") {
  476. lua_pushnumber(L, lua_iscfunction(L, getnum));
  477. }
  478. else if EQ("isfunction") {
  479. lua_pushnumber(L, lua_isfunction(L, getnum));
  480. }
  481. else if EQ("isuserdata") {
  482. lua_pushnumber(L, lua_isuserdata(L, getnum));
  483. }
  484. else if EQ("isnil") {
  485. lua_pushnumber(L, lua_isnil(L, getnum));
  486. }
  487. else if EQ("isnull") {
  488. lua_pushnumber(L, lua_isnone(L, getnum));
  489. }
  490. else if EQ("tonumber") {
  491. lua_pushnumber(L, lua_tonumber(L, getnum));
  492. }
  493. else if EQ("tostring") {
  494. const char *s = lua_tostring(L, getnum);
  495. lua_pushstring(L, s);
  496. }
  497. else if EQ("tonumber") {
  498. lua_pushnumber(L, lua_tonumber(L, getnum));
  499. }
  500. else if EQ("strlen") {
  501. lua_pushnumber(L, lua_strlen(L, getnum));
  502. }
  503. else if EQ("tocfunction") {
  504. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  505. }
  506. else if EQ("return") {
  507. return getnum;
  508. }
  509. else if EQ("gettop") {
  510. lua_pushnumber(L, lua_gettop(L));
  511. }
  512. else if EQ("settop") {
  513. lua_settop(L, getnum);
  514. }
  515. else if EQ("pop") {
  516. lua_pop(L, getnum);
  517. }
  518. else if EQ("pushnum") {
  519. lua_pushnumber(L, getnum);
  520. }
  521. else if EQ("pushnil") {
  522. lua_pushnil(L);
  523. }
  524. else if EQ("pushbool") {
  525. lua_pushboolean(L, getnum);
  526. }
  527. else if EQ("tobool") {
  528. lua_pushnumber(L, lua_toboolean(L, getnum));
  529. }
  530. else if EQ("pushvalue") {
  531. lua_pushvalue(L, getnum);
  532. }
  533. else if EQ("pushcclosure") {
  534. lua_pushcclosure(L, testC, getnum);
  535. }
  536. else if EQ("pushupvalues") {
  537. lua_pushupvalues(L);
  538. }
  539. else if EQ("remove") {
  540. lua_remove(L, getnum);
  541. }
  542. else if EQ("insert") {
  543. lua_insert(L, getnum);
  544. }
  545. else if EQ("replace") {
  546. lua_replace(L, getnum);
  547. }
  548. else if EQ("gettable") {
  549. lua_gettable(L, getnum);
  550. }
  551. else if EQ("settable") {
  552. lua_settable(L, getnum);
  553. }
  554. else if EQ("next") {
  555. lua_next(L, -2);
  556. }
  557. else if EQ("concat") {
  558. lua_concat(L, getnum);
  559. }
  560. else if EQ("lessthan") {
  561. int a = getnum;
  562. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  563. }
  564. else if EQ("equal") {
  565. int a = getnum;
  566. lua_pushboolean(L, lua_equal(L, a, getnum));
  567. }
  568. else if EQ("rawcall") {
  569. int narg = getnum;
  570. int nres = getnum;
  571. lua_upcall(L, narg, nres);
  572. }
  573. else if EQ("call") {
  574. int narg = getnum;
  575. int nres = getnum;
  576. lua_pcall(L, narg, nres);
  577. }
  578. else if EQ("loadstring") {
  579. size_t sl;
  580. const char *s = luaL_check_lstr(L, getnum, &sl);
  581. luaL_loadbuffer(L, s, sl, s);
  582. }
  583. else if EQ("loadfile") {
  584. luaL_loadfile(L, luaL_check_string(L, getnum));
  585. }
  586. else if EQ("setmetatable") {
  587. lua_setmetatable(L, getnum);
  588. }
  589. else if EQ("getmetatable") {
  590. if (lua_getmetatable(L, getnum) == 0)
  591. lua_pushnil(L);
  592. }
  593. else if EQ("type") {
  594. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  595. }
  596. else luaL_error(L, "unknown instruction %s", buff);
  597. }
  598. return 0;
  599. }
  600. /* }====================================================== */
  601. static const struct luaL_reg tests_funcs[] = {
  602. {"hash", hash_query},
  603. {"limits", get_limits},
  604. {"listcode", listcode},
  605. {"listk", listk},
  606. {"listlocals", listlocals},
  607. {"loadlib", loadlib},
  608. {"stacklevel", stacklevel},
  609. {"querystr", string_query},
  610. {"xpcall", xpcall},
  611. {"querytab", table_query},
  612. {"testC", testC},
  613. {"ref", tref},
  614. {"getref", getref},
  615. {"unref", unref},
  616. {"d2s", d2s},
  617. {"s2d", s2d},
  618. {"metatable", metatable},
  619. {"newuserdata", newuserdata},
  620. {"pushuserdata", pushuserdata},
  621. {"udataval", udataval},
  622. {"doonnewstack", doonnewstack},
  623. {"newstate", newstate},
  624. {"closestate", closestate},
  625. {"doremote", doremote},
  626. {"log2", log2_aux},
  627. {"totalmem", mem_query},
  628. {NULL, NULL}
  629. };
  630. static void fim (void) {
  631. if (!islocked)
  632. lua_close(lua_state);
  633. lua_assert(memdebug_numblocks == 0);
  634. lua_assert(memdebug_total == 0);
  635. }
  636. void luaB_opentests (lua_State *L) {
  637. *cast(int **, L) = &islocked; /* init lock */
  638. lua_state = L; /* keep first state to be opened */
  639. luaL_opennamedlib(L, "T", tests_funcs, 0);
  640. atexit(fim);
  641. }
  642. #endif