ltests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. ** $Id: ltests.c,v 1.76 2001/03/09 18:05:05 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("RETURN"),
  109. l_s("CALL"),
  110. l_s("PUSHNIL"),
  111. l_s("POP"),
  112. l_s("PUSHINT"),
  113. l_s("PUSHSTRING"),
  114. l_s("PUSHNUM"),
  115. l_s("PUSHNEGNUM"),
  116. l_s("PUSHUPVALUE"),
  117. l_s("GETLOCAL"),
  118. l_s("GETGLOBAL"),
  119. l_s("GETTABLE"),
  120. l_s("GETDOTTED"),
  121. l_s("GETINDEXED"),
  122. l_s("PUSHSELF"),
  123. l_s("CREATETABLE"),
  124. l_s("SETLOCAL"),
  125. l_s("SETGLOBAL"),
  126. l_s("SETTABLE"),
  127. l_s("SETLIST"),
  128. l_s("SETMAP"),
  129. l_s("ADD"),
  130. l_s("ADDI"),
  131. l_s("SUB"),
  132. l_s("MULT"),
  133. l_s("DIV"),
  134. l_s("POW"),
  135. l_s("CONCAT"),
  136. l_s("MINUS"),
  137. l_s("NOT"),
  138. l_s("JMPNE"),
  139. l_s("JMPEQ"),
  140. l_s("JMPLT"),
  141. l_s("JMPLE"),
  142. l_s("JMPGT"),
  143. l_s("JMPGE"),
  144. l_s("JMPT"),
  145. l_s("JMPF"),
  146. l_s("JMPONT"),
  147. l_s("JMPONF"),
  148. l_s("JMP"),
  149. l_s("PUSHNILJMP"),
  150. l_s("FORPREP"),
  151. l_s("FORLOOP"),
  152. l_s("LFORPREP"),
  153. l_s("LFORLOOP"),
  154. l_s("CLOSURE")
  155. };
  156. static void pushop (lua_State *L, Proto *p, int pc) {
  157. l_char buff[100];
  158. Instruction i = p->code[pc];
  159. OpCode o = GET_OPCODE(i);
  160. const l_char *name = instrname[o];
  161. sprintf(buff, l_s("%5d - "), luaG_getline(p->lineinfo, pc, 1, NULL));
  162. switch ((enum Mode)luaK_opproperties[o].mode) {
  163. case iO:
  164. sprintf(buff+8, l_s("%-12s"), name);
  165. break;
  166. case iU:
  167. sprintf(buff+8, l_s("%-12s%4u"), name, GETARG_U(i));
  168. break;
  169. case iS:
  170. sprintf(buff+8, l_s("%-12s%4d"), name, GETARG_S(i));
  171. break;
  172. case iAB:
  173. sprintf(buff+8, l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_B(i));
  174. break;
  175. }
  176. lua_pushstring(L, buff);
  177. }
  178. static int listcode (lua_State *L) {
  179. int pc;
  180. Proto *p;
  181. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  182. 1, l_s("Lua function expected"));
  183. p = clvalue(luaA_index(L, 1))->f.l;
  184. lua_newtable(L);
  185. setnameval(L, l_s("maxstack"), p->maxstacksize);
  186. setnameval(L, l_s("numparams"), p->numparams);
  187. for (pc=0; pc<p->sizecode; pc++) {
  188. lua_pushnumber(L, pc+1);
  189. pushop(L, p, pc);
  190. lua_settable(L, -3);
  191. }
  192. return 1;
  193. }
  194. static int liststrings (lua_State *L) {
  195. Proto *p;
  196. int i;
  197. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  198. 1, l_s("Lua function expected"));
  199. p = clvalue(luaA_index(L, 1))->f.l;
  200. lua_newtable(L);
  201. for (i=0; i<p->sizekstr; i++) {
  202. lua_pushnumber(L, i+1);
  203. lua_pushstring(L, getstr(p->kstr[i]));
  204. lua_settable(L, -3);
  205. }
  206. return 1;
  207. }
  208. static int listlocals (lua_State *L) {
  209. Proto *p;
  210. int pc = luaL_check_int(L, 2) - 1;
  211. int i = 0;
  212. const l_char *name;
  213. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  214. 1, l_s("Lua function expected"));
  215. p = clvalue(luaA_index(L, 1))->f.l;
  216. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  217. lua_pushstring(L, name);
  218. return i-1;
  219. }
  220. /* }====================================================== */
  221. static int pushbool (lua_State *L, int b) {
  222. if (b) lua_pushnumber(L, 1);
  223. else lua_pushnil(L);
  224. return 1;
  225. }
  226. static int get_limits (lua_State *L) {
  227. lua_newtable(L);
  228. setnameval(L, l_s("BITS_INT"), BITS_INT);
  229. setnameval(L, l_s("LFPF"), LFIELDS_PER_FLUSH);
  230. setnameval(L, l_s("MAXARG_A"), MAXARG_A);
  231. setnameval(L, l_s("MAXARG_B"), MAXARG_B);
  232. setnameval(L, l_s("MAXARG_S"), MAXARG_S);
  233. setnameval(L, l_s("MAXARG_U"), MAXARG_U);
  234. setnameval(L, l_s("MAXLOCALS"), MAXLOCALS);
  235. setnameval(L, l_s("MAXPARAMS"), MAXPARAMS);
  236. setnameval(L, l_s("MAXSTACK"), MAXSTACK);
  237. setnameval(L, l_s("MAXUPVALUES"), MAXUPVALUES);
  238. setnameval(L, l_s("MAXVARSLH"), MAXVARSLH);
  239. setnameval(L, l_s("RFPF"), RFIELDS_PER_FLUSH);
  240. setnameval(L, l_s("SIZE_A"), SIZE_A);
  241. setnameval(L, l_s("SIZE_B"), SIZE_B);
  242. setnameval(L, l_s("SIZE_OP"), SIZE_OP);
  243. setnameval(L, l_s("SIZE_U"), SIZE_U);
  244. return 1;
  245. }
  246. static int mem_query (lua_State *L) {
  247. if (lua_isnull(L, 1)) {
  248. lua_pushnumber(L, memdebug_total);
  249. lua_pushnumber(L, memdebug_numblocks);
  250. lua_pushnumber(L, memdebug_maxmem);
  251. return 3;
  252. }
  253. else {
  254. memdebug_memlimit = luaL_check_int(L, 1);
  255. return 0;
  256. }
  257. }
  258. static int hash_query (lua_State *L) {
  259. if (lua_isnull(L, 2)) {
  260. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, l_s("string expected"));
  261. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
  262. }
  263. else {
  264. Hash *t;
  265. Node n;
  266. TObject *o = luaA_index(L, 1);
  267. luaL_checktype(L, 2, LUA_TTABLE);
  268. t = hvalue(luaA_index(L, 2));
  269. setobj2key(&n, o);
  270. lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
  271. }
  272. return 1;
  273. }
  274. static int table_query (lua_State *L) {
  275. const Hash *t;
  276. int i = luaL_opt_int(L, 2, -1);
  277. luaL_checktype(L, 1, LUA_TTABLE);
  278. t = hvalue(luaA_index(L, 1));
  279. if (i == -1) {
  280. lua_pushnumber(L, t->size);
  281. lua_pushnumber(L, t->firstfree - t->node);
  282. return 2;
  283. }
  284. else if (i < t->size) {
  285. TObject o;
  286. setkey2obj(&o, &t->node[i]);
  287. luaA_pushobject(L, &o);
  288. luaA_pushobject(L, &t->node[i].val);
  289. if (t->node[i].next) {
  290. lua_pushnumber(L, t->node[i].next - t->node);
  291. return 3;
  292. }
  293. else
  294. return 2;
  295. }
  296. return 0;
  297. }
  298. static int string_query (lua_State *L) {
  299. stringtable *tb = (*luaL_check_string(L, 1) == l_c('s')) ? &G(L)->strt :
  300. &G(L)->udt;
  301. int s = luaL_opt_int(L, 2, 0) - 1;
  302. if (s==-1) {
  303. lua_pushnumber(L ,tb->nuse);
  304. lua_pushnumber(L ,tb->size);
  305. return 2;
  306. }
  307. else if (s < tb->size) {
  308. TString *ts;
  309. int n = 0;
  310. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  311. setsvalue(L->top, ts);
  312. incr_top;
  313. n++;
  314. }
  315. return n;
  316. }
  317. return 0;
  318. }
  319. static int tref (lua_State *L) {
  320. luaL_checkany(L, 1);
  321. lua_pushvalue(L, 1);
  322. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  323. return 1;
  324. }
  325. static int getref (lua_State *L) {
  326. if (lua_getref(L, luaL_check_int(L, 1)))
  327. return 1;
  328. else
  329. return 0;
  330. }
  331. static int unref (lua_State *L) {
  332. lua_unref(L, luaL_check_int(L, 1));
  333. return 0;
  334. }
  335. static int newuserdata (lua_State *L) {
  336. if (lua_isnumber(L, 2)) {
  337. int tag = luaL_check_int(L, 2);
  338. int res = lua_pushuserdata(L, (void *)luaL_check_int(L, 1));
  339. if (tag) lua_settag(L, tag);
  340. pushbool(L, res);
  341. return 2;
  342. }
  343. else {
  344. size_t size = luaL_check_int(L, 1);
  345. l_char *p = (l_char *)lua_newuserdata(L, size);
  346. while (size--) *p++ = l_c('\0');
  347. return 1;
  348. }
  349. }
  350. static int udataval (lua_State *L) {
  351. luaL_checktype(L, 1, LUA_TUSERDATA);
  352. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  353. return 1;
  354. }
  355. static int newtag (lua_State *L) {
  356. lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1),
  357. (int)lua_tonumber(L, 2)));
  358. return 1;
  359. }
  360. static int doonnewstack (lua_State *L) {
  361. lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
  362. if (L1 == NULL) return 0;
  363. *((int **)L1) = &islocked; /* initialize the lock */
  364. lua_dostring(L1, luaL_check_string(L, 2));
  365. lua_pushnumber(L, 1);
  366. lua_close(L1);
  367. return 1;
  368. }
  369. static int s2d (lua_State *L) {
  370. lua_pushnumber(L, *(double *)luaL_check_string(L, 1));
  371. return 1;
  372. }
  373. static int d2s (lua_State *L) {
  374. double d = luaL_check_number(L, 1);
  375. lua_pushlstring(L, (l_char *)&d, sizeof(d));
  376. return 1;
  377. }
  378. static int newstate (lua_State *L) {
  379. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  380. if (L1) {
  381. *((int **)L1) = &islocked; /* initialize the lock */
  382. lua_pushnumber(L, (unsigned long)L1);
  383. }
  384. else
  385. lua_pushnil(L);
  386. return 1;
  387. }
  388. static int loadlib (lua_State *L) {
  389. lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  390. lua_register(L1, "mathlibopen", lua_mathlibopen);
  391. lua_register(L1, "strlibopen", lua_strlibopen);
  392. lua_register(L1, "iolibopen", lua_iolibopen);
  393. lua_register(L1, "dblibopen", lua_dblibopen);
  394. lua_register(L1, "baselibopen", lua_baselibopen);
  395. return 0;
  396. }
  397. static int closestate (lua_State *L) {
  398. lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  399. lua_close(L1);
  400. lua_unlock(L); /* close cannot unlock that */
  401. return 0;
  402. }
  403. static int doremote (lua_State *L) {
  404. lua_State *L1;
  405. const l_char *code = luaL_check_string(L, 2);
  406. int status;
  407. L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
  408. status = lua_dostring(L1, code);
  409. if (status != 0) {
  410. lua_pushnil(L);
  411. lua_pushnumber(L, status);
  412. return 2;
  413. }
  414. else {
  415. int i = 0;
  416. while (!lua_isnull(L1, ++i))
  417. lua_pushstring(L, lua_tostring(L1, i));
  418. lua_pop(L1, i-1);
  419. return i-1;
  420. }
  421. }
  422. static int settagmethod (lua_State *L) {
  423. int tag = luaL_check_int(L, 1);
  424. const l_char *event = luaL_check_string(L, 2);
  425. luaL_checkany(L, 3);
  426. lua_gettagmethod(L, tag, event);
  427. lua_pushvalue(L, 3);
  428. lua_settagmethod(L, tag, event);
  429. return 1;
  430. }
  431. static int equal (lua_State *L) {
  432. return pushbool(L, lua_equal(L, 1, 2));
  433. }
  434. /*
  435. ** {======================================================
  436. ** function to test the API with C. It interprets a kind of assembler
  437. ** language with calls to the API, so the test can be driven by Lua code
  438. ** =======================================================
  439. */
  440. static const l_char *const delimits = l_s(" \t\n,;");
  441. static void skip (const l_char **pc) {
  442. while (**pc != l_c('\0') && strchr(delimits, **pc)) (*pc)++;
  443. }
  444. static int getnum (lua_State *L, const l_char **pc) {
  445. int res = 0;
  446. int sig = 1;
  447. skip(pc);
  448. if (**pc == l_c('.')) {
  449. res = (int)lua_tonumber(L, -1);
  450. lua_pop(L, 1);
  451. (*pc)++;
  452. return res;
  453. }
  454. else if (**pc == l_c('-')) {
  455. sig = -1;
  456. (*pc)++;
  457. }
  458. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - l_c('0');
  459. return sig*res;
  460. }
  461. static const l_char *getname (l_char *buff, const l_char **pc) {
  462. int i = 0;
  463. skip(pc);
  464. while (**pc != l_c('\0') && !strchr(delimits, **pc))
  465. buff[i++] = *(*pc)++;
  466. buff[i] = l_c('\0');
  467. return buff;
  468. }
  469. #define EQ(s1) (strcmp(s1, inst) == 0)
  470. #define getnum ((getnum)(L, &pc))
  471. #define getname ((getname)(buff, &pc))
  472. static int testC (lua_State *L) {
  473. l_char buff[30];
  474. const l_char *pc = luaL_check_string(L, 1);
  475. for (;;) {
  476. const l_char *inst = getname;
  477. if EQ(l_s("")) return 0;
  478. else if EQ(l_s("isnumber")) {
  479. lua_pushnumber(L, lua_isnumber(L, getnum));
  480. }
  481. else if EQ(l_s("isstring")) {
  482. lua_pushnumber(L, lua_isstring(L, getnum));
  483. }
  484. else if EQ(l_s("istable")) {
  485. lua_pushnumber(L, lua_istable(L, getnum));
  486. }
  487. else if EQ(l_s("iscfunction")) {
  488. lua_pushnumber(L, lua_iscfunction(L, getnum));
  489. }
  490. else if EQ(l_s("isfunction")) {
  491. lua_pushnumber(L, lua_isfunction(L, getnum));
  492. }
  493. else if EQ(l_s("isuserdata")) {
  494. lua_pushnumber(L, lua_isuserdata(L, getnum));
  495. }
  496. else if EQ(l_s("isnil")) {
  497. lua_pushnumber(L, lua_isnil(L, getnum));
  498. }
  499. else if EQ(l_s("isnull")) {
  500. lua_pushnumber(L, lua_isnull(L, getnum));
  501. }
  502. else if EQ(l_s("tonumber")) {
  503. lua_pushnumber(L, lua_tonumber(L, getnum));
  504. }
  505. else if EQ(l_s("tostring")) {
  506. const l_char *s = lua_tostring(L, getnum);
  507. lua_pushstring(L, s);
  508. }
  509. else if EQ(l_s("tonumber")) {
  510. lua_pushnumber(L, lua_tonumber(L, getnum));
  511. }
  512. else if EQ(l_s("strlen")) {
  513. lua_pushnumber(L, lua_strlen(L, getnum));
  514. }
  515. else if EQ(l_s("tocfunction")) {
  516. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  517. }
  518. else if EQ(l_s("return")) {
  519. return getnum;
  520. }
  521. else if EQ(l_s("gettop")) {
  522. lua_pushnumber(L, lua_gettop(L));
  523. }
  524. else if EQ(l_s("settop")) {
  525. lua_settop(L, getnum);
  526. }
  527. else if EQ(l_s("pop")) {
  528. lua_pop(L, getnum);
  529. }
  530. else if EQ(l_s("pushnum")) {
  531. lua_pushnumber(L, getnum);
  532. }
  533. else if EQ(l_s("pushvalue")) {
  534. lua_pushvalue(L, getnum);
  535. }
  536. else if EQ(l_s("remove")) {
  537. lua_remove(L, getnum);
  538. }
  539. else if EQ(l_s("insert")) {
  540. lua_insert(L, getnum);
  541. }
  542. else if EQ(l_s("gettable")) {
  543. lua_gettable(L, getnum);
  544. }
  545. else if EQ(l_s("settable")) {
  546. lua_settable(L, getnum);
  547. }
  548. else if EQ(l_s("next")) {
  549. lua_next(L, -2);
  550. }
  551. else if EQ(l_s("concat")) {
  552. lua_concat(L, getnum);
  553. }
  554. else if EQ(l_s("lessthan")) {
  555. int a = getnum;
  556. if (lua_lessthan(L, a, getnum))
  557. lua_pushnumber(L, 1);
  558. else
  559. lua_pushnil(L);
  560. }
  561. else if EQ(l_s("rawcall")) {
  562. int narg = getnum;
  563. int nres = getnum;
  564. lua_rawcall(L, narg, nres);
  565. }
  566. else if EQ(l_s("call")) {
  567. int narg = getnum;
  568. int nres = getnum;
  569. lua_call(L, narg, nres);
  570. }
  571. else if EQ(l_s("dostring")) {
  572. lua_dostring(L, luaL_check_string(L, getnum));
  573. }
  574. else if EQ(l_s("settagmethod")) {
  575. int tag = getnum;
  576. const l_char *event = getname;
  577. lua_settagmethod(L, tag, event);
  578. }
  579. else if EQ(l_s("gettagmethod")) {
  580. int tag = getnum;
  581. const l_char *event = getname;
  582. lua_gettagmethod(L, tag, event);
  583. }
  584. else if EQ(l_s("type")) {
  585. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  586. }
  587. else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
  588. }
  589. return 0;
  590. }
  591. /* }====================================================== */
  592. static const struct luaL_reg tests_funcs[] = {
  593. {l_s("hash"), hash_query},
  594. {l_s("limits"), get_limits},
  595. {l_s("listcode"), listcode},
  596. {l_s("liststrings"), liststrings},
  597. {l_s("listlocals"), listlocals},
  598. {l_s("loadlib"), loadlib},
  599. {l_s("querystr"), string_query},
  600. {l_s("querytab"), table_query},
  601. {l_s("testC"), testC},
  602. {l_s("ref"), tref},
  603. {l_s("getref"), getref},
  604. {l_s("unref"), unref},
  605. {l_s("d2s"), d2s},
  606. {l_s("s2d"), s2d},
  607. {l_s("newuserdata"), newuserdata},
  608. {l_s("udataval"), udataval},
  609. {l_s("newtag"), newtag},
  610. {l_s("doonnewstack"), doonnewstack},
  611. {l_s("newstate"), newstate},
  612. {l_s("closestate"), closestate},
  613. {l_s("doremote"), doremote},
  614. {l_s("settagmethod"), settagmethod},
  615. {l_s("equal"), equal},
  616. {l_s("totalmem"), mem_query}
  617. };
  618. void luaB_opentests (lua_State *L) {
  619. *((int **)L) = &islocked; /* init lock */
  620. lua_state = L; /* keep first state to be opened */
  621. /* open lib in a new table */
  622. lua_newtable(L);
  623. lua_getglobals(L);
  624. lua_pushvalue(L, -2);
  625. lua_setglobals(L);
  626. luaL_openl(L, tests_funcs); /* open functions inside new table */
  627. lua_setglobals(L); /* restore old table of globals */
  628. lua_setglobal(L, l_s("T")); /* set new table as global T */
  629. }
  630. #endif