ltests.c 11 KB

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