ltests.c 20 KB

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