ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. ** $Id: ltests.c,v 1.17 2000/05/08 19:32:53 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("OP_FORPREP"); break;
  89. case OP_FORLOOP: S("OP_FORLOOP"); break;
  90. case OP_CLOSURE: AB("CLOSURE"); break;
  91. case OP_SETLINE: U("SETLINE"); break;
  92. }
  93. lua_pushstring(L, buff);
  94. return 1;
  95. }
  96. static void listcode (lua_State *L) {
  97. lua_Object o = luaL_nonnullarg(L, 1);
  98. lua_Object t = lua_createtable(L);
  99. Instruction *pc;
  100. Proto *p;
  101. int res;
  102. luaL_arg_check(L, ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
  103. p = clvalue(o)->f.l;
  104. setnameval(L, t, "maxstack", p->maxstacksize);
  105. setnameval(L, t, "numparams", p->numparams);
  106. pc = p->code;
  107. do {
  108. lua_pushobject(L, t);
  109. lua_pushnumber(L, pc - p->code + 1);
  110. res = pushop(L, *pc++);
  111. lua_settable(L);
  112. } while (res);
  113. lua_pushobject(L, t);
  114. }
  115. /* }====================================================== */
  116. static void get_limits (lua_State *L) {
  117. lua_Object t = lua_createtable(L);
  118. setnameval(L, t, "SIZE_OP", SIZE_OP);
  119. setnameval(L, t, "SIZE_U", SIZE_U);
  120. setnameval(L, t, "SIZE_A", SIZE_A);
  121. setnameval(L, t, "SIZE_B", SIZE_B);
  122. setnameval(L, t, "MAXARG_U", MAXARG_U);
  123. setnameval(L, t, "MAXARG_S", MAXARG_S);
  124. setnameval(L, t, "MAXARG_A", MAXARG_A);
  125. setnameval(L, t, "MAXARG_B", MAXARG_B);
  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)->u.s.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. stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
  167. int s = luaL_opt_int(L, 2, 0) - 1;
  168. if (s==-1) {
  169. lua_pushnumber(L, tb->nuse);
  170. lua_pushnumber(L, tb->size);
  171. }
  172. else if (s < tb->size) {
  173. TString *ts;
  174. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  175. ttype(L->top) = TAG_STRING;
  176. tsvalue(L->top) = ts;
  177. incr_top;
  178. }
  179. }
  180. }
  181. /*
  182. ** {======================================================
  183. ** function to test the API with C. It interprets a kind of "assembler"
  184. ** language with calls to the API, so the test can be driven by Lua code
  185. ** =======================================================
  186. */
  187. static const char *const delimits = " \t\n,;";
  188. static void skip (const char **pc) {
  189. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  190. }
  191. static int getnum (const char **pc) {
  192. int res = 0;
  193. skip(pc);
  194. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  195. return res;
  196. }
  197. static int getreg (lua_State *L, const char **pc) {
  198. skip(pc);
  199. if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
  200. return getnum(pc);
  201. }
  202. static const char *getname (const char **pc) {
  203. static char buff[30];
  204. int i = 0;
  205. skip(pc);
  206. while (**pc != '\0' && !strchr(delimits, **pc))
  207. buff[i++] = *(*pc)++;
  208. buff[i] = '\0';
  209. return buff;
  210. }
  211. #define EQ(s1) (strcmp(s1, inst) == 0)
  212. static void testC (lua_State *L) {
  213. lua_Object reg[10];
  214. const char *pc = luaL_check_string(L, 1);
  215. for (;;) {
  216. const char *inst = getname(&pc);
  217. if EQ("") return;
  218. else if EQ("pushnum") {
  219. lua_pushnumber(L, getnum(&pc));
  220. }
  221. else if EQ("createtable") {
  222. reg[getreg(L, &pc)] = lua_createtable(L);
  223. }
  224. else if EQ("closure") {
  225. lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
  226. lua_pushcclosure(L, f, getnum(&pc));
  227. }
  228. else if EQ("pop") {
  229. reg[getreg(L, &pc)] = lua_pop(L);
  230. }
  231. else if EQ("getglobal") {
  232. int n = getreg(L, &pc);
  233. reg[n] = lua_getglobal(L, getname(&pc));
  234. }
  235. else if EQ("rawgetglobal") {
  236. int n = getreg(L, &pc);
  237. reg[n] = lua_rawgetglobal(L, getname(&pc));
  238. }
  239. else if EQ("ref") {
  240. lua_pushnumber(L, lua_ref(L, 0));
  241. reg[getreg(L, &pc)] = lua_pop(L);
  242. }
  243. else if EQ("reflock") {
  244. lua_pushnumber(L, lua_ref(L, 1));
  245. reg[getreg(L, &pc)] = lua_pop(L);
  246. }
  247. else if EQ("getref") {
  248. int n = getreg(L, &pc);
  249. reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  250. }
  251. else if EQ("unref") {
  252. lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  253. }
  254. else if EQ("getparam") {
  255. int n = getreg(L, &pc);
  256. reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the command itself */
  257. }
  258. else if EQ("getresult") {
  259. int n = getreg(L, &pc);
  260. reg[n] = lua_getparam(L, getnum(&pc));
  261. }
  262. else if EQ("setglobal") {
  263. lua_setglobal(L, getname(&pc));
  264. }
  265. else if EQ("rawsetglobal") {
  266. lua_rawsetglobal(L, getname(&pc));
  267. }
  268. else if EQ("pushstring") {
  269. lua_pushstring(L, getname(&pc));
  270. }
  271. else if EQ("pushreg") {
  272. lua_pushobject(L, reg[getreg(L, &pc)]);
  273. }
  274. else if EQ("call") {
  275. if (lua_call(L, getname(&pc))) lua_error(L, NULL);
  276. }
  277. else if EQ("gettable") {
  278. reg[getreg(L, &pc)] = lua_gettable(L);
  279. }
  280. else if EQ("rawgettable") {
  281. reg[getreg(L, &pc)] = lua_rawgettable(L);
  282. }
  283. else if EQ("settable") {
  284. lua_settable(L);
  285. }
  286. else if EQ("rawsettable") {
  287. lua_rawsettable(L);
  288. }
  289. else if EQ("tag") {
  290. lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)]));
  291. }
  292. else if EQ("type") {
  293. lua_pushstring(L, lua_type(L, reg[getreg(L, &pc)]));
  294. }
  295. else if EQ("next") {
  296. int n = getreg(L, &pc);
  297. n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  298. lua_pushnumber(L, n);
  299. }
  300. else if EQ("equal") {
  301. int n1 = getreg(L, &pc);
  302. int n2 = getreg(L, &pc);
  303. lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
  304. }
  305. else if EQ("pushusertag") {
  306. int val = getreg(L, &pc);
  307. int tag = getreg(L, &pc);
  308. lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
  309. (int)lua_getnumber(L, reg[tag]));
  310. }
  311. else if EQ("udataval") {
  312. int n = getreg(L, &pc);
  313. lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
  314. reg[n] = lua_pop(L);
  315. }
  316. else if EQ("settagmethod") {
  317. int n = getreg(L, &pc);
  318. lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
  319. }
  320. else if EQ("beginblock") {
  321. lua_beginblock(L);
  322. }
  323. else if EQ("endblock") {
  324. lua_endblock(L);
  325. }
  326. else if EQ("newstate") {
  327. int stacksize = getnum(&pc);
  328. lua_State *L1 = lua_newstate("stack", stacksize,
  329. "builtin", getnum(&pc), NULL);
  330. lua_pushuserdata(L, L1);
  331. }
  332. else if EQ("closestate") {
  333. lua_close((lua_State *)lua_getuserdata(L, reg[getreg(L, &pc)]));
  334. }
  335. else if EQ("doremote") {
  336. lua_Object ol1 = reg[getreg(L, &pc)];
  337. lua_Object str = reg[getreg(L, &pc)];
  338. lua_State *L1;
  339. lua_Object temp;
  340. int i;
  341. if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
  342. lua_error(L, "bad arguments for `doremote'");
  343. L1 = (lua_State *)lua_getuserdata(L, ol1);
  344. lua_dostring(L1, lua_getstring(L, str));
  345. i = 1;
  346. while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
  347. lua_pushstring(L, lua_getstring(L1, temp));
  348. }
  349. else luaL_verror(L, "unknown command in `testC': %.20s", inst);
  350. }
  351. }
  352. /* }====================================================== */
  353. static const struct luaL_reg tests_funcs[] = {
  354. {"hash", hash_query},
  355. {"limits", get_limits},
  356. {"listcode", listcode},
  357. {"querystr", string_query},
  358. {"querytab", table_query},
  359. {"testC", testC},
  360. {"totalmem", mem_query}
  361. };
  362. void luaB_opentests (lua_State *L) {
  363. luaL_openl(L, tests_funcs);
  364. }
  365. #endif