ltests.c 12 KB

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