ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. ** $Id: ltests.c,v 1.31 2000/08/08 18:26:05 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. void luaB_opentests (lua_State *L);
  24. /*
  25. ** The whole module only makes sense with DEBUG on
  26. */
  27. #ifdef DEBUG
  28. static void setnameval (lua_Object t, const char *name, int val) {
  29. lua_pushobject(t);
  30. lua_pushstring(name);
  31. lua_pushnumber(val);
  32. lua_settable();
  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 (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(buff);
  70. return (o != OP_END);
  71. }
  72. static void listcode (void) {
  73. lua_Object o = luaL_nonnullarg(1);
  74. lua_Object t = lua_createtable();
  75. int pc;
  76. Proto *p;
  77. int res;
  78. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  79. p = clvalue(o)->f.l;
  80. setnameval(t, "maxstack", p->maxstacksize);
  81. setnameval(t, "numparams", p->numparams);
  82. pc = 0;
  83. do {
  84. lua_pushobject(t);
  85. lua_pushnumber(pc+1);
  86. res = pushop(p, pc++);
  87. lua_settable();
  88. } while (res);
  89. lua_pushobject(t);
  90. }
  91. static void liststrings (void) {
  92. lua_Object o = luaL_nonnullarg(1);
  93. lua_Object t = lua_createtable();
  94. Proto *p;
  95. int i;
  96. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  97. p = clvalue(o)->f.l;
  98. for (i=0; i<p->nkstr; i++) {
  99. lua_pushobject(t);
  100. lua_pushnumber(i+1);
  101. lua_pushstring(p->kstr[i]->str);
  102. lua_settable();
  103. }
  104. lua_pushobject(t);
  105. }
  106. static void listlocals (void) {
  107. lua_Object o = luaL_nonnullarg(1);
  108. Proto *p;
  109. int pc = luaL_check_int(2) - 1;
  110. int i = 1;
  111. const char *name;
  112. luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  113. p = clvalue(o)->f.l;
  114. while ((name = luaF_getlocalname(p, i++, pc)) != NULL)
  115. lua_pushstring(name);
  116. }
  117. /* }====================================================== */
  118. static void get_limits (void) {
  119. lua_Object t = lua_createtable();
  120. setnameval(t, "BITS_INT", BITS_INT);
  121. setnameval(t, "LFPF", LFIELDS_PER_FLUSH);
  122. setnameval(t, "MAXARG_A", MAXARG_A);
  123. setnameval(t, "MAXARG_B", MAXARG_B);
  124. setnameval(t, "MAXARG_S", MAXARG_S);
  125. setnameval(t, "MAXARG_U", MAXARG_U);
  126. setnameval(t, "MAXLOCALS", MAXLOCALS);
  127. setnameval(t, "MAXPARAMS", MAXPARAMS);
  128. setnameval(t, "MAXSTACK", MAXSTACK);
  129. setnameval(t, "MAXUPVALUES", MAXUPVALUES);
  130. setnameval(t, "MAXVARSLH", MAXVARSLH);
  131. setnameval(t, "RFPF", RFIELDS_PER_FLUSH);
  132. setnameval(t, "SIZE_A", SIZE_A);
  133. setnameval(t, "SIZE_B", SIZE_B);
  134. setnameval(t, "SIZE_OP", SIZE_OP);
  135. setnameval(t, "SIZE_U", SIZE_U);
  136. lua_pushobject(t);
  137. }
  138. static void mem_query (void) {
  139. lua_Object arg = lua_getparam(1);
  140. if (arg == LUA_NOOBJECT) {
  141. lua_pushnumber(memdebug_total);
  142. lua_pushnumber(memdebug_numblocks);
  143. lua_pushnumber(memdebug_maxmem);
  144. }
  145. else
  146. memdebug_memlimit = luaL_check_int(1);
  147. }
  148. static void hash_query (void) {
  149. lua_Object o = luaL_nonnullarg(1);
  150. if (lua_getparam(2) == LUA_NOOBJECT) {
  151. luaL_arg_check(ttype(o) == TAG_STRING, 1, "string expected");
  152. lua_pushnumber(tsvalue(o)->u.s.hash);
  153. }
  154. else {
  155. const Hash *t = hvalue(luaL_tablearg(2));
  156. lua_pushnumber(luaH_mainposition(t, o) - t->node);
  157. }
  158. }
  159. static void table_query (void) {
  160. const Hash *t = hvalue(luaL_tablearg(1));
  161. int i = luaL_opt_int(2, -1);
  162. if (i == -1) {
  163. lua_pushnumber(t->size);
  164. lua_pushnumber(t->firstfree - t->node);
  165. }
  166. else if (i < t->size) {
  167. luaA_pushobject(lua_state, &t->node[i].key);
  168. luaA_pushobject(lua_state, &t->node[i].val);
  169. if (t->node[i].next)
  170. lua_pushnumber(t->node[i].next - t->node);
  171. }
  172. }
  173. static void string_query (void) {
  174. lua_State *L = lua_state;
  175. stringtable *tb = (*luaL_check_string(1) == 's') ? &L->strt : &L->udt;
  176. int s = luaL_opt_int(2, 0) - 1;
  177. if (s==-1) {
  178. lua_pushnumber(tb->nuse);
  179. lua_pushnumber(tb->size);
  180. }
  181. else if (s < tb->size) {
  182. TString *ts;
  183. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  184. ttype(L->top) = TAG_STRING;
  185. tsvalue(L->top) = ts;
  186. incr_top;
  187. }
  188. }
  189. }
  190. /*
  191. ** {======================================================
  192. ** function to test the API with C. It interprets a kind of "assembler"
  193. ** language with calls to the API, so the test can be driven by Lua code
  194. ** =======================================================
  195. */
  196. static const char *const delimits = " \t\n,;";
  197. static void skip (const char **pc) {
  198. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  199. }
  200. static int getnum (const char **pc) {
  201. int res = 0;
  202. skip(pc);
  203. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  204. return res;
  205. }
  206. static int getreg (const char **pc) {
  207. skip(pc);
  208. if (*(*pc)++ != 'r') lua_error("`testC' expecting a register");
  209. return getnum(pc);
  210. }
  211. static const char *getname (const char **pc) {
  212. static char buff[30];
  213. int i = 0;
  214. skip(pc);
  215. while (**pc != '\0' && !strchr(delimits, **pc))
  216. buff[i++] = *(*pc)++;
  217. buff[i] = '\0';
  218. return buff;
  219. }
  220. #define EQ(s1) (strcmp(s1, inst) == 0)
  221. static void testC (void) {
  222. lua_Object reg[10];
  223. const char *pc = luaL_check_string(1);
  224. for (;;) {
  225. const char *inst = getname(&pc);
  226. if EQ("") return;
  227. else if EQ("pushnum") {
  228. lua_pushnumber(getnum(&pc));
  229. }
  230. else if EQ("createtable") {
  231. reg[getreg(&pc)] = lua_createtable();
  232. }
  233. else if EQ("closure") {
  234. lua_CFunction f = lua_getcfunction(lua_getglobal(getname(&pc)));
  235. lua_pushcclosure(f, getnum(&pc));
  236. }
  237. else if EQ("pop") {
  238. reg[getreg(&pc)] = lua_pop();
  239. }
  240. else if EQ("getglobal") {
  241. int n = getreg(&pc);
  242. reg[n] = lua_getglobal(getname(&pc));
  243. }
  244. else if EQ("ref") {
  245. lua_pushnumber(lua_ref(0));
  246. reg[getreg(&pc)] = lua_pop();
  247. }
  248. else if EQ("reflock") {
  249. lua_pushnumber(lua_ref(1));
  250. reg[getreg(&pc)] = lua_pop();
  251. }
  252. else if EQ("getref") {
  253. int n = getreg(&pc);
  254. reg[n] = lua_getref((int)lua_getnumber(reg[getreg(&pc)]));
  255. }
  256. else if EQ("unref") {
  257. lua_unref((int)lua_getnumber(reg[getreg(&pc)]));
  258. }
  259. else if EQ("getparam") {
  260. int n = getreg(&pc);
  261. reg[n] = lua_getparam(getnum(&pc)+1); /* skips the command itself */
  262. }
  263. else if EQ("getresult") {
  264. int n = getreg(&pc);
  265. reg[n] = lua_getparam(getnum(&pc));
  266. }
  267. else if EQ("setglobal") {
  268. lua_setglobal(getname(&pc));
  269. }
  270. else if EQ("pushglobals") {
  271. lua_pushglobaltable();
  272. }
  273. else if EQ("pushstring") {
  274. lua_pushstring(getname(&pc));
  275. }
  276. else if EQ("pushreg") {
  277. lua_pushobject(reg[getreg(&pc)]);
  278. }
  279. else if EQ("call") {
  280. if (lua_call(getname(&pc))) lua_error(NULL);
  281. }
  282. else if EQ("gettable") {
  283. reg[getreg(&pc)] = lua_gettable();
  284. }
  285. else if EQ("rawget") {
  286. reg[getreg(&pc)] = lua_rawget();
  287. }
  288. else if EQ("settable") {
  289. lua_settable();
  290. }
  291. else if EQ("rawset") {
  292. lua_rawset();
  293. }
  294. else if EQ("tag") {
  295. lua_pushnumber(lua_tag(reg[getreg(&pc)]));
  296. }
  297. else if EQ("type") {
  298. lua_pushstring(lua_type(reg[getreg(&pc)]));
  299. }
  300. else if EQ("next") {
  301. int n = getreg(&pc);
  302. n = lua_next(reg[n], (int)lua_getnumber(reg[getreg(&pc)]));
  303. lua_pushnumber(n);
  304. }
  305. else if EQ("equal") {
  306. int n1 = getreg(&pc);
  307. int n2 = getreg(&pc);
  308. lua_pushnumber(lua_equal(reg[n1], reg[n2]));
  309. }
  310. else if EQ("pushusertag") {
  311. int val = getreg(&pc);
  312. int tag = getreg(&pc);
  313. lua_pushusertag((void *)(int)lua_getnumber(reg[val]),
  314. (int)lua_getnumber(reg[tag]));
  315. }
  316. else if EQ("udataval") {
  317. int n = getreg(&pc);
  318. lua_pushnumber((int)lua_getuserdata(reg[getreg(&pc)]));
  319. reg[n] = lua_pop();
  320. }
  321. else if EQ("settagmethod") {
  322. int n = getreg(&pc);
  323. lua_settagmethod((int)lua_getnumber(reg[n]), getname(&pc));
  324. }
  325. else if EQ("beginblock") {
  326. lua_beginblock();
  327. }
  328. else if EQ("endblock") {
  329. lua_endblock();
  330. }
  331. else if EQ("newstate") {
  332. int stacksize = getnum(&pc);
  333. lua_State *L1 = lua_newstate(stacksize, getnum(&pc));
  334. if (L1)
  335. lua_pushuserdata(L1);
  336. else
  337. lua_pushnil();
  338. }
  339. else if EQ("closestate") {
  340. (lua_close)((lua_State *)lua_getuserdata(reg[getreg(&pc)]));
  341. }
  342. else if EQ("doremote") {
  343. lua_Object ol1 = reg[getreg(&pc)];
  344. lua_Object str = reg[getreg(&pc)];
  345. lua_State *L1;
  346. lua_Object temp;
  347. int status;
  348. if (!lua_isuserdata(ol1) || !lua_isstring(str))
  349. lua_error("bad arguments for `doremote'");
  350. L1 = (lua_State *)lua_getuserdata(ol1);
  351. status = (lua_dostring)(L1, lua_getstring(str));
  352. if (status != 0) {
  353. lua_pushnil();
  354. lua_pushnumber(status);
  355. }
  356. else {
  357. int i = 1;
  358. while ((temp = (lua_getresult)(L1, i++)) != LUA_NOOBJECT)
  359. lua_pushstring((lua_getstring)(L1, temp));
  360. }
  361. }
  362. #if LUA_DEPRECATETFUNCS
  363. else if EQ("rawsetglobal") {
  364. lua_rawsetglobal(getname(&pc));
  365. }
  366. else if EQ("rawgetglobal") {
  367. int n = getreg(&pc);
  368. reg[n] = lua_rawgetglobal(getname(&pc));
  369. }
  370. #endif
  371. else luaL_verror(lua_state, "unknown command in `testC': %.20s", inst);
  372. }
  373. }
  374. /* }====================================================== */
  375. static const struct luaL_reg tests_funcs[] = {
  376. {"hash", (lua_CFunction)hash_query},
  377. {"limits", (lua_CFunction)get_limits},
  378. {"listcode", (lua_CFunction)listcode},
  379. {"liststrings", (lua_CFunction)liststrings},
  380. {"listlocals", (lua_CFunction)listlocals},
  381. {"querystr", (lua_CFunction)string_query},
  382. {"querytab", (lua_CFunction)table_query},
  383. {"testC", (lua_CFunction)testC},
  384. {"totalmem", (lua_CFunction)mem_query}
  385. };
  386. void luaB_opentests (lua_State *L) {
  387. if (lua_state != NULL) return; /* do not open tests for auxiliar states */
  388. lua_state = L;
  389. luaL_openl(tests_funcs);
  390. }
  391. #endif