ltests.c 20 KB

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