ltests.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. ** $Id: ltests.c,v 1.154 2003/02/27 12:33:07 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 func_at(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(func_at(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(func_at(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(func_at(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(func_at(L, 1))->tsv.hash);
  221. }
  222. else {
  223. TObject *o = func_at(L, 1);
  224. Table *t;
  225. luaL_checktype(L, 2, LUA_TTABLE);
  226. t = hvalue(func_at(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(func_at(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 upvalue (lua_State *L) {
  326. int n = luaL_checkint(L, 2);
  327. luaL_checktype(L, 1, LUA_TFUNCTION);
  328. if (lua_isnone(L, 3)) {
  329. const char *name = lua_getupvalue(L, 1, n);
  330. if (name == NULL) return 0;
  331. lua_pushstring(L, name);
  332. return 2;
  333. }
  334. else {
  335. const char *name = lua_setupvalue(L, 1, n);
  336. lua_pushstring(L, name);
  337. return 1;
  338. }
  339. }
  340. static int newuserdata (lua_State *L) {
  341. size_t size = luaL_checkint(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_pushlightuserdata(L, cast(void *, luaL_checkint(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_checklstring(L, 1, &l);
  358. int status = luaL_loadbuffer(L1, s, l, s);
  359. if (status == 0)
  360. status = lua_pcall(L1, 0, 0, 0);
  361. lua_pushnumber(L, status);
  362. return 1;
  363. }
  364. static int s2d (lua_State *L) {
  365. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  366. return 1;
  367. }
  368. static int d2s (lua_State *L) {
  369. double d = luaL_checknumber(L, 1);
  370. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  371. return 1;
  372. }
  373. static int newstate (lua_State *L) {
  374. lua_State *L1 = lua_open();
  375. if (L1) {
  376. lua_userstateopen(L1); /* init 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. static const luaL_reg libs[] = {
  385. {"mathlibopen", luaopen_math},
  386. {"strlibopen", luaopen_string},
  387. {"iolibopen", luaopen_io},
  388. {"tablibopen", luaopen_table},
  389. {"dblibopen", luaopen_debug},
  390. {"baselibopen", luaopen_base},
  391. {NULL, NULL}
  392. };
  393. lua_State *L1 = cast(lua_State *,
  394. cast(unsigned long, luaL_checknumber(L, 1)));
  395. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  396. luaL_openlib(L1, NULL, libs, 0);
  397. return 0;
  398. }
  399. static int closestate (lua_State *L) {
  400. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  401. lua_close(L1);
  402. lua_unlock(L); /* close cannot unlock that */
  403. return 0;
  404. }
  405. static int doremote (lua_State *L) {
  406. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  407. size_t lcode;
  408. const char *code = luaL_checklstring(L, 2, &lcode);
  409. int status;
  410. lua_settop(L1, 0);
  411. status = luaL_loadbuffer(L1, code, lcode, code);
  412. if (status == 0)
  413. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  414. if (status != 0) {
  415. lua_pushnil(L);
  416. lua_pushnumber(L, status);
  417. lua_pushstring(L, lua_tostring(L1, -1));
  418. return 3;
  419. }
  420. else {
  421. int i = 0;
  422. while (!lua_isnone(L1, ++i))
  423. lua_pushstring(L, lua_tostring(L1, i));
  424. lua_pop(L1, i-1);
  425. return i-1;
  426. }
  427. }
  428. static int log2_aux (lua_State *L) {
  429. lua_pushnumber(L, luaO_log2(luaL_checkint(L, 1)));
  430. return 1;
  431. }
  432. static int int2fb_aux (lua_State *L) {
  433. int b = luaO_int2fb(luaL_checkint(L, 1));
  434. lua_pushnumber(L, b);
  435. lua_pushnumber(L, fb2int(b));
  436. return 2;
  437. }
  438. static int test_do (lua_State *L) {
  439. const char *p = luaL_checkstring(L, 1);
  440. if (*p == '@')
  441. lua_dofile(L, p+1);
  442. else
  443. lua_dostring(L, p);
  444. return lua_gettop(L);
  445. }
  446. /*
  447. ** {======================================================
  448. ** function to test the API with C. It interprets a kind of assembler
  449. ** language with calls to the API, so the test can be driven by Lua code
  450. ** =======================================================
  451. */
  452. static const char *const delimits = " \t\n,;";
  453. static void skip (const char **pc) {
  454. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  455. }
  456. static int getnum_aux (lua_State *L, const char **pc) {
  457. int res = 0;
  458. int sig = 1;
  459. skip(pc);
  460. if (**pc == '.') {
  461. res = cast(int, lua_tonumber(L, -1));
  462. lua_pop(L, 1);
  463. (*pc)++;
  464. return res;
  465. }
  466. else if (**pc == '-') {
  467. sig = -1;
  468. (*pc)++;
  469. }
  470. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  471. return sig*res;
  472. }
  473. static const char *getname_aux (char *buff, const char **pc) {
  474. int i = 0;
  475. skip(pc);
  476. while (**pc != '\0' && !strchr(delimits, **pc))
  477. buff[i++] = *(*pc)++;
  478. buff[i] = '\0';
  479. return buff;
  480. }
  481. #define EQ(s1) (strcmp(s1, inst) == 0)
  482. #define getnum (getnum_aux(L, &pc))
  483. #define getname (getname_aux(buff, &pc))
  484. static int testC (lua_State *L) {
  485. char buff[30];
  486. const char *pc = luaL_checkstring(L, 1);
  487. for (;;) {
  488. const char *inst = getname;
  489. if EQ("") return 0;
  490. else if EQ("isnumber") {
  491. lua_pushnumber(L, lua_isnumber(L, getnum));
  492. }
  493. else if EQ("isstring") {
  494. lua_pushnumber(L, lua_isstring(L, getnum));
  495. }
  496. else if EQ("istable") {
  497. lua_pushnumber(L, lua_istable(L, getnum));
  498. }
  499. else if EQ("iscfunction") {
  500. lua_pushnumber(L, lua_iscfunction(L, getnum));
  501. }
  502. else if EQ("isfunction") {
  503. lua_pushnumber(L, lua_isfunction(L, getnum));
  504. }
  505. else if EQ("isuserdata") {
  506. lua_pushnumber(L, lua_isuserdata(L, getnum));
  507. }
  508. else if EQ("isudataval") {
  509. lua_pushnumber(L, lua_islightuserdata(L, getnum));
  510. }
  511. else if EQ("isnil") {
  512. lua_pushnumber(L, lua_isnil(L, getnum));
  513. }
  514. else if EQ("isnull") {
  515. lua_pushnumber(L, lua_isnone(L, getnum));
  516. }
  517. else if EQ("tonumber") {
  518. lua_pushnumber(L, lua_tonumber(L, getnum));
  519. }
  520. else if EQ("tostring") {
  521. const char *s = lua_tostring(L, getnum);
  522. lua_pushstring(L, s);
  523. }
  524. else if EQ("tonumber") {
  525. lua_pushnumber(L, lua_tonumber(L, getnum));
  526. }
  527. else if EQ("strlen") {
  528. lua_pushnumber(L, lua_strlen(L, getnum));
  529. }
  530. else if EQ("tocfunction") {
  531. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  532. }
  533. else if EQ("return") {
  534. return getnum;
  535. }
  536. else if EQ("gettop") {
  537. lua_pushnumber(L, lua_gettop(L));
  538. }
  539. else if EQ("settop") {
  540. lua_settop(L, getnum);
  541. }
  542. else if EQ("pop") {
  543. lua_pop(L, getnum);
  544. }
  545. else if EQ("pushnum") {
  546. lua_pushnumber(L, getnum);
  547. }
  548. else if EQ("pushnil") {
  549. lua_pushnil(L);
  550. }
  551. else if EQ("pushbool") {
  552. lua_pushboolean(L, getnum);
  553. }
  554. else if EQ("tobool") {
  555. lua_pushnumber(L, lua_toboolean(L, getnum));
  556. }
  557. else if EQ("pushvalue") {
  558. lua_pushvalue(L, getnum);
  559. }
  560. else if EQ("pushcclosure") {
  561. lua_pushcclosure(L, testC, getnum);
  562. }
  563. else if EQ("pushupvalues") {
  564. lua_pushupvalues(L);
  565. }
  566. else if EQ("remove") {
  567. lua_remove(L, getnum);
  568. }
  569. else if EQ("insert") {
  570. lua_insert(L, getnum);
  571. }
  572. else if EQ("replace") {
  573. lua_replace(L, getnum);
  574. }
  575. else if EQ("gettable") {
  576. lua_gettable(L, getnum);
  577. }
  578. else if EQ("settable") {
  579. lua_settable(L, getnum);
  580. }
  581. else if EQ("next") {
  582. lua_next(L, -2);
  583. }
  584. else if EQ("concat") {
  585. lua_concat(L, getnum);
  586. }
  587. else if EQ("lessthan") {
  588. int a = getnum;
  589. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  590. }
  591. else if EQ("equal") {
  592. int a = getnum;
  593. lua_pushboolean(L, lua_equal(L, a, getnum));
  594. }
  595. else if EQ("rawcall") {
  596. int narg = getnum;
  597. int nres = getnum;
  598. lua_call(L, narg, nres);
  599. }
  600. else if EQ("call") {
  601. int narg = getnum;
  602. int nres = getnum;
  603. lua_pcall(L, narg, nres, 0);
  604. }
  605. else if EQ("loadstring") {
  606. size_t sl;
  607. const char *s = luaL_checklstring(L, getnum, &sl);
  608. luaL_loadbuffer(L, s, sl, s);
  609. }
  610. else if EQ("loadfile") {
  611. luaL_loadfile(L, luaL_checkstring(L, getnum));
  612. }
  613. else if EQ("setmetatable") {
  614. lua_setmetatable(L, getnum);
  615. }
  616. else if EQ("getmetatable") {
  617. if (lua_getmetatable(L, getnum) == 0)
  618. lua_pushnil(L);
  619. }
  620. else if EQ("type") {
  621. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  622. }
  623. else luaL_error(L, "unknown instruction %s", buff);
  624. }
  625. return 0;
  626. }
  627. /* }====================================================== */
  628. /*
  629. ** {======================================================
  630. ** tests for yield inside hooks
  631. ** =======================================================
  632. */
  633. static void yieldf (lua_State *L, lua_Debug *ar) {
  634. lua_yield(L, 0);
  635. }
  636. static int setyhook (lua_State *L) {
  637. if (lua_isnoneornil(L, 1))
  638. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  639. else {
  640. const char *smask = luaL_checkstring(L, 1);
  641. int count = luaL_optint(L, 2, 0);
  642. int mask = 0;
  643. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  644. if (count > 0) mask |= LUA_MASKCOUNT;
  645. lua_sethook(L, yieldf, mask, count);
  646. }
  647. return 0;
  648. }
  649. static int coresume (lua_State *L) {
  650. int status;
  651. lua_State *co = lua_tothread(L, 1);
  652. luaL_argcheck(L, co, 1, "coroutine expected");
  653. status = lua_resume(co, 0);
  654. if (status != 0) {
  655. lua_pushboolean(L, 0);
  656. lua_insert(L, -2);
  657. return 2; /* return false + error message */
  658. }
  659. else {
  660. lua_pushboolean(L, 1);
  661. return 1;
  662. }
  663. }
  664. /* }====================================================== */
  665. static const struct luaL_reg tests_funcs[] = {
  666. {"hash", hash_query},
  667. {"limits", get_limits},
  668. {"listcode", listcode},
  669. {"listk", listk},
  670. {"listlocals", listlocals},
  671. {"loadlib", loadlib},
  672. {"stacklevel", stacklevel},
  673. {"querystr", string_query},
  674. {"querytab", table_query},
  675. {"doit", test_do},
  676. {"testC", testC},
  677. {"ref", tref},
  678. {"getref", getref},
  679. {"unref", unref},
  680. {"d2s", d2s},
  681. {"s2d", s2d},
  682. {"metatable", metatable},
  683. {"upvalue", upvalue},
  684. {"newuserdata", newuserdata},
  685. {"pushuserdata", pushuserdata},
  686. {"udataval", udataval},
  687. {"doonnewstack", doonnewstack},
  688. {"newstate", newstate},
  689. {"closestate", closestate},
  690. {"doremote", doremote},
  691. {"log2", log2_aux},
  692. {"int2fb", int2fb_aux},
  693. {"totalmem", mem_query},
  694. {"resume", coresume},
  695. {"setyhook", setyhook},
  696. {NULL, NULL}
  697. };
  698. static void fim (void) {
  699. if (!islocked)
  700. lua_close(lua_state);
  701. lua_assert(memdebug_numblocks == 0);
  702. lua_assert(memdebug_total == 0);
  703. }
  704. static int l_panic (lua_State *L) {
  705. UNUSED(L);
  706. fprintf(stderr, "unable to recover; exiting\n");
  707. return 0;
  708. }
  709. int luaB_opentests (lua_State *L) {
  710. lua_atpanic(L, l_panic);
  711. lua_userstateopen(L); /* init lock */
  712. lua_state = L; /* keep first state to be opened */
  713. luaL_openlib(L, "T", tests_funcs, 0);
  714. atexit(fim);
  715. return 0;
  716. }
  717. #undef main
  718. int main (int argc, char *argv[]) {
  719. char *limit = getenv("MEMLIMIT");
  720. if (limit)
  721. memdebug_memlimit = strtoul(limit, NULL, 10);
  722. l_main(argc, argv);
  723. return 0;
  724. }
  725. #endif