ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. ** $Id: ltests.c,v 1.29 2000/06/30 19:17:08 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_Object arg = lua_getparam(1);
  143. if (arg == LUA_NOOBJECT) {
  144. lua_pushnumber(memdebug_total);
  145. lua_pushnumber(memdebug_numblocks);
  146. lua_pushnumber(memdebug_maxmem);
  147. }
  148. else
  149. memdebug_memlimit = luaL_check_int(1);
  150. }
  151. static void hash_query (void) {
  152. lua_Object o = luaL_nonnullarg(1);
  153. if (lua_getparam(2) == LUA_NOOBJECT) {
  154. luaL_arg_check(ttype(o) == TAG_STRING, 1, "string expected");
  155. lua_pushnumber(tsvalue(o)->u.s.hash);
  156. }
  157. else {
  158. const Hash *t = hvalue(luaL_tablearg(2));
  159. lua_pushnumber(luaH_mainposition(t, o) - t->node);
  160. }
  161. }
  162. static void table_query (void) {
  163. const Hash *t = hvalue(luaL_tablearg(1));
  164. int i = luaL_opt_int(2, -1);
  165. if (i == -1) {
  166. lua_pushnumber(t->size);
  167. lua_pushnumber(t->firstfree - t->node);
  168. }
  169. else if (i < t->size) {
  170. luaA_pushobject(lua_state, &t->node[i].key);
  171. luaA_pushobject(lua_state, &t->node[i].val);
  172. if (t->node[i].next)
  173. lua_pushnumber(t->node[i].next - t->node);
  174. }
  175. }
  176. static void string_query (void) {
  177. lua_State *L = lua_state;
  178. stringtable *tb = (*luaL_check_string(1) == 's') ? &L->strt : &L->udt;
  179. int s = luaL_opt_int(2, 0) - 1;
  180. if (s==-1) {
  181. lua_pushnumber(tb->nuse);
  182. lua_pushnumber(tb->size);
  183. }
  184. else if (s < tb->size) {
  185. TString *ts;
  186. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  187. ttype(L->top) = TAG_STRING;
  188. tsvalue(L->top) = ts;
  189. incr_top;
  190. }
  191. }
  192. }
  193. /*
  194. ** {======================================================
  195. ** function to test the API with C. It interprets a kind of "assembler"
  196. ** language with calls to the API, so the test can be driven by Lua code
  197. ** =======================================================
  198. */
  199. static const char *const delimits = " \t\n,;";
  200. static void skip (const char **pc) {
  201. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  202. }
  203. static int getnum (const char **pc) {
  204. int res = 0;
  205. skip(pc);
  206. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  207. return res;
  208. }
  209. static int getreg (const char **pc) {
  210. skip(pc);
  211. if (*(*pc)++ != 'r') lua_error("`testC' expecting a register");
  212. return getnum(pc);
  213. }
  214. static const char *getname (const char **pc) {
  215. static char buff[30];
  216. int i = 0;
  217. skip(pc);
  218. while (**pc != '\0' && !strchr(delimits, **pc))
  219. buff[i++] = *(*pc)++;
  220. buff[i] = '\0';
  221. return buff;
  222. }
  223. #define EQ(s1) (strcmp(s1, inst) == 0)
  224. static void testC (void) {
  225. lua_Object reg[10];
  226. const char *pc = luaL_check_string(1);
  227. for (;;) {
  228. const char *inst = getname(&pc);
  229. if EQ("") return;
  230. else if EQ("pushnum") {
  231. lua_pushnumber(getnum(&pc));
  232. }
  233. else if EQ("createtable") {
  234. reg[getreg(&pc)] = lua_createtable();
  235. }
  236. else if EQ("closure") {
  237. lua_CFunction f = lua_getcfunction(lua_getglobal(getname(&pc)));
  238. lua_pushcclosure(f, getnum(&pc));
  239. }
  240. else if EQ("pop") {
  241. reg[getreg(&pc)] = lua_pop();
  242. }
  243. else if EQ("getglobal") {
  244. int n = getreg(&pc);
  245. reg[n] = lua_getglobal(getname(&pc));
  246. }
  247. else if EQ("ref") {
  248. lua_pushnumber(lua_ref(0));
  249. reg[getreg(&pc)] = lua_pop();
  250. }
  251. else if EQ("reflock") {
  252. lua_pushnumber(lua_ref(1));
  253. reg[getreg(&pc)] = lua_pop();
  254. }
  255. else if EQ("getref") {
  256. int n = getreg(&pc);
  257. reg[n] = lua_getref((int)lua_getnumber(reg[getreg(&pc)]));
  258. }
  259. else if EQ("unref") {
  260. lua_unref((int)lua_getnumber(reg[getreg(&pc)]));
  261. }
  262. else if EQ("getparam") {
  263. int n = getreg(&pc);
  264. reg[n] = lua_getparam(getnum(&pc)+1); /* skips the command itself */
  265. }
  266. else if EQ("getresult") {
  267. int n = getreg(&pc);
  268. reg[n] = lua_getparam(getnum(&pc));
  269. }
  270. else if EQ("setglobal") {
  271. lua_setglobal(getname(&pc));
  272. }
  273. else if EQ("pushglobals") {
  274. lua_pushglobaltable();
  275. }
  276. else if EQ("pushstring") {
  277. lua_pushstring(getname(&pc));
  278. }
  279. else if EQ("pushreg") {
  280. lua_pushobject(reg[getreg(&pc)]);
  281. }
  282. else if EQ("call") {
  283. if (lua_call(getname(&pc))) lua_error(NULL);
  284. }
  285. else if EQ("gettable") {
  286. reg[getreg(&pc)] = lua_gettable();
  287. }
  288. else if EQ("rawget") {
  289. reg[getreg(&pc)] = lua_rawget();
  290. }
  291. else if EQ("settable") {
  292. lua_settable();
  293. }
  294. else if EQ("rawset") {
  295. lua_rawset();
  296. }
  297. else if EQ("tag") {
  298. lua_pushnumber(lua_tag(reg[getreg(&pc)]));
  299. }
  300. else if EQ("type") {
  301. lua_pushstring(lua_type(reg[getreg(&pc)]));
  302. }
  303. else if EQ("next") {
  304. int n = getreg(&pc);
  305. n = lua_next(reg[n], (int)lua_getnumber(reg[getreg(&pc)]));
  306. lua_pushnumber(n);
  307. }
  308. else if EQ("equal") {
  309. int n1 = getreg(&pc);
  310. int n2 = getreg(&pc);
  311. lua_pushnumber(lua_equal(reg[n1], reg[n2]));
  312. }
  313. else if EQ("pushusertag") {
  314. int val = getreg(&pc);
  315. int tag = getreg(&pc);
  316. lua_pushusertag((void *)(int)lua_getnumber(reg[val]),
  317. (int)lua_getnumber(reg[tag]));
  318. }
  319. else if EQ("udataval") {
  320. int n = getreg(&pc);
  321. lua_pushnumber((int)lua_getuserdata(reg[getreg(&pc)]));
  322. reg[n] = lua_pop();
  323. }
  324. else if EQ("settagmethod") {
  325. int n = getreg(&pc);
  326. lua_settagmethod((int)lua_getnumber(reg[n]), getname(&pc));
  327. }
  328. else if EQ("beginblock") {
  329. lua_beginblock();
  330. }
  331. else if EQ("endblock") {
  332. lua_endblock();
  333. }
  334. else if EQ("newstate") {
  335. int stacksize = getnum(&pc);
  336. lua_State *L1 = lua_newstate(stacksize, getnum(&pc));
  337. if (L1)
  338. lua_pushuserdata(L1);
  339. else
  340. lua_pushnil();
  341. }
  342. else if EQ("closestate") {
  343. (lua_close)((lua_State *)lua_getuserdata(reg[getreg(&pc)]));
  344. }
  345. else if EQ("doremote") {
  346. lua_Object ol1 = reg[getreg(&pc)];
  347. lua_Object str = reg[getreg(&pc)];
  348. lua_State *L1;
  349. lua_Object temp;
  350. int status;
  351. if (!lua_isuserdata(ol1) || !lua_isstring(str))
  352. lua_error("bad arguments for `doremote'");
  353. L1 = (lua_State *)lua_getuserdata(ol1);
  354. status = (lua_dostring)(L1, lua_getstring(str));
  355. if (status != 0) {
  356. lua_pushnil();
  357. lua_pushnumber(status);
  358. }
  359. else {
  360. int i = 1;
  361. while ((temp = (lua_getresult)(L1, i++)) != LUA_NOOBJECT)
  362. lua_pushstring((lua_getstring)(L1, temp));
  363. }
  364. }
  365. #if LUA_DEPRECATETFUNCS
  366. else if EQ("rawsetglobal") {
  367. lua_rawsetglobal(getname(&pc));
  368. }
  369. else if EQ("rawgetglobal") {
  370. int n = getreg(&pc);
  371. reg[n] = lua_rawgetglobal(getname(&pc));
  372. }
  373. #endif
  374. else luaL_verror(lua_state, "unknown command in `testC': %.20s", inst);
  375. }
  376. }
  377. /* }====================================================== */
  378. static const struct luaL_reg tests_funcs[] = {
  379. {"hash", (lua_CFunction)hash_query},
  380. {"limits", (lua_CFunction)get_limits},
  381. {"listcode", (lua_CFunction)listcode},
  382. {"liststrings", (lua_CFunction)liststrings},
  383. {"listlocals", (lua_CFunction)listlocals},
  384. {"querystr", (lua_CFunction)string_query},
  385. {"querytab", (lua_CFunction)table_query},
  386. {"testC", (lua_CFunction)testC},
  387. {"totalmem", (lua_CFunction)mem_query}
  388. };
  389. void luaB_opentests (lua_State *L) {
  390. if (lua_state != NULL) return; /* do not open tests for auxiliar states */
  391. lua_state = L;
  392. luaL_openl(tests_funcs);
  393. }
  394. #endif