ltests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. ** $Id: ltests.c,v 1.53 2000/10/30 16:29:59 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 <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "lauxlib.h"
  13. #include "lcode.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lmem.h"
  18. #include "lopcodes.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "luadebug.h"
  23. #include "lualib.h"
  24. void luaB_opentests (lua_State *L);
  25. /*
  26. ** The whole module only makes sense with LUA_DEBUG on
  27. */
  28. #ifdef LUA_DEBUG
  29. static void setnameval (lua_State *L, const char *name, int val) {
  30. lua_pushstring(L, name);
  31. lua_pushnumber(L, val);
  32. lua_settable(L, -3);
  33. }
  34. /*
  35. ** {======================================================
  36. ** Disassembler
  37. ** =======================================================
  38. */
  39. static const char *const instrname[NUM_OPCODES] = {
  40. "END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
  41. "PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
  42. "GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
  43. "CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
  44. "ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
  45. "JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
  46. "JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
  47. "LFORLOOP", "CLOSURE"
  48. };
  49. static int pushop (lua_State *L, Proto *p, int pc) {
  50. char buff[100];
  51. Instruction i = p->code[pc];
  52. OpCode o = GET_OPCODE(i);
  53. const char *name = instrname[o];
  54. sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
  55. switch ((enum Mode)luaK_opproperties[o].mode) {
  56. case iO:
  57. sprintf(buff+8, "%-12s", name);
  58. break;
  59. case iU:
  60. sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
  61. break;
  62. case iS:
  63. sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
  64. break;
  65. case iAB:
  66. sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
  67. break;
  68. }
  69. lua_pushstring(L, buff);
  70. return (o != OP_END);
  71. }
  72. static int listcode (lua_State *L) {
  73. int pc;
  74. Proto *p;
  75. int res;
  76. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  77. 1, "Lua function expected");
  78. p = clvalue(luaA_index(L, 1))->f.l;
  79. lua_newtable(L);
  80. setnameval(L, "maxstack", p->maxstacksize);
  81. setnameval(L, "numparams", p->numparams);
  82. pc = 0;
  83. do {
  84. lua_pushnumber(L, pc+1);
  85. res = pushop(L, p, pc++);
  86. lua_settable(L, -3);
  87. } while (res);
  88. return 1;
  89. }
  90. static int liststrings (lua_State *L) {
  91. Proto *p;
  92. int i;
  93. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  94. 1, "Lua function expected");
  95. p = clvalue(luaA_index(L, 1))->f.l;
  96. lua_newtable(L);
  97. for (i=0; i<p->nkstr; i++) {
  98. lua_pushnumber(L, i+1);
  99. lua_pushstring(L, p->kstr[i]->str);
  100. lua_settable(L, -3);
  101. }
  102. return 1;
  103. }
  104. static int listlocals (lua_State *L) {
  105. Proto *p;
  106. int pc = luaL_check_int(L, 2) - 1;
  107. int i = 0;
  108. const char *name;
  109. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  110. 1, "Lua function expected");
  111. p = clvalue(luaA_index(L, 1))->f.l;
  112. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  113. lua_pushstring(L, name);
  114. return i-1;
  115. }
  116. /* }====================================================== */
  117. static int get_limits (lua_State *L) {
  118. lua_newtable(L);
  119. setnameval(L, "BITS_INT", BITS_INT);
  120. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  121. setnameval(L, "MAXARG_A", MAXARG_A);
  122. setnameval(L, "MAXARG_B", MAXARG_B);
  123. setnameval(L, "MAXARG_S", MAXARG_S);
  124. setnameval(L, "MAXARG_U", MAXARG_U);
  125. setnameval(L, "MAXLOCALS", MAXLOCALS);
  126. setnameval(L, "MAXPARAMS", MAXPARAMS);
  127. setnameval(L, "MAXSTACK", MAXSTACK);
  128. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  129. setnameval(L, "MAXVARSLH", MAXVARSLH);
  130. setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
  131. setnameval(L, "SIZE_A", SIZE_A);
  132. setnameval(L, "SIZE_B", SIZE_B);
  133. setnameval(L, "SIZE_OP", SIZE_OP);
  134. setnameval(L, "SIZE_U", SIZE_U);
  135. return 1;
  136. }
  137. static int mem_query (lua_State *L) {
  138. if (lua_isnull(L, 1)) {
  139. lua_pushnumber(L, memdebug_total);
  140. lua_pushnumber(L, memdebug_numblocks);
  141. lua_pushnumber(L, memdebug_maxmem);
  142. return 3;
  143. }
  144. else {
  145. memdebug_memlimit = luaL_check_int(L, 1);
  146. return 0;
  147. }
  148. }
  149. static int hash_query (lua_State *L) {
  150. if (lua_isnull(L, 2)) {
  151. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
  152. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
  153. }
  154. else {
  155. Hash *t;
  156. luaL_checktype(L, 2, LUA_TTABLE);
  157. t = hvalue(luaA_index(L, 2));
  158. lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
  159. }
  160. return 1;
  161. }
  162. static int table_query (lua_State *L) {
  163. const Hash *t;
  164. int i = luaL_opt_int(L, 2, -1);
  165. luaL_checktype(L, 1, LUA_TTABLE);
  166. t = hvalue(luaA_index(L, 1));
  167. if (i == -1) {
  168. lua_pushnumber(L, t->size);
  169. lua_pushnumber(L, t->firstfree - t->node);
  170. return 2;
  171. }
  172. else if (i < t->size) {
  173. luaA_pushobject(L, &t->node[i].key);
  174. luaA_pushobject(L, &t->node[i].val);
  175. if (t->node[i].next) {
  176. lua_pushnumber(L, t->node[i].next - t->node);
  177. return 3;
  178. }
  179. else
  180. return 2;
  181. }
  182. return 0;
  183. }
  184. static int string_query (lua_State *L) {
  185. stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
  186. int s = luaL_opt_int(L, 2, 0) - 1;
  187. if (s==-1) {
  188. lua_pushnumber(L ,tb->nuse);
  189. lua_pushnumber(L ,tb->size);
  190. return 2;
  191. }
  192. else if (s < tb->size) {
  193. TString *ts;
  194. int n = 0;
  195. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  196. ttype(L->top) = LUA_TSTRING;
  197. tsvalue(L->top) = ts;
  198. incr_top;
  199. n++;
  200. }
  201. return n;
  202. }
  203. return 0;
  204. }
  205. static int tref (lua_State *L) {
  206. luaL_checkany(L, 1);
  207. lua_pushvalue(L, 1);
  208. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  209. return 1;
  210. }
  211. static int getref (lua_State *L) {
  212. if (lua_getref(L, luaL_check_int(L, 1)))
  213. return 1;
  214. else
  215. return 0;
  216. }
  217. static int unref (lua_State *L) {
  218. lua_unref(L, luaL_check_int(L, 1));
  219. return 0;
  220. }
  221. static int newuserdata (lua_State *L) {
  222. if (lua_isnumber(L, 2))
  223. lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
  224. else
  225. lua_newuserdata(L, luaL_check_int(L, 1));
  226. return 1;
  227. }
  228. static int udataval (lua_State *L) {
  229. luaL_checktype(L, 1, LUA_TUSERDATA);
  230. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  231. return 1;
  232. }
  233. static int newstate (lua_State *L) {
  234. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  235. if (L1)
  236. lua_pushuserdata(L, L1);
  237. else
  238. lua_pushnil(L);
  239. return 1;
  240. }
  241. static int loadlib (lua_State *L) {
  242. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  243. switch (*luaL_check_string(L, 2)) {
  244. case 'm': lua_mathlibopen(L1); break;
  245. case 's': lua_strlibopen(L1); break;
  246. case 'i': lua_iolibopen(L1); break;
  247. case 'd': lua_dblibopen(L1); break;
  248. case 'b': lua_baselibopen(L1); break;
  249. default: luaL_argerror(L, 2, "invalid option");
  250. }
  251. return 0;
  252. }
  253. static int closestate (lua_State *L) {
  254. luaL_checktype(L, 1, LUA_TUSERDATA);
  255. lua_close((lua_State *)lua_touserdata(L, 1));
  256. return 0;
  257. }
  258. static int doremote (lua_State *L) {
  259. lua_State *L1;
  260. const char *code = luaL_check_string(L, 2);
  261. int status;
  262. luaL_checktype(L, 1, LUA_TUSERDATA);
  263. L1 = (lua_State *)lua_touserdata(L, 1);
  264. status = lua_dostring(L1, code);
  265. if (status != 0) {
  266. lua_pushnil(L);
  267. lua_pushnumber(L, status);
  268. return 2;
  269. }
  270. else {
  271. int i = 0;
  272. while (!lua_isnull(L1, ++i))
  273. lua_pushstring(L, lua_tostring(L1, i));
  274. return i-1;
  275. }
  276. }
  277. static int settagmethod (lua_State *L) {
  278. int tag = luaL_check_int(L, 1);
  279. const char *event = luaL_check_string(L, 2);
  280. luaL_checkany(L, 3);
  281. lua_gettagmethod(L, tag, event);
  282. lua_pushvalue(L, 3);
  283. lua_settagmethod(L, tag, event);
  284. return 1;
  285. }
  286. static int pushbool (lua_State *L, int b) {
  287. if (b) lua_pushnumber(L, 1);
  288. else lua_pushnil(L);
  289. return 1;
  290. }
  291. static int equal (lua_State *L) {
  292. return pushbool(L, lua_equal(L, 1, 2));
  293. }
  294. /*
  295. ** {======================================================
  296. ** function to test the API with C. It interprets a kind of "assembler"
  297. ** language with calls to the API, so the test can be driven by Lua code
  298. ** =======================================================
  299. */
  300. static const char *const delimits = " \t\n,;";
  301. static void skip (const char **pc) {
  302. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  303. }
  304. static int getnum (lua_State *L, const char **pc) {
  305. int res = 0;
  306. int sig = 1;
  307. skip(pc);
  308. if (**pc == '.') {
  309. res = (int)lua_tonumber(L, -1);
  310. lua_pop(L, 1);
  311. (*pc)++;
  312. return res;
  313. }
  314. else if (**pc == '-') {
  315. sig = -1;
  316. (*pc)++;
  317. }
  318. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  319. return sig*res;
  320. }
  321. static const char *getname (char *buff, const char **pc) {
  322. int i = 0;
  323. skip(pc);
  324. while (**pc != '\0' && !strchr(delimits, **pc))
  325. buff[i++] = *(*pc)++;
  326. buff[i] = '\0';
  327. return buff;
  328. }
  329. #define EQ(s1) (strcmp(s1, inst) == 0)
  330. #define getnum ((getnum)(L, &pc))
  331. #define getname ((getname)(buff, &pc))
  332. static int testC (lua_State *L) {
  333. char buff[30];
  334. const char *pc = luaL_check_string(L, 1);
  335. for (;;) {
  336. const char *inst = getname;
  337. if EQ("") return 0;
  338. else if EQ("isnumber") {
  339. lua_pushnumber(L, lua_isnumber(L, getnum));
  340. }
  341. else if EQ("isstring") {
  342. lua_pushnumber(L, lua_isstring(L, getnum));
  343. }
  344. else if EQ("istable") {
  345. lua_pushnumber(L, lua_istable(L, getnum));
  346. }
  347. else if EQ("iscfunction") {
  348. lua_pushnumber(L, lua_iscfunction(L, getnum));
  349. }
  350. else if EQ("isfunction") {
  351. lua_pushnumber(L, lua_isfunction(L, getnum));
  352. }
  353. else if EQ("isuserdata") {
  354. lua_pushnumber(L, lua_isuserdata(L, getnum));
  355. }
  356. else if EQ("isnil") {
  357. lua_pushnumber(L, lua_isnil(L, getnum));
  358. }
  359. else if EQ("isnull") {
  360. lua_pushnumber(L, lua_isnull(L, getnum));
  361. }
  362. else if EQ("tonumber") {
  363. lua_pushnumber(L, lua_tonumber(L, getnum));
  364. }
  365. else if EQ("tostring") {
  366. lua_pushstring(L, lua_tostring(L, getnum));
  367. }
  368. else if EQ("tonumber") {
  369. lua_pushnumber(L, lua_tonumber(L, getnum));
  370. }
  371. else if EQ("strlen") {
  372. lua_pushnumber(L, lua_strlen(L, getnum));
  373. }
  374. else if EQ("tocfunction") {
  375. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  376. }
  377. else if EQ("return") {
  378. return getnum;
  379. }
  380. else if EQ("gettop") {
  381. lua_pushnumber(L, lua_gettop(L));
  382. }
  383. else if EQ("settop") {
  384. lua_settop(L, getnum);
  385. }
  386. else if EQ("pop") {
  387. lua_pop(L, getnum);
  388. }
  389. else if EQ("pushnum") {
  390. lua_pushnumber(L, getnum);
  391. }
  392. else if EQ("pushvalue") {
  393. lua_pushvalue(L, getnum);
  394. }
  395. else if EQ("remove") {
  396. lua_remove(L, getnum);
  397. }
  398. else if EQ("insert") {
  399. lua_insert(L, getnum);
  400. }
  401. else if EQ("gettable") {
  402. lua_gettable(L, getnum);
  403. }
  404. else if EQ("settable") {
  405. lua_settable(L, getnum);
  406. }
  407. else if EQ("next") {
  408. lua_next(L, -2);
  409. }
  410. else if EQ("concat") {
  411. lua_concat(L, getnum);
  412. }
  413. else if EQ("rawcall") {
  414. int narg = getnum;
  415. int nres = getnum;
  416. lua_rawcall(L, narg, nres);
  417. }
  418. else if EQ("call") {
  419. int narg = getnum;
  420. int nres = getnum;
  421. lua_call(L, narg, nres);
  422. }
  423. else if EQ("dostring") {
  424. lua_dostring(L, luaL_check_string(L, getnum));
  425. }
  426. else if EQ("settagmethod") {
  427. int tag = getnum;
  428. const char *event = getname;
  429. lua_settagmethod(L, tag, event);
  430. }
  431. else if EQ("gettagmethod") {
  432. int tag = getnum;
  433. const char *event = getname;
  434. lua_gettagmethod(L, tag, event);
  435. }
  436. else if EQ("type") {
  437. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  438. }
  439. else luaL_verror(L, "unknown instruction %.30s", buff);
  440. }
  441. return 0;
  442. }
  443. /* }====================================================== */
  444. static const struct luaL_reg tests_funcs[] = {
  445. {"hash", hash_query},
  446. {"limits", get_limits},
  447. {"listcode", listcode},
  448. {"liststrings", liststrings},
  449. {"listlocals", listlocals},
  450. {"loadlib", loadlib},
  451. {"querystr", string_query},
  452. {"querytab", table_query},
  453. {"testC", testC},
  454. {"ref", tref},
  455. {"getref", getref},
  456. {"unref", unref},
  457. {"newuserdata", newuserdata},
  458. {"udataval", udataval},
  459. {"newstate", newstate},
  460. {"closestate", closestate},
  461. {"doremote", doremote},
  462. {"settagmethod", settagmethod},
  463. {"equal", equal},
  464. {"totalmem", mem_query}
  465. };
  466. void luaB_opentests (lua_State *L) {
  467. lua_newtable(L);
  468. lua_getglobals(L);
  469. lua_pushvalue(L, -2);
  470. lua_setglobals(L);
  471. luaL_openl(L, tests_funcs); /* open functions inside new table */
  472. lua_setglobals(L); /* restore old table of globals */
  473. lua_setglobal(L, "T"); /* set new table as global T */
  474. }
  475. #endif