ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. ** $Id: ltests.c,v 1.63 2001/02/06 16:01:29 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 "luadebug.h"
  24. #include "lualib.h"
  25. /*
  26. ** The whole module only makes sense with LUA_DEBUG on
  27. */
  28. #ifdef LUA_DEBUG
  29. lua_State *lua_state = NULL;
  30. int islocked = 0;
  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. /* ensures maximum alignment for HEADER */
  42. #define HEADER (sizeof(union L_Umaxalign))
  43. #define MARKSIZE 32
  44. #define MARK 0x55 /* 01010101 (a nice pattern) */
  45. #define blocksize(b) ((size_t *)((char *)(b) - HEADER))
  46. unsigned long memdebug_numblocks = 0;
  47. unsigned long memdebug_total = 0;
  48. unsigned long memdebug_maxmem = 0;
  49. unsigned long memdebug_memlimit = ULONG_MAX;
  50. static void *checkblock (void *block) {
  51. size_t *b = blocksize(block);
  52. size_t size = *b;
  53. int i;
  54. for (i=0;i<MARKSIZE;i++)
  55. lua_assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  56. return b;
  57. }
  58. static void freeblock (void *block) {
  59. if (block) {
  60. size_t size = *blocksize(block);
  61. block = checkblock(block);
  62. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  63. free(block); /* free original block */
  64. memdebug_numblocks--;
  65. memdebug_total -= size;
  66. }
  67. }
  68. void *debug_realloc (void *block, size_t oldsize, size_t size) {
  69. lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  70. if (size == 0) {
  71. freeblock(block);
  72. return NULL;
  73. }
  74. else if (memdebug_total+size > memdebug_memlimit)
  75. return NULL; /* to test memory allocation errors */
  76. else {
  77. char *newblock;
  78. int i;
  79. size_t realsize = HEADER+size+MARKSIZE;
  80. if (realsize < size) return NULL; /* overflow! */
  81. newblock = (char *)malloc(realsize); /* alloc a new block */
  82. if (newblock == NULL) return NULL;
  83. if (oldsize > size) oldsize = size;
  84. if (block) {
  85. memcpy(newblock+HEADER, block, oldsize);
  86. freeblock(block); /* erase (and check) old copy */
  87. }
  88. /* initialize new part of the block with something `weird' */
  89. memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
  90. memdebug_total += size;
  91. if (memdebug_total > memdebug_maxmem)
  92. memdebug_maxmem = memdebug_total;
  93. memdebug_numblocks++;
  94. *(size_t *)newblock = size;
  95. for (i=0;i<MARKSIZE;i++)
  96. *(newblock+HEADER+size+i) = (char)(MARK+i);
  97. return newblock+HEADER;
  98. }
  99. }
  100. /* }====================================================================== */
  101. /*
  102. ** {======================================================
  103. ** Disassembler
  104. ** =======================================================
  105. */
  106. static const char *const instrname[NUM_OPCODES] = {
  107. "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
  108. "PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
  109. "GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
  110. "CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
  111. "ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
  112. "JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
  113. "JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
  114. "LFORLOOP", "CLOSURE"
  115. };
  116. static void pushop (lua_State *L, Proto *p, int pc) {
  117. char buff[100];
  118. Instruction i = p->code[pc];
  119. OpCode o = GET_OPCODE(i);
  120. const char *name = instrname[o];
  121. sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
  122. switch ((enum Mode)luaK_opproperties[o].mode) {
  123. case iO:
  124. sprintf(buff+8, "%-12s", name);
  125. break;
  126. case iU:
  127. sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
  128. break;
  129. case iS:
  130. sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
  131. break;
  132. case iAB:
  133. sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
  134. break;
  135. }
  136. lua_pushstring(L, buff);
  137. }
  138. static int listcode (lua_State *L) {
  139. int pc;
  140. Proto *p;
  141. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  142. 1, "Lua function expected");
  143. p = clvalue(luaA_index(L, 1))->f.l;
  144. lua_newtable(L);
  145. setnameval(L, "maxstack", p->maxstacksize);
  146. setnameval(L, "numparams", p->numparams);
  147. for (pc=0; pc<p->sizecode; pc++) {
  148. lua_pushnumber(L, pc+1);
  149. pushop(L, p, pc);
  150. lua_settable(L, -3);
  151. }
  152. return 1;
  153. }
  154. static int liststrings (lua_State *L) {
  155. Proto *p;
  156. int i;
  157. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  158. 1, "Lua function expected");
  159. p = clvalue(luaA_index(L, 1))->f.l;
  160. lua_newtable(L);
  161. for (i=0; i<p->sizekstr; i++) {
  162. lua_pushnumber(L, i+1);
  163. lua_pushstring(L, p->kstr[i]->str);
  164. lua_settable(L, -3);
  165. }
  166. return 1;
  167. }
  168. static int listlocals (lua_State *L) {
  169. Proto *p;
  170. int pc = luaL_check_int(L, 2) - 1;
  171. int i = 0;
  172. const char *name;
  173. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  174. 1, "Lua function expected");
  175. p = clvalue(luaA_index(L, 1))->f.l;
  176. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  177. lua_pushstring(L, name);
  178. return i-1;
  179. }
  180. /* }====================================================== */
  181. static int get_limits (lua_State *L) {
  182. lua_newtable(L);
  183. setnameval(L, "BITS_INT", BITS_INT);
  184. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  185. setnameval(L, "MAXARG_A", MAXARG_A);
  186. setnameval(L, "MAXARG_B", MAXARG_B);
  187. setnameval(L, "MAXARG_S", MAXARG_S);
  188. setnameval(L, "MAXARG_U", MAXARG_U);
  189. setnameval(L, "MAXLOCALS", MAXLOCALS);
  190. setnameval(L, "MAXPARAMS", MAXPARAMS);
  191. setnameval(L, "MAXSTACK", MAXSTACK);
  192. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  193. setnameval(L, "MAXVARSLH", MAXVARSLH);
  194. setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
  195. setnameval(L, "SIZE_A", SIZE_A);
  196. setnameval(L, "SIZE_B", SIZE_B);
  197. setnameval(L, "SIZE_OP", SIZE_OP);
  198. setnameval(L, "SIZE_U", SIZE_U);
  199. return 1;
  200. }
  201. static int mem_query (lua_State *L) {
  202. if (lua_isnull(L, 1)) {
  203. lua_pushnumber(L, memdebug_total);
  204. lua_pushnumber(L, memdebug_numblocks);
  205. lua_pushnumber(L, memdebug_maxmem);
  206. return 3;
  207. }
  208. else {
  209. memdebug_memlimit = luaL_check_int(L, 1);
  210. return 0;
  211. }
  212. }
  213. static int hash_query (lua_State *L) {
  214. if (lua_isnull(L, 2)) {
  215. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
  216. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
  217. }
  218. else {
  219. Hash *t;
  220. Node n;
  221. TObject *o = luaA_index(L, 1);
  222. luaL_checktype(L, 2, LUA_TTABLE);
  223. t = hvalue(luaA_index(L, 2));
  224. setobj2key(&n, o);
  225. lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
  226. }
  227. return 1;
  228. }
  229. static int table_query (lua_State *L) {
  230. const Hash *t;
  231. int i = luaL_opt_int(L, 2, -1);
  232. luaL_checktype(L, 1, LUA_TTABLE);
  233. t = hvalue(luaA_index(L, 1));
  234. if (i == -1) {
  235. lua_pushnumber(L, t->size);
  236. lua_pushnumber(L, t->firstfree - t->node);
  237. return 2;
  238. }
  239. else if (i < t->size) {
  240. TObject o;
  241. setkey2obj(&o, &t->node[i]);
  242. luaA_pushobject(L, &o);
  243. luaA_pushobject(L, &t->node[i].val);
  244. if (t->node[i].next) {
  245. lua_pushnumber(L, t->node[i].next - t->node);
  246. return 3;
  247. }
  248. else
  249. return 2;
  250. }
  251. return 0;
  252. }
  253. static int string_query (lua_State *L) {
  254. stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &G(L)->strt :
  255. &G(L)->udt;
  256. int s = luaL_opt_int(L, 2, 0) - 1;
  257. if (s==-1) {
  258. lua_pushnumber(L ,tb->nuse);
  259. lua_pushnumber(L ,tb->size);
  260. return 2;
  261. }
  262. else if (s < tb->size) {
  263. TString *ts;
  264. int n = 0;
  265. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  266. setsvalue(L->top, ts);
  267. incr_top;
  268. n++;
  269. }
  270. return n;
  271. }
  272. return 0;
  273. }
  274. static int tref (lua_State *L) {
  275. luaL_checkany(L, 1);
  276. lua_pushvalue(L, 1);
  277. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  278. return 1;
  279. }
  280. static int getref (lua_State *L) {
  281. if (lua_getref(L, luaL_check_int(L, 1)))
  282. return 1;
  283. else
  284. return 0;
  285. }
  286. static int unref (lua_State *L) {
  287. lua_unref(L, luaL_check_int(L, 1));
  288. return 0;
  289. }
  290. static int newuserdata (lua_State *L) {
  291. if (lua_isnumber(L, 2))
  292. lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
  293. else
  294. lua_newuserdata(L, luaL_check_int(L, 1));
  295. return 1;
  296. }
  297. static int udataval (lua_State *L) {
  298. luaL_checktype(L, 1, LUA_TUSERDATA);
  299. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  300. return 1;
  301. }
  302. static int doonnewstack (lua_State *L) {
  303. lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
  304. if (L1 == NULL) return 0;
  305. *((int **)L1) = &islocked; /* initialize the lock */
  306. lua_dostring(L1, luaL_check_string(L, 2));
  307. lua_pushnumber(L, 1);
  308. lua_close(L1);
  309. return 1;
  310. }
  311. static int newstate (lua_State *L) {
  312. lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1));
  313. if (L1) {
  314. *((int **)L1) = &islocked; /* initialize the lock */
  315. lua_pushuserdata(L, L1);
  316. }
  317. else
  318. lua_pushnil(L);
  319. return 1;
  320. }
  321. static int loadlib (lua_State *L) {
  322. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  323. switch (*luaL_check_string(L, 2)) {
  324. case 'm': lua_mathlibopen(L1); break;
  325. case 's': lua_strlibopen(L1); break;
  326. case 'i': lua_iolibopen(L1); break;
  327. case 'd': lua_dblibopen(L1); break;
  328. case 'b': lua_baselibopen(L1); break;
  329. default: luaL_argerror(L, 2, "invalid option");
  330. }
  331. return 0;
  332. }
  333. static int closestate (lua_State *L) {
  334. luaL_checktype(L, 1, LUA_TUSERDATA);
  335. lua_close((lua_State *)lua_touserdata(L, 1));
  336. LUA_UNLOCK(L); /* close cannot unlock that */
  337. return 0;
  338. }
  339. static int doremote (lua_State *L) {
  340. lua_State *L1;
  341. const char *code = luaL_check_string(L, 2);
  342. int status;
  343. luaL_checktype(L, 1, LUA_TUSERDATA);
  344. L1 = (lua_State *)lua_touserdata(L, 1);
  345. status = lua_dostring(L1, code);
  346. if (status != 0) {
  347. lua_pushnil(L);
  348. lua_pushnumber(L, status);
  349. return 2;
  350. }
  351. else {
  352. int i = 0;
  353. while (!lua_isnull(L1, ++i))
  354. lua_pushstring(L, lua_tostring(L1, i));
  355. return i-1;
  356. }
  357. }
  358. static int settagmethod (lua_State *L) {
  359. int tag = luaL_check_int(L, 1);
  360. const char *event = luaL_check_string(L, 2);
  361. luaL_checkany(L, 3);
  362. lua_gettagmethod(L, tag, event);
  363. lua_pushvalue(L, 3);
  364. lua_settagmethod(L, tag, event);
  365. return 1;
  366. }
  367. static int pushbool (lua_State *L, int b) {
  368. if (b) lua_pushnumber(L, 1);
  369. else lua_pushnil(L);
  370. return 1;
  371. }
  372. static int equal (lua_State *L) {
  373. return pushbool(L, lua_equal(L, 1, 2));
  374. }
  375. /*
  376. ** {======================================================
  377. ** function to test the API with C. It interprets a kind of "assembler"
  378. ** language with calls to the API, so the test can be driven by Lua code
  379. ** =======================================================
  380. */
  381. static const char *const delimits = " \t\n,;";
  382. static void skip (const char **pc) {
  383. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  384. }
  385. static int getnum (lua_State *L, const char **pc) {
  386. int res = 0;
  387. int sig = 1;
  388. skip(pc);
  389. if (**pc == '.') {
  390. res = (int)lua_tonumber(L, -1);
  391. lua_pop(L, 1);
  392. (*pc)++;
  393. return res;
  394. }
  395. else if (**pc == '-') {
  396. sig = -1;
  397. (*pc)++;
  398. }
  399. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  400. return sig*res;
  401. }
  402. static const char *getname (char *buff, const char **pc) {
  403. int i = 0;
  404. skip(pc);
  405. while (**pc != '\0' && !strchr(delimits, **pc))
  406. buff[i++] = *(*pc)++;
  407. buff[i] = '\0';
  408. return buff;
  409. }
  410. #define EQ(s1) (strcmp(s1, inst) == 0)
  411. #define getnum ((getnum)(L, &pc))
  412. #define getname ((getname)(buff, &pc))
  413. static int testC (lua_State *L) {
  414. char buff[30];
  415. const char *pc = luaL_check_string(L, 1);
  416. for (;;) {
  417. const char *inst = getname;
  418. if EQ("") return 0;
  419. else if EQ("isnumber") {
  420. lua_pushnumber(L, lua_isnumber(L, getnum));
  421. }
  422. else if EQ("isstring") {
  423. lua_pushnumber(L, lua_isstring(L, getnum));
  424. }
  425. else if EQ("istable") {
  426. lua_pushnumber(L, lua_istable(L, getnum));
  427. }
  428. else if EQ("iscfunction") {
  429. lua_pushnumber(L, lua_iscfunction(L, getnum));
  430. }
  431. else if EQ("isfunction") {
  432. lua_pushnumber(L, lua_isfunction(L, getnum));
  433. }
  434. else if EQ("isuserdata") {
  435. lua_pushnumber(L, lua_isuserdata(L, getnum));
  436. }
  437. else if EQ("isnil") {
  438. lua_pushnumber(L, lua_isnil(L, getnum));
  439. }
  440. else if EQ("isnull") {
  441. lua_pushnumber(L, lua_isnull(L, getnum));
  442. }
  443. else if EQ("tonumber") {
  444. lua_pushnumber(L, lua_tonumber(L, getnum));
  445. }
  446. else if EQ("tostring") {
  447. lua_pushstring(L, lua_tostring(L, getnum));
  448. }
  449. else if EQ("tonumber") {
  450. lua_pushnumber(L, lua_tonumber(L, getnum));
  451. }
  452. else if EQ("strlen") {
  453. lua_pushnumber(L, lua_strlen(L, getnum));
  454. }
  455. else if EQ("tocfunction") {
  456. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  457. }
  458. else if EQ("return") {
  459. return getnum;
  460. }
  461. else if EQ("gettop") {
  462. lua_pushnumber(L, lua_gettop(L));
  463. }
  464. else if EQ("settop") {
  465. lua_settop(L, getnum);
  466. }
  467. else if EQ("pop") {
  468. lua_pop(L, getnum);
  469. }
  470. else if EQ("pushnum") {
  471. lua_pushnumber(L, getnum);
  472. }
  473. else if EQ("pushvalue") {
  474. lua_pushvalue(L, getnum);
  475. }
  476. else if EQ("remove") {
  477. lua_remove(L, getnum);
  478. }
  479. else if EQ("insert") {
  480. lua_insert(L, getnum);
  481. }
  482. else if EQ("gettable") {
  483. lua_gettable(L, getnum);
  484. }
  485. else if EQ("settable") {
  486. lua_settable(L, getnum);
  487. }
  488. else if EQ("next") {
  489. lua_next(L, -2);
  490. }
  491. else if EQ("concat") {
  492. lua_concat(L, getnum);
  493. }
  494. else if EQ("lessthan") {
  495. int a = getnum;
  496. if (lua_lessthan(L, a, getnum))
  497. lua_pushnumber(L, 1);
  498. else
  499. lua_pushnil(L);
  500. }
  501. else if EQ("rawcall") {
  502. int narg = getnum;
  503. int nres = getnum;
  504. lua_rawcall(L, narg, nres);
  505. }
  506. else if EQ("call") {
  507. int narg = getnum;
  508. int nres = getnum;
  509. lua_call(L, narg, nres);
  510. }
  511. else if EQ("dostring") {
  512. lua_dostring(L, luaL_check_string(L, getnum));
  513. }
  514. else if EQ("settagmethod") {
  515. int tag = getnum;
  516. const char *event = getname;
  517. lua_settagmethod(L, tag, event);
  518. }
  519. else if EQ("gettagmethod") {
  520. int tag = getnum;
  521. const char *event = getname;
  522. lua_gettagmethod(L, tag, event);
  523. }
  524. else if EQ("type") {
  525. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  526. }
  527. else luaL_verror(L, "unknown instruction %.30s", buff);
  528. }
  529. return 0;
  530. }
  531. /* }====================================================== */
  532. static const struct luaL_reg tests_funcs[] = {
  533. {"hash", hash_query},
  534. {"limits", get_limits},
  535. {"listcode", listcode},
  536. {"liststrings", liststrings},
  537. {"listlocals", listlocals},
  538. {"loadlib", loadlib},
  539. {"querystr", string_query},
  540. {"querytab", table_query},
  541. {"testC", testC},
  542. {"ref", tref},
  543. {"getref", getref},
  544. {"unref", unref},
  545. {"newuserdata", newuserdata},
  546. {"udataval", udataval},
  547. {"doonnewstack", doonnewstack},
  548. {"newstate", newstate},
  549. {"closestate", closestate},
  550. {"doremote", doremote},
  551. {"settagmethod", settagmethod},
  552. {"equal", equal},
  553. {"totalmem", mem_query}
  554. };
  555. void luaB_opentests (lua_State *L) {
  556. *((int **)L) = &islocked; /* init lock */
  557. lua_state = L; /* keep first state to be opened */
  558. /* open lib in a new table */
  559. lua_newtable(L);
  560. lua_getglobals(L);
  561. lua_pushvalue(L, -2);
  562. lua_setglobals(L);
  563. luaL_openl(L, tests_funcs); /* open functions inside new table */
  564. lua_setglobals(L); /* restore old table of globals */
  565. lua_setglobal(L, "T"); /* set new table as global T */
  566. }
  567. #endif