ltests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. ** $Id: ltests.c,v 1.81 2001/06/05 18:17:01 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. #define LUA_PRIVATE
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "lauxlib.h"
  15. #include "lcode.h"
  16. #include "ldebug.h"
  17. #include "ldo.h"
  18. #include "lfunc.h"
  19. #include "lmem.h"
  20. #include "lopcodes.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "ltable.h"
  24. #include "luadebug.h"
  25. #include "lualib.h"
  26. /*
  27. ** The whole module only makes sense with LUA_DEBUG on
  28. */
  29. #ifdef LUA_DEBUG
  30. lua_State *lua_state = NULL;
  31. int islocked = 0;
  32. static void setnameval (lua_State *L, const l_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) ((size_t *)((l_char *)(b) - HEADER))
  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(*(((l_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. l_char *newblock;
  79. int i;
  80. size_t realsize = HEADER+size+MARKSIZE;
  81. if (realsize < size) return NULL; /* overflow! */
  82. newblock = (l_char *)malloc(realsize); /* alloc a new block */
  83. if (newblock == NULL) return NULL;
  84. if (oldsize > size) oldsize = size;
  85. if (block) {
  86. memcpy(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(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. *(size_t *)newblock = size;
  96. for (i=0;i<MARKSIZE;i++)
  97. *(newblock+HEADER+size+i) = (l_char)(MARK+i);
  98. return newblock+HEADER;
  99. }
  100. }
  101. /* }====================================================================== */
  102. /*
  103. ** {======================================================
  104. ** Disassembler
  105. ** =======================================================
  106. */
  107. static const l_char *const instrname[NUM_OPCODES] = {
  108. l_s("OP_MOVE"),
  109. l_s("OP_LOADK"),
  110. l_s("OP_LOADINT"),
  111. l_s("OP_LOADNIL"),
  112. l_s("OP_LOADUPVAL"),
  113. l_s("OP_GETGLOBAL"),
  114. l_s("OP_GETTABLE"),
  115. l_s("OP_SETGLOBAL"),
  116. l_s("OP_SETTABLE"),
  117. l_s("OP_NEWTABLE"),
  118. l_s("OP_SELF"),
  119. l_s("OP_ADD"),
  120. l_s("OP_SUB"),
  121. l_s("OP_MUL"),
  122. l_s("OP_DIV"),
  123. l_s("OP_POW"),
  124. l_s("OP_UNM"),
  125. l_s("OP_NOT"),
  126. l_s("OP_CONCAT"),
  127. l_s("OP_JMP"),
  128. l_s("OP_CJMP"),
  129. l_s("OP_TESTEQ"),
  130. l_s("OP_TESTNE"),
  131. l_s("OP_TESTLT"),
  132. l_s("OP_TESTLE"),
  133. l_s("OP_TESTGT"),
  134. l_s("OP_TESTGE"),
  135. l_s("OP_TESTT"),
  136. l_s("OP_TESTF"),
  137. l_s("OP_NILJMP"),
  138. l_s("OP_CALL"),
  139. l_s("OP_RETURN"),
  140. l_s("OP_FORPREP"),
  141. l_s("OP_FORLOOP"),
  142. l_s("OP_LFORPREP"),
  143. l_s("OP_LFORLOOP"),
  144. l_s("OP_SETLIST"),
  145. l_s("OP_CLOSURE")
  146. };
  147. static l_char *buildop (Proto *p, int pc, l_char *buff) {
  148. Instruction i = p->code[pc];
  149. OpCode o = GET_OPCODE(i);
  150. const l_char *name = instrname[o];
  151. sprintf(buff, l_s("%4d - "), pc);
  152. switch (getOpMode(o)) {
  153. case iABC:
  154. sprintf(buff+strlen(buff), l_s("%-12s%4d %4d %4d"), name,
  155. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  156. break;
  157. case iABc:
  158. sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_Bc(i));
  159. break;
  160. case iAsBc:
  161. sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_sBc(i));
  162. break;
  163. }
  164. return buff;
  165. }
  166. static int listcode (lua_State *L) {
  167. int pc;
  168. Proto *p;
  169. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  170. 1, l_s("Lua function expected"));
  171. p = clvalue(luaA_index(L, 1))->f.l;
  172. lua_newtable(L);
  173. setnameval(L, l_s("maxstack"), p->maxstacksize);
  174. setnameval(L, l_s("numparams"), p->numparams);
  175. for (pc=0; pc<p->sizecode; pc++) {
  176. l_char buff[100];
  177. lua_pushnumber(L, pc+1);
  178. lua_pushstring(L, buildop(p, pc, buff));
  179. lua_settable(L, -3);
  180. }
  181. return 1;
  182. }
  183. static int listk (lua_State *L) {
  184. Proto *p;
  185. int i;
  186. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  187. 1, l_s("Lua function expected"));
  188. p = clvalue(luaA_index(L, 1))->f.l;
  189. lua_newtable(L);
  190. for (i=0; i<p->sizek; i++) {
  191. lua_pushnumber(L, i+1);
  192. luaA_pushobject(L, p->k+i);
  193. lua_settable(L, -3);
  194. }
  195. return 1;
  196. }
  197. static int listlocals (lua_State *L) {
  198. Proto *p;
  199. int pc = luaL_check_int(L, 2) - 1;
  200. int i = 0;
  201. const l_char *name;
  202. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  203. 1, l_s("Lua function expected"));
  204. p = clvalue(luaA_index(L, 1))->f.l;
  205. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  206. lua_pushstring(L, name);
  207. return i-1;
  208. }
  209. /* }====================================================== */
  210. static int pushbool (lua_State *L, int b) {
  211. if (b) lua_pushnumber(L, 1);
  212. else lua_pushnil(L);
  213. return 1;
  214. }
  215. static int get_limits (lua_State *L) {
  216. lua_newtable(L);
  217. setnameval(L, l_s("BITS_INT"), BITS_INT);
  218. setnameval(L, l_s("LFPF"), LFIELDS_PER_FLUSH);
  219. setnameval(L, l_s("MAXLOCALS"), MAXLOCALS);
  220. setnameval(L, l_s("MAXPARAMS"), MAXPARAMS);
  221. setnameval(L, l_s("MAXSTACK"), MAXSTACK);
  222. setnameval(L, l_s("MAXUPVALUES"), MAXUPVALUES);
  223. return 1;
  224. }
  225. static int mem_query (lua_State *L) {
  226. if (lua_isnull(L, 1)) {
  227. lua_pushnumber(L, memdebug_total);
  228. lua_pushnumber(L, memdebug_numblocks);
  229. lua_pushnumber(L, memdebug_maxmem);
  230. return 3;
  231. }
  232. else {
  233. memdebug_memlimit = luaL_check_int(L, 1);
  234. return 0;
  235. }
  236. }
  237. static int hash_query (lua_State *L) {
  238. if (lua_isnull(L, 2)) {
  239. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, l_s("string expected"));
  240. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->hash);
  241. }
  242. else {
  243. Hash *t;
  244. Node n;
  245. TObject *o = luaA_index(L, 1);
  246. luaL_checktype(L, 2, LUA_TTABLE);
  247. t = hvalue(luaA_index(L, 2));
  248. setobj2key(&n, o);
  249. lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
  250. }
  251. return 1;
  252. }
  253. static int table_query (lua_State *L) {
  254. const Hash *t;
  255. int i = luaL_opt_int(L, 2, -1);
  256. luaL_checktype(L, 1, LUA_TTABLE);
  257. t = hvalue(luaA_index(L, 1));
  258. if (i == -1) {
  259. lua_pushnumber(L, t->size);
  260. lua_pushnumber(L, t->firstfree - t->node);
  261. return 2;
  262. }
  263. else if (i < t->size) {
  264. if (ttype(val(node(t, i))) != LUA_TNIL ||
  265. ttype_key(node(t, i)) == LUA_TNIL ||
  266. ttype_key(node(t, i)) == LUA_TNUMBER ||
  267. tsvalue_key(node(t, i)) != NULL) {
  268. TObject o;
  269. setkey2obj(&o, node(t, i));
  270. luaA_pushobject(L, &o);
  271. }
  272. else
  273. lua_pushstring(L, "<undef>");
  274. luaA_pushobject(L, &t->node[i].val);
  275. if (t->node[i].next) {
  276. lua_pushnumber(L, t->node[i].next - t->node);
  277. return 3;
  278. }
  279. else
  280. return 2;
  281. }
  282. return 0;
  283. }
  284. static int string_query (lua_State *L) {
  285. stringtable *tb = &G(L)->strt;
  286. int s = luaL_opt_int(L, 2, 0) - 1;
  287. if (s==-1) {
  288. lua_pushnumber(L ,tb->nuse);
  289. lua_pushnumber(L ,tb->size);
  290. return 2;
  291. }
  292. else if (s < tb->size) {
  293. TString *ts;
  294. int n = 0;
  295. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  296. setsvalue(L->top, ts);
  297. incr_top;
  298. n++;
  299. }
  300. return n;
  301. }
  302. return 0;
  303. }
  304. static int tref (lua_State *L) {
  305. int level = lua_gettop(L);
  306. luaL_checkany(L, 1);
  307. lua_pushvalue(L, 1);
  308. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  309. assert(lua_gettop(L) == level+1); /* +1 for result */
  310. return 1;
  311. }
  312. static int getref (lua_State *L) {
  313. int level = lua_gettop(L);
  314. if (lua_getref(L, luaL_check_int(L, 1))) {
  315. assert(lua_gettop(L) == level+1);
  316. return 1;
  317. }
  318. else {
  319. assert(lua_gettop(L) == level);
  320. return 0;
  321. }
  322. }
  323. static int unref (lua_State *L) {
  324. int level = lua_gettop(L);
  325. lua_unref(L, luaL_check_int(L, 1));
  326. assert(lua_gettop(L) == level);
  327. return 0;
  328. }
  329. static int newuserdata (lua_State *L) {
  330. size_t size = luaL_check_int(L, 1);
  331. l_char *p = (l_char *)lua_newuserdata(L, size);
  332. while (size--) *p++ = l_c('\0');
  333. return 1;
  334. }
  335. static int newuserdatabox (lua_State *L) {
  336. lua_newuserdatabox(L, (void *)luaL_check_int(L, 1));
  337. return 1;
  338. }
  339. static int settag (lua_State *L) {
  340. luaL_checkany(L, 1);
  341. lua_pushvalue(L, 1); /* push value */
  342. lua_settag(L, luaL_check_int(L, 2));
  343. return 1; /* return value */
  344. }
  345. static int udataval (lua_State *L) {
  346. luaL_checktype(L, 1, LUA_TUSERDATA);
  347. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  348. return 1;
  349. }
  350. static int newtag (lua_State *L) {
  351. lua_pushnumber(L, lua_newxtype(L, lua_tostring(L, 1),
  352. (int)lua_tonumber(L, 2)));
  353. return 1;
  354. }
  355. static int doonnewstack (lua_State *L) {
  356. lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
  357. if (L1 == NULL) return 0;
  358. *((int **)L1) = &islocked; /* initialize the lock */
  359. lua_dostring(L1, luaL_check_string(L, 2));
  360. lua_pushnumber(L, 1);
  361. lua_close(L1);
  362. return 1;
  363. }
  364. static int s2d (lua_State *L) {
  365. lua_pushnumber(L, *(double *)luaL_check_string(L, 1));
  366. return 1;
  367. }
  368. static int d2s (lua_State *L) {
  369. double d = luaL_check_number(L, 1);
  370. lua_pushlstring(L, (l_char *)&d, sizeof(d));
  371. return 1;
  372. }
  373. static int newstate (lua_State *L) {
  374. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  375. if (L1) {
  376. *((int **)L1) = &islocked; /* initialize the lock */
  377. lua_pushnumber(L, (unsigned long)L1);
  378. }
  379. else
  380. lua_pushnil(L);
  381. return 1;
  382. }
  383. static int loadlib (lua_State *L) {
  384. lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  385. lua_register(L1, "mathlibopen", lua_mathlibopen);
  386. lua_register(L1, "strlibopen", lua_strlibopen);
  387. lua_register(L1, "iolibopen", lua_iolibopen);
  388. lua_register(L1, "dblibopen", lua_dblibopen);
  389. lua_register(L1, "baselibopen", lua_baselibopen);
  390. return 0;
  391. }
  392. static int closestate (lua_State *L) {
  393. lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  394. lua_close(L1);
  395. lua_unlock(L); /* close cannot unlock that */
  396. return 0;
  397. }
  398. static int doremote (lua_State *L) {
  399. lua_State *L1;
  400. const l_char *code = luaL_check_string(L, 2);
  401. int status;
  402. L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  403. status = lua_dostring(L1, code);
  404. if (status != 0) {
  405. lua_pushnil(L);
  406. lua_pushnumber(L, status);
  407. return 2;
  408. }
  409. else {
  410. int i = 0;
  411. while (!lua_isnull(L1, ++i))
  412. lua_pushstring(L, lua_tostring(L1, i));
  413. lua_pop(L1, i-1);
  414. return i-1;
  415. }
  416. }
  417. static int settagmethod (lua_State *L) {
  418. int tag = luaL_check_int(L, 1);
  419. const l_char *event = luaL_check_string(L, 2);
  420. luaL_checkany(L, 3);
  421. lua_gettagmethod(L, tag, event);
  422. lua_pushvalue(L, 3);
  423. lua_settagmethod(L, tag, event);
  424. return 1;
  425. }
  426. static int equal (lua_State *L) {
  427. return pushbool(L, lua_equal(L, 1, 2));
  428. }
  429. /*
  430. ** {======================================================
  431. ** function to test the API with C. It interprets a kind of assembler
  432. ** language with calls to the API, so the test can be driven by Lua code
  433. ** =======================================================
  434. */
  435. static const l_char *const delimits = l_s(" \t\n,;");
  436. static void skip (const l_char **pc) {
  437. while (**pc != l_c('\0') && strchr(delimits, **pc)) (*pc)++;
  438. }
  439. static int getnum (lua_State *L, const l_char **pc) {
  440. int res = 0;
  441. int sig = 1;
  442. skip(pc);
  443. if (**pc == l_c('.')) {
  444. res = (int)lua_tonumber(L, -1);
  445. lua_pop(L, 1);
  446. (*pc)++;
  447. return res;
  448. }
  449. else if (**pc == l_c('-')) {
  450. sig = -1;
  451. (*pc)++;
  452. }
  453. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - l_c('0');
  454. return sig*res;
  455. }
  456. static const l_char *getname (l_char *buff, const l_char **pc) {
  457. int i = 0;
  458. skip(pc);
  459. while (**pc != l_c('\0') && !strchr(delimits, **pc))
  460. buff[i++] = *(*pc)++;
  461. buff[i] = l_c('\0');
  462. return buff;
  463. }
  464. #define EQ(s1) (strcmp(s1, inst) == 0)
  465. #define getnum ((getnum)(L, &pc))
  466. #define getname ((getname)(buff, &pc))
  467. static int testC (lua_State *L) {
  468. l_char buff[30];
  469. const l_char *pc = luaL_check_string(L, 1);
  470. for (;;) {
  471. const l_char *inst = getname;
  472. if EQ(l_s("")) return 0;
  473. else if EQ(l_s("isnumber")) {
  474. lua_pushnumber(L, lua_isnumber(L, getnum));
  475. }
  476. else if EQ(l_s("isstring")) {
  477. lua_pushnumber(L, lua_isstring(L, getnum));
  478. }
  479. else if EQ(l_s("istable")) {
  480. lua_pushnumber(L, lua_istable(L, getnum));
  481. }
  482. else if EQ(l_s("iscfunction")) {
  483. lua_pushnumber(L, lua_iscfunction(L, getnum));
  484. }
  485. else if EQ(l_s("isfunction")) {
  486. lua_pushnumber(L, lua_isfunction(L, getnum));
  487. }
  488. else if EQ(l_s("isuserdata")) {
  489. lua_pushnumber(L, lua_isuserdata(L, getnum));
  490. }
  491. else if EQ(l_s("isnil")) {
  492. lua_pushnumber(L, lua_isnil(L, getnum));
  493. }
  494. else if EQ(l_s("isnull")) {
  495. lua_pushnumber(L, lua_isnull(L, getnum));
  496. }
  497. else if EQ(l_s("tonumber")) {
  498. lua_pushnumber(L, lua_tonumber(L, getnum));
  499. }
  500. else if EQ(l_s("tostring")) {
  501. const l_char *s = lua_tostring(L, getnum);
  502. lua_pushstring(L, s);
  503. }
  504. else if EQ(l_s("tonumber")) {
  505. lua_pushnumber(L, lua_tonumber(L, getnum));
  506. }
  507. else if EQ(l_s("strlen")) {
  508. lua_pushnumber(L, lua_strlen(L, getnum));
  509. }
  510. else if EQ(l_s("tocfunction")) {
  511. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  512. }
  513. else if EQ(l_s("return")) {
  514. return getnum;
  515. }
  516. else if EQ(l_s("gettop")) {
  517. lua_pushnumber(L, lua_gettop(L));
  518. }
  519. else if EQ(l_s("settop")) {
  520. lua_settop(L, getnum);
  521. }
  522. else if EQ(l_s("pop")) {
  523. lua_pop(L, getnum);
  524. }
  525. else if EQ(l_s("pushnum")) {
  526. lua_pushnumber(L, getnum);
  527. }
  528. else if EQ(l_s("pushvalue")) {
  529. lua_pushvalue(L, getnum);
  530. }
  531. else if EQ(l_s("remove")) {
  532. lua_remove(L, getnum);
  533. }
  534. else if EQ(l_s("insert")) {
  535. lua_insert(L, getnum);
  536. }
  537. else if EQ(l_s("gettable")) {
  538. lua_gettable(L, getnum);
  539. }
  540. else if EQ(l_s("settable")) {
  541. lua_settable(L, getnum);
  542. }
  543. else if EQ(l_s("next")) {
  544. lua_next(L, -2);
  545. }
  546. else if EQ(l_s("concat")) {
  547. lua_concat(L, getnum);
  548. }
  549. else if EQ(l_s("lessthan")) {
  550. int a = getnum;
  551. if (lua_lessthan(L, a, getnum))
  552. lua_pushnumber(L, 1);
  553. else
  554. lua_pushnil(L);
  555. }
  556. else if EQ(l_s("rawcall")) {
  557. int narg = getnum;
  558. int nres = getnum;
  559. lua_rawcall(L, narg, nres);
  560. }
  561. else if EQ(l_s("call")) {
  562. int narg = getnum;
  563. int nres = getnum;
  564. lua_call(L, narg, nres);
  565. }
  566. else if EQ(l_s("dostring")) {
  567. lua_dostring(L, luaL_check_string(L, getnum));
  568. }
  569. else if EQ(l_s("settagmethod")) {
  570. int tag = getnum;
  571. const l_char *event = getname;
  572. lua_settagmethod(L, tag, event);
  573. }
  574. else if EQ(l_s("gettagmethod")) {
  575. int tag = getnum;
  576. const l_char *event = getname;
  577. lua_gettagmethod(L, tag, event);
  578. }
  579. else if EQ(l_s("type")) {
  580. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  581. }
  582. else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
  583. }
  584. return 0;
  585. }
  586. /* }====================================================== */
  587. static const struct luaL_reg tests_funcs[] = {
  588. {l_s("hash"), hash_query},
  589. {l_s("limits"), get_limits},
  590. {l_s("listcode"), listcode},
  591. {l_s("listk"), listk},
  592. {l_s("listlocals"), listlocals},
  593. {l_s("loadlib"), loadlib},
  594. {l_s("querystr"), string_query},
  595. {l_s("querytab"), table_query},
  596. {l_s("testC"), testC},
  597. {l_s("ref"), tref},
  598. {l_s("getref"), getref},
  599. {l_s("unref"), unref},
  600. {l_s("d2s"), d2s},
  601. {l_s("s2d"), s2d},
  602. {l_s("newuserdata"), newuserdata},
  603. {l_s("newuserdatabox"), newuserdatabox},
  604. {l_s("settag"), settag},
  605. {l_s("udataval"), udataval},
  606. {l_s("newtag"), newtag},
  607. {l_s("doonnewstack"), doonnewstack},
  608. {l_s("newstate"), newstate},
  609. {l_s("closestate"), closestate},
  610. {l_s("doremote"), doremote},
  611. {l_s("settagmethod"), settagmethod},
  612. {l_s("equal"), equal},
  613. {l_s("totalmem"), mem_query}
  614. };
  615. void luaB_opentests (lua_State *L) {
  616. *((int **)L) = &islocked; /* init lock */
  617. lua_state = L; /* keep first state to be opened */
  618. /* open lib in a new table */
  619. lua_newtable(L);
  620. lua_getglobals(L);
  621. lua_pushvalue(L, -2);
  622. lua_setglobals(L);
  623. luaL_openl(L, tests_funcs); /* open functions inside new table */
  624. lua_setglobals(L); /* restore old table of globals */
  625. lua_setglobal(L, l_s("T")); /* set new table as global T */
  626. }
  627. #endif