ltests.c 19 KB

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