ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. ** $Id: ltests.c,v 1.27 2000/06/26 19:28:31 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 "ldo.h"
  15. #include "lfunc.h"
  16. #include "lmem.h"
  17. #include "lopcodes.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "luadebug.h"
  22. void luaB_opentests (lua_State *L);
  23. /*
  24. ** The whole module only makes sense with DEBUG on
  25. */
  26. #ifdef DEBUG
  27. static void setnameval (lua_Object t, const char *name, int val) {
  28. lua_pushobject(t);
  29. lua_pushstring(name);
  30. lua_pushnumber(val);
  31. lua_settable();
  32. }
  33. /*
  34. ** {======================================================
  35. ** Disassembler
  36. ** =======================================================
  37. */
  38. static const char *const instrname[NUM_OPCODES] = {
  39. "END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
  40. "PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
  41. "GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
  42. "CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
  43. "ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
  44. "JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
  45. "JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
  46. "LFORLOOP", "CLOSURE"
  47. };
  48. static int pushop (Proto *p, int pc) {
  49. char buff[100];
  50. Instruction i = p->code[pc];
  51. OpCode o = GET_OPCODE(i);
  52. const char *name = instrname[o];
  53. int *line = p->lines;
  54. if (line)
  55. sprintf(buff, "%5d - ", line[pc]);
  56. else
  57. strcpy(buff, " ");
  58. switch ((enum Mode)luaK_opproperties[o].mode) {
  59. case iO:
  60. sprintf(buff+8, "%s", name);
  61. break;
  62. case iU:
  63. sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
  64. break;
  65. case iS:
  66. sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
  67. break;
  68. case iAB:
  69. sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
  70. break;
  71. }
  72. lua_pushstring(buff);
  73. return (o != OP_END);
  74. }
  75. static void listcode (void) {
  76. lua_Object o = luaL_nonnullarg(1);
  77. lua_Object t = lua_createtable();
  78. int pc;
  79. Proto *p;
  80. int res;
  81. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  82. p = clvalue(o)->f.l;
  83. setnameval(t, "maxstack", p->maxstacksize);
  84. setnameval(t, "numparams", p->numparams);
  85. pc = 0;
  86. do {
  87. lua_pushobject(t);
  88. lua_pushnumber(pc+1);
  89. res = pushop(p, pc++);
  90. lua_settable();
  91. } while (res);
  92. lua_pushobject(t);
  93. }
  94. static void liststrings (void) {
  95. lua_Object o = luaL_nonnullarg(1);
  96. lua_Object t = lua_createtable();
  97. Proto *p;
  98. int i;
  99. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  100. p = clvalue(o)->f.l;
  101. for (i=0; i<p->nkstr; i++) {
  102. lua_pushobject(t);
  103. lua_pushnumber(i+1);
  104. lua_pushstring(p->kstr[i]->str);
  105. lua_settable();
  106. }
  107. lua_pushobject(t);
  108. }
  109. static void listlocals (void) {
  110. lua_Object o = luaL_nonnullarg(1);
  111. Proto *p;
  112. int pc = luaL_check_int(2) - 1;
  113. int i = 1;
  114. const char *name;
  115. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  116. p = clvalue(o)->f.l;
  117. while ((name = luaF_getlocalname(p, i++, pc)) != NULL)
  118. lua_pushstring(name);
  119. }
  120. /* }====================================================== */
  121. static void get_limits (void) {
  122. lua_Object t = lua_createtable();
  123. setnameval(t, "BITS_INT", BITS_INT);
  124. setnameval(t, "LFPF", LFIELDS_PER_FLUSH);
  125. setnameval(t, "MAXARG_A", MAXARG_A);
  126. setnameval(t, "MAXARG_B", MAXARG_B);
  127. setnameval(t, "MAXARG_S", MAXARG_S);
  128. setnameval(t, "MAXARG_U", MAXARG_U);
  129. setnameval(t, "MAXLOCALS", MAXLOCALS);
  130. setnameval(t, "MAXPARAMS", MAXPARAMS);
  131. setnameval(t, "MAXSTACK", MAXSTACK);
  132. setnameval(t, "MAXUPVALUES", MAXUPVALUES);
  133. setnameval(t, "MAXVARSLH", MAXVARSLH);
  134. setnameval(t, "RFPF", RFIELDS_PER_FLUSH);
  135. setnameval(t, "SIZE_A", SIZE_A);
  136. setnameval(t, "SIZE_B", SIZE_B);
  137. setnameval(t, "SIZE_OP", SIZE_OP);
  138. setnameval(t, "SIZE_U", SIZE_U);
  139. lua_pushobject(t);
  140. }
  141. static void mem_query (void) {
  142. lua_pushnumber(memdebug_total);
  143. lua_pushnumber(memdebug_numblocks);
  144. lua_pushnumber(memdebug_maxmem);
  145. }
  146. static void hash_query (void) {
  147. lua_Object o = luaL_nonnullarg(1);
  148. if (lua_getparam(2) == LUA_NOOBJECT) {
  149. luaL_arg_check(ttype(o) == TAG_STRING, 1, "string expected");
  150. lua_pushnumber(tsvalue(o)->u.s.hash);
  151. }
  152. else {
  153. const Hash *t = hvalue(luaL_tablearg(2));
  154. lua_pushnumber(luaH_mainposition(t, o) - t->node);
  155. }
  156. }
  157. static void table_query (void) {
  158. const Hash *t = hvalue(luaL_tablearg(1));
  159. int i = luaL_opt_int(2, -1);
  160. if (i == -1) {
  161. lua_pushnumber(t->size);
  162. lua_pushnumber(t->firstfree - t->node);
  163. }
  164. else if (i < t->size) {
  165. luaA_pushobject(lua_state, &t->node[i].key);
  166. luaA_pushobject(lua_state, &t->node[i].val);
  167. if (t->node[i].next)
  168. lua_pushnumber(t->node[i].next - t->node);
  169. }
  170. }
  171. static void string_query (void) {
  172. lua_State *L = lua_state;
  173. stringtable *tb = (*luaL_check_string(1) == 's') ? &L->strt : &L->udt;
  174. int s = luaL_opt_int(2, 0) - 1;
  175. if (s==-1) {
  176. lua_pushnumber(tb->nuse);
  177. lua_pushnumber(tb->size);
  178. }
  179. else if (s < tb->size) {
  180. TString *ts;
  181. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  182. ttype(L->top) = TAG_STRING;
  183. tsvalue(L->top) = ts;
  184. incr_top;
  185. }
  186. }
  187. }
  188. /*
  189. ** {======================================================
  190. ** function to test the API with C. It interprets a kind of "assembler"
  191. ** language with calls to the API, so the test can be driven by Lua code
  192. ** =======================================================
  193. */
  194. static const char *const delimits = " \t\n,;";
  195. static void skip (const char **pc) {
  196. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  197. }
  198. static int getnum (const char **pc) {
  199. int res = 0;
  200. skip(pc);
  201. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  202. return res;
  203. }
  204. static int getreg (const char **pc) {
  205. skip(pc);
  206. if (*(*pc)++ != 'r') lua_error("`testC' expecting a register");
  207. return getnum(pc);
  208. }
  209. static const char *getname (const char **pc) {
  210. static char buff[30];
  211. int i = 0;
  212. skip(pc);
  213. while (**pc != '\0' && !strchr(delimits, **pc))
  214. buff[i++] = *(*pc)++;
  215. buff[i] = '\0';
  216. return buff;
  217. }
  218. #define EQ(s1) (strcmp(s1, inst) == 0)
  219. static void testC (void) {
  220. lua_Object reg[10];
  221. const char *pc = luaL_check_string(1);
  222. for (;;) {
  223. const char *inst = getname(&pc);
  224. if EQ("") return;
  225. else if EQ("pushnum") {
  226. lua_pushnumber(getnum(&pc));
  227. }
  228. else if EQ("createtable") {
  229. reg[getreg(&pc)] = lua_createtable();
  230. }
  231. else if EQ("closure") {
  232. lua_CFunction f = lua_getcfunction(lua_getglobal(getname(&pc)));
  233. lua_pushcclosure(f, getnum(&pc));
  234. }
  235. else if EQ("pop") {
  236. reg[getreg(&pc)] = lua_pop();
  237. }
  238. else if EQ("getglobal") {
  239. int n = getreg(&pc);
  240. reg[n] = lua_getglobal(getname(&pc));
  241. }
  242. else if EQ("ref") {
  243. lua_pushnumber(lua_ref(0));
  244. reg[getreg(&pc)] = lua_pop();
  245. }
  246. else if EQ("reflock") {
  247. lua_pushnumber(lua_ref(1));
  248. reg[getreg(&pc)] = lua_pop();
  249. }
  250. else if EQ("getref") {
  251. int n = getreg(&pc);
  252. reg[n] = lua_getref((int)lua_getnumber(reg[getreg(&pc)]));
  253. }
  254. else if EQ("unref") {
  255. lua_unref((int)lua_getnumber(reg[getreg(&pc)]));
  256. }
  257. else if EQ("getparam") {
  258. int n = getreg(&pc);
  259. reg[n] = lua_getparam(getnum(&pc)+1); /* skips the command itself */
  260. }
  261. else if EQ("getresult") {
  262. int n = getreg(&pc);
  263. reg[n] = lua_getparam(getnum(&pc));
  264. }
  265. else if EQ("setglobal") {
  266. lua_setglobal(getname(&pc));
  267. }
  268. else if EQ("pushglobals") {
  269. lua_pushglobaltable();
  270. }
  271. else if EQ("pushstring") {
  272. lua_pushstring(getname(&pc));
  273. }
  274. else if EQ("pushreg") {
  275. lua_pushobject(reg[getreg(&pc)]);
  276. }
  277. else if EQ("call") {
  278. if (lua_call(getname(&pc))) lua_error(NULL);
  279. }
  280. else if EQ("gettable") {
  281. reg[getreg(&pc)] = lua_gettable();
  282. }
  283. else if EQ("rawget") {
  284. reg[getreg(&pc)] = lua_rawget();
  285. }
  286. else if EQ("settable") {
  287. lua_settable();
  288. }
  289. else if EQ("rawset") {
  290. lua_rawset();
  291. }
  292. else if EQ("tag") {
  293. lua_pushnumber(lua_tag(reg[getreg(&pc)]));
  294. }
  295. else if EQ("type") {
  296. lua_pushstring(lua_type(reg[getreg(&pc)]));
  297. }
  298. else if EQ("next") {
  299. int n = getreg(&pc);
  300. n = lua_next(reg[n], (int)lua_getnumber(reg[getreg(&pc)]));
  301. lua_pushnumber(n);
  302. }
  303. else if EQ("equal") {
  304. int n1 = getreg(&pc);
  305. int n2 = getreg(&pc);
  306. lua_pushnumber(lua_equal(reg[n1], reg[n2]));
  307. }
  308. else if EQ("pushusertag") {
  309. int val = getreg(&pc);
  310. int tag = getreg(&pc);
  311. lua_pushusertag((void *)(int)lua_getnumber(reg[val]),
  312. (int)lua_getnumber(reg[tag]));
  313. }
  314. else if EQ("udataval") {
  315. int n = getreg(&pc);
  316. lua_pushnumber((int)lua_getuserdata(reg[getreg(&pc)]));
  317. reg[n] = lua_pop();
  318. }
  319. else if EQ("settagmethod") {
  320. int n = getreg(&pc);
  321. lua_settagmethod((int)lua_getnumber(reg[n]), getname(&pc));
  322. }
  323. else if EQ("beginblock") {
  324. lua_beginblock();
  325. }
  326. else if EQ("endblock") {
  327. lua_endblock();
  328. }
  329. else if EQ("newstate") {
  330. int stacksize = getnum(&pc);
  331. lua_State *L1 = lua_newstate("stack", stacksize,
  332. "builtin", getnum(&pc), NULL);
  333. lua_pushuserdata(L1);
  334. }
  335. else if EQ("closestate") {
  336. (lua_close)((lua_State *)lua_getuserdata(reg[getreg(&pc)]));
  337. }
  338. else if EQ("doremote") {
  339. lua_Object ol1 = reg[getreg(&pc)];
  340. lua_Object str = reg[getreg(&pc)];
  341. lua_State *L1;
  342. lua_Object temp;
  343. int i;
  344. if (!lua_isuserdata(ol1) || !lua_isstring(str))
  345. lua_error("bad arguments for `doremote'");
  346. L1 = (lua_State *)lua_getuserdata(ol1);
  347. (lua_dostring)(L1, lua_getstring(str));
  348. i = 1;
  349. while ((temp = (lua_getresult)(L1, i++)) != LUA_NOOBJECT)
  350. lua_pushstring((lua_getstring)(L1, temp));
  351. }
  352. #if LUA_DEPRECATETFUNCS
  353. else if EQ("rawsetglobal") {
  354. lua_rawsetglobal(getname(&pc));
  355. }
  356. else if EQ("rawgetglobal") {
  357. int n = getreg(&pc);
  358. reg[n] = lua_rawgetglobal(getname(&pc));
  359. }
  360. #endif
  361. else luaL_verror(lua_state, "unknown command in `testC': %.20s", inst);
  362. }
  363. }
  364. /* }====================================================== */
  365. static const struct luaL_reg tests_funcs[] = {
  366. {"hash", (lua_CFunction)hash_query},
  367. {"limits", (lua_CFunction)get_limits},
  368. {"listcode", (lua_CFunction)listcode},
  369. {"liststrings", (lua_CFunction)liststrings},
  370. {"listlocals", (lua_CFunction)listlocals},
  371. {"querystr", (lua_CFunction)string_query},
  372. {"querytab", (lua_CFunction)table_query},
  373. {"testC", (lua_CFunction)testC},
  374. {"totalmem", (lua_CFunction)mem_query}
  375. };
  376. void luaB_opentests (lua_State *L) {
  377. if (lua_state != NULL) return; /* do not open tests for auxiliar states */
  378. lua_state = L;
  379. luaL_openl(tests_funcs);
  380. }
  381. #endif