ltests.c 19 KB

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