ltests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. ** $Id: ltests.c,v 1.49 2000/10/05 13:00:17 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 DEBUG on
  27. */
  28. #ifdef 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, "%s", 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. lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
  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 newstate (lua_State *L) {
  231. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  232. if (L1)
  233. lua_pushuserdata(L, L1);
  234. else
  235. lua_pushnil(L);
  236. return 1;
  237. }
  238. static int loadlib (lua_State *L) {
  239. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  240. switch (*luaL_check_string(L, 2)) {
  241. case 'm': lua_mathlibopen(L1); break;
  242. case 's': lua_strlibopen(L1); break;
  243. case 'i': lua_iolibopen(L1); break;
  244. case 'd': lua_dblibopen(L1); break;
  245. case 'b': lua_baselibopen(L1); break;
  246. default: luaL_argerror(L, 2, "invalid option");
  247. }
  248. return 0;
  249. }
  250. static int closestate (lua_State *L) {
  251. luaL_checktype(L, 1, LUA_TUSERDATA);
  252. lua_close((lua_State *)lua_touserdata(L, 1));
  253. return 0;
  254. }
  255. static int doremote (lua_State *L) {
  256. lua_State *L1;
  257. const char *code = luaL_check_string(L, 2);
  258. int status;
  259. luaL_checktype(L, 1, LUA_TUSERDATA);
  260. L1 = (lua_State *)lua_touserdata(L, 1);
  261. status = lua_dostring(L1, code);
  262. if (status != 0) {
  263. lua_pushnil(L);
  264. lua_pushnumber(L, status);
  265. return 2;
  266. }
  267. else {
  268. int i = 0;
  269. while (!lua_isnull(L1, ++i))
  270. lua_pushstring(L, lua_tostring(L1, i));
  271. return i-1;
  272. }
  273. }
  274. static int settagmethod (lua_State *L) {
  275. luaL_checkany(L, 3);
  276. lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
  277. return 1;
  278. }
  279. static int pushbool (lua_State *L, int b) {
  280. if (b) lua_pushnumber(L, 1);
  281. else lua_pushnil(L);
  282. return 1;
  283. }
  284. static int equal (lua_State *L) {
  285. return pushbool(L, lua_equal(L, 1, 2));
  286. }
  287. /*
  288. ** {======================================================
  289. ** function to test the API with C. It interprets a kind of "assembler"
  290. ** language with calls to the API, so the test can be driven by Lua code
  291. ** =======================================================
  292. */
  293. static const char *const delimits = " \t\n,;";
  294. static void skip (const char **pc) {
  295. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  296. }
  297. static int getnum (lua_State *L, const char **pc) {
  298. int res = 0;
  299. int sig = 1;
  300. skip(pc);
  301. if (**pc == '.') {
  302. res = (int)lua_tonumber(L, -1);
  303. lua_pop(L, 1);
  304. (*pc)++;
  305. return res;
  306. }
  307. else if (**pc == '-') {
  308. sig = -1;
  309. (*pc)++;
  310. }
  311. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  312. return sig*res;
  313. }
  314. static const char *getname (char *buff, const char **pc) {
  315. int i = 0;
  316. skip(pc);
  317. while (**pc != '\0' && !strchr(delimits, **pc))
  318. buff[i++] = *(*pc)++;
  319. buff[i] = '\0';
  320. return buff;
  321. }
  322. #define EQ(s1) (strcmp(s1, inst) == 0)
  323. #define getnum ((getnum)(L, &pc))
  324. #define getname ((getname)(buff, &pc))
  325. static int testC (lua_State *L) {
  326. char buff[30];
  327. const char *pc = luaL_check_string(L, 1);
  328. for (;;) {
  329. const char *inst = getname;
  330. if EQ("") return 0;
  331. else if EQ("isnumber") {
  332. lua_pushnumber(L, lua_isnumber(L, getnum));
  333. }
  334. else if EQ("isstring") {
  335. lua_pushnumber(L, lua_isstring(L, getnum));
  336. }
  337. else if EQ("istable") {
  338. lua_pushnumber(L, lua_istable(L, getnum));
  339. }
  340. else if EQ("iscfunction") {
  341. lua_pushnumber(L, lua_iscfunction(L, getnum));
  342. }
  343. else if EQ("isfunction") {
  344. lua_pushnumber(L, lua_isfunction(L, getnum));
  345. }
  346. else if EQ("isuserdata") {
  347. lua_pushnumber(L, lua_isuserdata(L, getnum));
  348. }
  349. else if EQ("isnil") {
  350. lua_pushnumber(L, lua_isnil(L, getnum));
  351. }
  352. else if EQ("isnull") {
  353. lua_pushnumber(L, lua_isnull(L, getnum));
  354. }
  355. else if EQ("tonumber") {
  356. lua_pushnumber(L, lua_tonumber(L, getnum));
  357. }
  358. else if EQ("tostring") {
  359. lua_pushstring(L, lua_tostring(L, getnum));
  360. }
  361. else if EQ("tonumber") {
  362. lua_pushnumber(L, lua_tonumber(L, getnum));
  363. }
  364. else if EQ("strlen") {
  365. lua_pushnumber(L, lua_strlen(L, getnum));
  366. }
  367. else if EQ("tocfunction") {
  368. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  369. }
  370. else if EQ("return") {
  371. return getnum;
  372. }
  373. else if EQ("gettop") {
  374. lua_pushnumber(L, lua_gettop(L));
  375. }
  376. else if EQ("settop") {
  377. lua_settop(L, getnum);
  378. }
  379. else if EQ("pop") {
  380. lua_pop(L, getnum);
  381. }
  382. else if EQ("pushnum") {
  383. lua_pushnumber(L, getnum);
  384. }
  385. else if EQ("pushvalue") {
  386. lua_pushvalue(L, getnum);
  387. }
  388. else if EQ("remove") {
  389. lua_remove(L, getnum);
  390. }
  391. else if EQ("insert") {
  392. lua_insert(L, getnum);
  393. }
  394. else if EQ("gettable") {
  395. lua_gettable(L, getnum);
  396. }
  397. else if EQ("settable") {
  398. lua_settable(L, getnum);
  399. }
  400. else if EQ("next") {
  401. lua_next(L, -2);
  402. }
  403. else if EQ("concat") {
  404. lua_concat(L, getnum);
  405. }
  406. else if EQ("rawcall") {
  407. int narg = getnum;
  408. int nres = getnum;
  409. lua_rawcall(L, narg, nres);
  410. }
  411. else if EQ("call") {
  412. int narg = getnum;
  413. int nres = getnum;
  414. lua_call(L, narg, nres);
  415. }
  416. else if EQ("dostring") {
  417. lua_dostring(L, luaL_check_string(L, getnum));
  418. }
  419. else if EQ("settagmethod") {
  420. int tag = getnum;
  421. const char *event = getname;
  422. lua_settagmethod(L, tag, event);
  423. }
  424. else if EQ("gettagmethod") {
  425. int tag = getnum;
  426. const char *event = getname;
  427. lua_gettagmethod(L, tag, event);
  428. }
  429. else if EQ("type") {
  430. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  431. }
  432. else luaL_verror(L, "unknown instruction %.30s", buff);
  433. }
  434. return 0;
  435. }
  436. /* }====================================================== */
  437. static const struct luaL_reg tests_funcs[] = {
  438. {"hash", hash_query},
  439. {"limits", get_limits},
  440. {"listcode", listcode},
  441. {"liststrings", liststrings},
  442. {"listlocals", listlocals},
  443. {"loadlib", loadlib},
  444. {"querystr", string_query},
  445. {"querytab", table_query},
  446. {"testC", testC},
  447. {"ref", tref},
  448. {"getref", getref},
  449. {"unref", unref},
  450. {"newuserdata", newuserdata},
  451. {"udataval", udataval},
  452. {"newstate", newstate},
  453. {"closestate", closestate},
  454. {"doremote", doremote},
  455. {"settagmethod", settagmethod},
  456. {"equal", equal},
  457. {"totalmem", mem_query}
  458. };
  459. void luaB_opentests (lua_State *L) {
  460. lua_newtable(L);
  461. lua_getglobals(L);
  462. lua_pushvalue(L, -2);
  463. lua_setglobals(L);
  464. luaL_openl(L, tests_funcs); /* open functions inside new table */
  465. lua_setglobals(L); /* restore old table of globals */
  466. lua_setglobal(L, "T"); /* set new table as global T */
  467. }
  468. #endif