ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. ** $Id: ltests.c,v 1.41 2000/09/11 19:42:57 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_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
  77. p = clvalue(luaA_index(L, 1))->f.l;
  78. lua_newtable(L);
  79. setnameval(L, "maxstack", p->maxstacksize);
  80. setnameval(L, "numparams", p->numparams);
  81. pc = 0;
  82. do {
  83. lua_pushnumber(L, pc+1);
  84. res = pushop(L, p, pc++);
  85. lua_settable(L, -3);
  86. } while (res);
  87. return 1;
  88. }
  89. static int liststrings (lua_State *L) {
  90. Proto *p;
  91. int i;
  92. luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
  93. p = clvalue(luaA_index(L, 1))->f.l;
  94. lua_newtable(L);
  95. for (i=0; i<p->nkstr; i++) {
  96. lua_pushnumber(L, i+1);
  97. lua_pushstring(L, p->kstr[i]->str);
  98. lua_settable(L, -3);
  99. }
  100. return 1;
  101. }
  102. static int listlocals (lua_State *L) {
  103. Proto *p;
  104. int pc = luaL_check_int(L, 2) - 1;
  105. int i = 0;
  106. const char *name;
  107. luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 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) == TAG_STRING, 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, "table");
  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, "table");
  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') ? &L->strt : &L->udt;
  183. int s = luaL_opt_int(L, 2, 0) - 1;
  184. if (s==-1) {
  185. lua_pushnumber(L ,tb->nuse);
  186. lua_pushnumber(L ,tb->size);
  187. return 2;
  188. }
  189. else if (s < tb->size) {
  190. TString *ts;
  191. int n = 0;
  192. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  193. ttype(L->top) = TAG_STRING;
  194. tsvalue(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_checktype(L, 1, "any");
  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. lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
  220. return 1;
  221. }
  222. static int udataval (lua_State *L) {
  223. luaL_checktype(L, 1, "userdata");
  224. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  225. return 1;
  226. }
  227. static int newstate (lua_State *L) {
  228. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  229. if (L1)
  230. lua_pushuserdata(L, L1);
  231. else
  232. lua_pushnil(L);
  233. return 1;
  234. }
  235. static int loadlib (lua_State *L) {
  236. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  237. switch (*luaL_check_string(L, 2)) {
  238. case 'm': lua_mathlibopen(L1); break;
  239. case 's': lua_strlibopen(L1); break;
  240. case 'i': lua_iolibopen(L1); break;
  241. case 'd': lua_dblibopen(L1); break;
  242. case 'b': lua_baselibopen(L1); break;
  243. default: luaL_argerror(L, 2, "invalid option");
  244. }
  245. return 0;
  246. }
  247. static int closestate (lua_State *L) {
  248. luaL_checktype(L, 1, "userdata");
  249. lua_close((lua_State *)lua_touserdata(L, 1));
  250. return 0;
  251. }
  252. static int doremote (lua_State *L) {
  253. lua_State *L1;
  254. const char *code = luaL_check_string(L, 2);
  255. int status;
  256. luaL_checktype(L, 1, "userdata");
  257. L1 = (lua_State *)lua_touserdata(L, 1);
  258. status = lua_dostring(L1, code);
  259. if (status != 0) {
  260. lua_pushnil(L);
  261. lua_pushnumber(L, status);
  262. return 2;
  263. }
  264. else {
  265. int i = 0;
  266. while (!lua_isnull(L1, ++i))
  267. lua_pushstring(L, lua_tostring(L1, i));
  268. return i-1;
  269. }
  270. }
  271. static int settagmethod (lua_State *L) {
  272. luaL_checktype(L, 3, "any");
  273. lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
  274. return 1;
  275. }
  276. static int pushbool (lua_State *L, int b) {
  277. if (b) lua_pushnumber(L, 1);
  278. else lua_pushnil(L);
  279. return 1;
  280. }
  281. static int equal (lua_State *L) {
  282. return pushbool(L, lua_equal(L, 1, 2));
  283. }
  284. /*
  285. ** {======================================================
  286. ** function to test the API with C. It interprets a kind of "assembler"
  287. ** language with calls to the API, so the test can be driven by Lua code
  288. ** =======================================================
  289. */
  290. static const char *const delimits = " \t\n,;";
  291. static void skip (const char **pc) {
  292. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  293. }
  294. static int getnum (lua_State *L, const char **pc) {
  295. int res = 0;
  296. int sig = 1;
  297. skip(pc);
  298. if (**pc == '.') {
  299. res = (int)lua_tonumber(L, -1);
  300. lua_pop(L, 1);
  301. (*pc)++;
  302. return res;
  303. }
  304. else if (**pc == '-') {
  305. sig = -1;
  306. (*pc)++;
  307. }
  308. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  309. return sig*res;
  310. }
  311. static const char *getname (char *buff, const char **pc) {
  312. int i = 0;
  313. skip(pc);
  314. while (**pc != '\0' && !strchr(delimits, **pc))
  315. buff[i++] = *(*pc)++;
  316. buff[i] = '\0';
  317. return buff;
  318. }
  319. #define EQ(s1) (strcmp(s1, inst) == 0)
  320. #define getnum ((getnum)(L, &pc))
  321. #define getname ((getname)(buff, &pc))
  322. static int testC (lua_State *L) {
  323. char buff[30];
  324. const char *pc = luaL_check_string(L, 1);
  325. for (;;) {
  326. const char *inst = getname;
  327. if EQ("") return 0;
  328. else if EQ("return") {
  329. return getnum;
  330. }
  331. else if EQ("gettop") {
  332. lua_pushnumber(L, lua_gettop(L));
  333. }
  334. else if EQ("settop") {
  335. lua_settop(L, getnum);
  336. }
  337. else if EQ("pop") {
  338. lua_pop(L, getnum);
  339. }
  340. else if EQ("pushnum") {
  341. lua_pushnumber(L, getnum);
  342. }
  343. else if EQ("pushvalue") {
  344. lua_pushvalue(L, getnum);
  345. }
  346. else if EQ("remove") {
  347. lua_remove(L, getnum);
  348. }
  349. else if EQ("insert") {
  350. lua_insert(L, getnum);
  351. }
  352. else if EQ("next") {
  353. lua_next(L, -2);
  354. }
  355. else if EQ("concat") {
  356. lua_concat(L, getnum);
  357. }
  358. else if EQ("call") {
  359. int narg = getnum;
  360. int nres = getnum;
  361. lua_rawcall(L, narg, nres);
  362. }
  363. else if EQ("type") {
  364. lua_pushstring(L, lua_type(L, getnum));
  365. }
  366. else luaL_verror(L, "unknown instruction %.30s", buff);
  367. }
  368. return 0;
  369. }
  370. /* }====================================================== */
  371. static const struct luaL_reg tests_funcs[] = {
  372. {"hash", hash_query},
  373. {"limits", get_limits},
  374. {"listcode", listcode},
  375. {"liststrings", liststrings},
  376. {"listlocals", listlocals},
  377. {"loadlib", loadlib},
  378. {"querystr", string_query},
  379. {"querytab", table_query},
  380. {"testC", testC},
  381. {"ref", tref},
  382. {"getref", getref},
  383. {"unref", unref},
  384. {"newuserdata", newuserdata},
  385. {"udataval", udataval},
  386. {"newstate", newstate},
  387. {"closestate", closestate},
  388. {"doremote", doremote},
  389. {"settagmethod", settagmethod},
  390. {"equal", equal},
  391. {"totalmem", mem_query}
  392. };
  393. void luaB_opentests (lua_State *L) {
  394. lua_newtable(L);
  395. lua_getglobals(L);
  396. lua_pushvalue(L, -2);
  397. lua_setglobals(L);
  398. luaL_openl(L, tests_funcs); /* open functions inside new table */
  399. lua_setglobals(L); /* restore old table of globals */
  400. lua_setglobal(L, "T"); /* set new table as global T */
  401. }
  402. #endif