ltests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. ** $Id: ltests.c,v 1.58 2001/01/19 13:20:30 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. "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 void 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. }
  71. static int listcode (lua_State *L) {
  72. int pc;
  73. Proto *p;
  74. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  75. 1, "Lua function expected");
  76. p = clvalue(luaA_index(L, 1))->f.l;
  77. lua_newtable(L);
  78. setnameval(L, "maxstack", p->maxstacksize);
  79. setnameval(L, "numparams", p->numparams);
  80. for (pc=0; pc<p->sizecode; pc++) {
  81. lua_pushnumber(L, pc+1);
  82. pushop(L, p, pc);
  83. lua_settable(L, -3);
  84. }
  85. return 1;
  86. }
  87. static int liststrings (lua_State *L) {
  88. Proto *p;
  89. int i;
  90. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  91. 1, "Lua function expected");
  92. p = clvalue(luaA_index(L, 1))->f.l;
  93. lua_newtable(L);
  94. for (i=0; i<p->sizekstr; i++) {
  95. lua_pushnumber(L, i+1);
  96. lua_pushstring(L, p->kstr[i]->str);
  97. lua_settable(L, -3);
  98. }
  99. return 1;
  100. }
  101. static int listlocals (lua_State *L) {
  102. Proto *p;
  103. int pc = luaL_check_int(L, 2) - 1;
  104. int i = 0;
  105. const char *name;
  106. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  107. 1, "Lua function expected");
  108. p = clvalue(luaA_index(L, 1))->f.l;
  109. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  110. lua_pushstring(L, name);
  111. return i-1;
  112. }
  113. /* }====================================================== */
  114. static int get_limits (lua_State *L) {
  115. lua_newtable(L);
  116. setnameval(L, "BITS_INT", BITS_INT);
  117. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  118. setnameval(L, "MAXARG_A", MAXARG_A);
  119. setnameval(L, "MAXARG_B", MAXARG_B);
  120. setnameval(L, "MAXARG_S", MAXARG_S);
  121. setnameval(L, "MAXARG_U", MAXARG_U);
  122. setnameval(L, "MAXLOCALS", MAXLOCALS);
  123. setnameval(L, "MAXPARAMS", MAXPARAMS);
  124. setnameval(L, "MAXSTACK", MAXSTACK);
  125. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  126. setnameval(L, "MAXVARSLH", MAXVARSLH);
  127. setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
  128. setnameval(L, "SIZE_A", SIZE_A);
  129. setnameval(L, "SIZE_B", SIZE_B);
  130. setnameval(L, "SIZE_OP", SIZE_OP);
  131. setnameval(L, "SIZE_U", SIZE_U);
  132. return 1;
  133. }
  134. static int mem_query (lua_State *L) {
  135. if (lua_isnull(L, 1)) {
  136. lua_pushnumber(L, memdebug_total);
  137. lua_pushnumber(L, memdebug_numblocks);
  138. lua_pushnumber(L, memdebug_maxmem);
  139. return 3;
  140. }
  141. else {
  142. memdebug_memlimit = luaL_check_int(L, 1);
  143. return 0;
  144. }
  145. }
  146. static int hash_query (lua_State *L) {
  147. if (lua_isnull(L, 2)) {
  148. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
  149. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
  150. }
  151. else {
  152. Hash *t;
  153. luaL_checktype(L, 2, LUA_TTABLE);
  154. t = hvalue(luaA_index(L, 2));
  155. lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
  156. }
  157. return 1;
  158. }
  159. static int table_query (lua_State *L) {
  160. const Hash *t;
  161. int i = luaL_opt_int(L, 2, -1);
  162. luaL_checktype(L, 1, LUA_TTABLE);
  163. t = hvalue(luaA_index(L, 1));
  164. if (i == -1) {
  165. lua_pushnumber(L, t->size);
  166. lua_pushnumber(L, t->firstfree - t->node);
  167. return 2;
  168. }
  169. else if (i < t->size) {
  170. luaA_pushobject(L, &t->node[i].key);
  171. luaA_pushobject(L, &t->node[i].val);
  172. if (t->node[i].next) {
  173. lua_pushnumber(L, t->node[i].next - t->node);
  174. return 3;
  175. }
  176. else
  177. return 2;
  178. }
  179. return 0;
  180. }
  181. static int string_query (lua_State *L) {
  182. stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &G(L)->strt :
  183. &G(L)->udt;
  184. int s = luaL_opt_int(L, 2, 0) - 1;
  185. if (s==-1) {
  186. lua_pushnumber(L ,tb->nuse);
  187. lua_pushnumber(L ,tb->size);
  188. return 2;
  189. }
  190. else if (s < tb->size) {
  191. TString *ts;
  192. int n = 0;
  193. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  194. setsvalue(L->top, ts);
  195. incr_top;
  196. n++;
  197. }
  198. return n;
  199. }
  200. return 0;
  201. }
  202. static int tref (lua_State *L) {
  203. luaL_checkany(L, 1);
  204. lua_pushvalue(L, 1);
  205. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  206. return 1;
  207. }
  208. static int getref (lua_State *L) {
  209. if (lua_getref(L, luaL_check_int(L, 1)))
  210. return 1;
  211. else
  212. return 0;
  213. }
  214. static int unref (lua_State *L) {
  215. lua_unref(L, luaL_check_int(L, 1));
  216. return 0;
  217. }
  218. static int newuserdata (lua_State *L) {
  219. if (lua_isnumber(L, 2))
  220. lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
  221. else
  222. lua_newuserdata(L, luaL_check_int(L, 1));
  223. return 1;
  224. }
  225. static int udataval (lua_State *L) {
  226. luaL_checktype(L, 1, LUA_TUSERDATA);
  227. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  228. return 1;
  229. }
  230. static int doonnewstack (lua_State *L) {
  231. lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
  232. if (L1 == NULL) return 0;
  233. lua_dostring(L1, luaL_check_string(L, 2));
  234. lua_pushnumber(L, 1);
  235. lua_close(L1);
  236. return 1;
  237. }
  238. static int newstate (lua_State *L) {
  239. lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1));
  240. if (L1)
  241. lua_pushuserdata(L, L1);
  242. else
  243. lua_pushnil(L);
  244. return 1;
  245. }
  246. static int loadlib (lua_State *L) {
  247. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  248. switch (*luaL_check_string(L, 2)) {
  249. case 'm': lua_mathlibopen(L1); break;
  250. case 's': lua_strlibopen(L1); break;
  251. case 'i': lua_iolibopen(L1); break;
  252. case 'd': lua_dblibopen(L1); break;
  253. case 'b': lua_baselibopen(L1); break;
  254. default: luaL_argerror(L, 2, "invalid option");
  255. }
  256. return 0;
  257. }
  258. static int closestate (lua_State *L) {
  259. luaL_checktype(L, 1, LUA_TUSERDATA);
  260. lua_close((lua_State *)lua_touserdata(L, 1));
  261. return 0;
  262. }
  263. static int doremote (lua_State *L) {
  264. lua_State *L1;
  265. const char *code = luaL_check_string(L, 2);
  266. int status;
  267. luaL_checktype(L, 1, LUA_TUSERDATA);
  268. L1 = (lua_State *)lua_touserdata(L, 1);
  269. status = lua_dostring(L1, code);
  270. if (status != 0) {
  271. lua_pushnil(L);
  272. lua_pushnumber(L, status);
  273. return 2;
  274. }
  275. else {
  276. int i = 0;
  277. while (!lua_isnull(L1, ++i))
  278. lua_pushstring(L, lua_tostring(L1, i));
  279. return i-1;
  280. }
  281. }
  282. static int settagmethod (lua_State *L) {
  283. int tag = luaL_check_int(L, 1);
  284. const char *event = luaL_check_string(L, 2);
  285. luaL_checkany(L, 3);
  286. lua_gettagmethod(L, tag, event);
  287. lua_pushvalue(L, 3);
  288. lua_settagmethod(L, tag, event);
  289. return 1;
  290. }
  291. static int pushbool (lua_State *L, int b) {
  292. if (b) lua_pushnumber(L, 1);
  293. else lua_pushnil(L);
  294. return 1;
  295. }
  296. static int equal (lua_State *L) {
  297. return pushbool(L, lua_equal(L, 1, 2));
  298. }
  299. /*
  300. ** {======================================================
  301. ** function to test the API with C. It interprets a kind of "assembler"
  302. ** language with calls to the API, so the test can be driven by Lua code
  303. ** =======================================================
  304. */
  305. static const char *const delimits = " \t\n,;";
  306. static void skip (const char **pc) {
  307. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  308. }
  309. static int getnum (lua_State *L, const char **pc) {
  310. int res = 0;
  311. int sig = 1;
  312. skip(pc);
  313. if (**pc == '.') {
  314. res = (int)lua_tonumber(L, -1);
  315. lua_pop(L, 1);
  316. (*pc)++;
  317. return res;
  318. }
  319. else if (**pc == '-') {
  320. sig = -1;
  321. (*pc)++;
  322. }
  323. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  324. return sig*res;
  325. }
  326. static const char *getname (char *buff, const char **pc) {
  327. int i = 0;
  328. skip(pc);
  329. while (**pc != '\0' && !strchr(delimits, **pc))
  330. buff[i++] = *(*pc)++;
  331. buff[i] = '\0';
  332. return buff;
  333. }
  334. #define EQ(s1) (strcmp(s1, inst) == 0)
  335. #define getnum ((getnum)(L, &pc))
  336. #define getname ((getname)(buff, &pc))
  337. static int testC (lua_State *L) {
  338. char buff[30];
  339. const char *pc = luaL_check_string(L, 1);
  340. for (;;) {
  341. const char *inst = getname;
  342. if EQ("") return 0;
  343. else if EQ("isnumber") {
  344. lua_pushnumber(L, lua_isnumber(L, getnum));
  345. }
  346. else if EQ("isstring") {
  347. lua_pushnumber(L, lua_isstring(L, getnum));
  348. }
  349. else if EQ("istable") {
  350. lua_pushnumber(L, lua_istable(L, getnum));
  351. }
  352. else if EQ("iscfunction") {
  353. lua_pushnumber(L, lua_iscfunction(L, getnum));
  354. }
  355. else if EQ("isfunction") {
  356. lua_pushnumber(L, lua_isfunction(L, getnum));
  357. }
  358. else if EQ("isuserdata") {
  359. lua_pushnumber(L, lua_isuserdata(L, getnum));
  360. }
  361. else if EQ("isnil") {
  362. lua_pushnumber(L, lua_isnil(L, getnum));
  363. }
  364. else if EQ("isnull") {
  365. lua_pushnumber(L, lua_isnull(L, getnum));
  366. }
  367. else if EQ("tonumber") {
  368. lua_pushnumber(L, lua_tonumber(L, getnum));
  369. }
  370. else if EQ("tostring") {
  371. lua_pushstring(L, lua_tostring(L, getnum));
  372. }
  373. else if EQ("tonumber") {
  374. lua_pushnumber(L, lua_tonumber(L, getnum));
  375. }
  376. else if EQ("strlen") {
  377. lua_pushnumber(L, lua_strlen(L, getnum));
  378. }
  379. else if EQ("tocfunction") {
  380. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  381. }
  382. else if EQ("return") {
  383. return getnum;
  384. }
  385. else if EQ("gettop") {
  386. lua_pushnumber(L, lua_gettop(L));
  387. }
  388. else if EQ("settop") {
  389. lua_settop(L, getnum);
  390. }
  391. else if EQ("pop") {
  392. lua_pop(L, getnum);
  393. }
  394. else if EQ("pushnum") {
  395. lua_pushnumber(L, getnum);
  396. }
  397. else if EQ("pushvalue") {
  398. lua_pushvalue(L, getnum);
  399. }
  400. else if EQ("remove") {
  401. lua_remove(L, getnum);
  402. }
  403. else if EQ("insert") {
  404. lua_insert(L, getnum);
  405. }
  406. else if EQ("gettable") {
  407. lua_gettable(L, getnum);
  408. }
  409. else if EQ("settable") {
  410. lua_settable(L, getnum);
  411. }
  412. else if EQ("next") {
  413. lua_next(L, -2);
  414. }
  415. else if EQ("concat") {
  416. lua_concat(L, getnum);
  417. }
  418. else if EQ("rawcall") {
  419. int narg = getnum;
  420. int nres = getnum;
  421. lua_rawcall(L, narg, nres);
  422. }
  423. else if EQ("call") {
  424. int narg = getnum;
  425. int nres = getnum;
  426. lua_call(L, narg, nres);
  427. }
  428. else if EQ("dostring") {
  429. lua_dostring(L, luaL_check_string(L, getnum));
  430. }
  431. else if EQ("settagmethod") {
  432. int tag = getnum;
  433. const char *event = getname;
  434. lua_settagmethod(L, tag, event);
  435. }
  436. else if EQ("gettagmethod") {
  437. int tag = getnum;
  438. const char *event = getname;
  439. lua_gettagmethod(L, tag, event);
  440. }
  441. else if EQ("type") {
  442. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  443. }
  444. else luaL_verror(L, "unknown instruction %.30s", buff);
  445. }
  446. return 0;
  447. }
  448. /* }====================================================== */
  449. static const struct luaL_reg tests_funcs[] = {
  450. {"hash", hash_query},
  451. {"limits", get_limits},
  452. {"listcode", listcode},
  453. {"liststrings", liststrings},
  454. {"listlocals", listlocals},
  455. {"loadlib", loadlib},
  456. {"querystr", string_query},
  457. {"querytab", table_query},
  458. {"testC", testC},
  459. {"ref", tref},
  460. {"getref", getref},
  461. {"unref", unref},
  462. {"newuserdata", newuserdata},
  463. {"udataval", udataval},
  464. {"doonnewstack", doonnewstack},
  465. {"newstate", newstate},
  466. {"closestate", closestate},
  467. {"doremote", doremote},
  468. {"settagmethod", settagmethod},
  469. {"equal", equal},
  470. {"totalmem", mem_query}
  471. };
  472. void luaB_opentests (lua_State *L) {
  473. lua_newtable(L);
  474. lua_getglobals(L);
  475. lua_pushvalue(L, -2);
  476. lua_setglobals(L);
  477. luaL_openl(L, tests_funcs); /* open functions inside new table */
  478. lua_setglobals(L); /* restore old table of globals */
  479. lua_setglobal(L, "T"); /* set new table as global T */
  480. }
  481. #endif