ltests.c 10 KB

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