ltests.c 11 KB

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