ltests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. ** $Id: ltests.c,v 1.15 2000/04/13 16:51:01 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. static int pushop (lua_State *L, Instruction i) {
  41. char buff[100];
  42. switch (GET_OPCODE(i)) {
  43. case OP_END: O("END"); lua_pushstring(L, buff); return 0;
  44. case OP_RETURN: U("RETURN"); break;
  45. case OP_CALL: AB("CALL"); break;
  46. case OP_TAILCALL: AB("TAILCALL"); break;
  47. case OP_PUSHNIL: U("PUSHNIL"); break;
  48. case OP_POP: U("POP"); break;
  49. case OP_PUSHINT: S("PUSHINT"); break;
  50. case OP_PUSHSTRING: U("PUSHSTRING"); break;
  51. case OP_PUSHNUM: U("PUSHNUM"); break;
  52. case OP_PUSHNEGNUM: U("PUSHNEGNUM"); break;
  53. case OP_PUSHUPVALUE: U("PUSHUPVALUE"); break;
  54. case OP_GETLOCAL: U("GETLOCAL"); break;
  55. case OP_GETGLOBAL: U("GETGLOBAL"); break;
  56. case OP_GETTABLE: O("GETTABLE"); break;
  57. case OP_GETDOTTED: U("GETDOTTED"); break;
  58. case OP_GETINDEXED: U("GETINDEXED"); 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_SETTABLE: AB("SETTABLE"); break;
  64. case OP_SETLIST: AB("SETLIST"); break;
  65. case OP_SETMAP: U("SETMAP"); break;
  66. case OP_ADD: O("ADD"); break;
  67. case OP_ADDI: S("ADDI"); break;
  68. case OP_SUB: O("SUB"); break;
  69. case OP_MULT: O("MULT"); break;
  70. case OP_DIV: O("DIV"); break;
  71. case OP_POW: O("POW"); break;
  72. case OP_CONCAT: U("CONCAT"); break;
  73. case OP_MINUS: O("MINUS"); break;
  74. case OP_NOT: O("NOT"); break;
  75. case OP_JMPNE: S("JMPNE"); break;
  76. case OP_JMPEQ: S("JMPEQ"); break;
  77. case OP_JMPLT: S("JMPLT"); break;
  78. case OP_JMPLE: S("JMPLE"); break;
  79. case OP_JMPGT: S("JMPGT"); break;
  80. case OP_JMPGE: S("JMPGE"); break;
  81. case OP_JMPT: S("JMPT"); break;
  82. case OP_JMPF: S("JMPF"); 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_FORPREP: S("OP_FORPREP"); break;
  88. case OP_FORLOOP: S("OP_FORLOOP"); 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 listcode (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 = pushop(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, "MAXSTACK", MAXSTACK);
  126. setnameval(L, t, "MAXLOCALS", MAXLOCALS);
  127. setnameval(L, t, "MAXUPVALUES", MAXUPVALUES);
  128. setnameval(L, t, "MAXVARSLH", MAXVARSLH);
  129. setnameval(L, t, "MAXPARAMS", MAXPARAMS);
  130. setnameval(L, t, "LFPF", LFIELDS_PER_FLUSH);
  131. setnameval(L, t, "RFPF", RFIELDS_PER_FLUSH);
  132. lua_pushobject(L, t);
  133. }
  134. static void mem_query (lua_State *L) {
  135. lua_pushnumber(L, memdebug_total);
  136. lua_pushnumber(L, memdebug_numblocks);
  137. lua_pushnumber(L, memdebug_maxmem);
  138. }
  139. static void hash_query (lua_State *L) {
  140. lua_Object o = luaL_nonnullarg(L, 1);
  141. if (lua_getparam(L, 2) == LUA_NOOBJECT) {
  142. luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "string expected");
  143. lua_pushnumber(L, tsvalue(o)->hash);
  144. }
  145. else {
  146. const Hash *t = avalue(luaL_tablearg(L, 2));
  147. lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
  148. }
  149. }
  150. static void table_query (lua_State *L) {
  151. const Hash *t = avalue(luaL_tablearg(L, 1));
  152. int i = luaL_opt_int(L, 2, -1);
  153. if (i == -1) {
  154. lua_pushnumber(L, t->size);
  155. lua_pushnumber(L, t->firstfree - t->node);
  156. }
  157. else if (i < t->size) {
  158. luaA_pushobject(L, &t->node[i].key);
  159. luaA_pushobject(L, &t->node[i].val);
  160. if (t->node[i].next)
  161. lua_pushnumber(L, t->node[i].next - t->node);
  162. }
  163. }
  164. static void string_query (lua_State *L) {
  165. int h = luaL_check_int(L, 1) - 1;
  166. int s = luaL_opt_int(L, 2, 0) - 1;
  167. if (s==-1) {
  168. if (h < NUM_HASHS) {
  169. lua_pushnumber(L, L->string_root[h].nuse);
  170. lua_pushnumber(L, L->string_root[h].size);
  171. }
  172. }
  173. else {
  174. TString *ts = L->string_root[h].hash[s];
  175. for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
  176. if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
  177. else lua_pushstring(L, ts->str);
  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("nextvar") {
  296. lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)])));
  297. }
  298. else if EQ("next") {
  299. int n = getreg(L, &pc);
  300. n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  301. lua_pushnumber(L, n);
  302. }
  303. else if EQ("equal") {
  304. int n1 = getreg(L, &pc);
  305. int n2 = getreg(L, &pc);
  306. lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
  307. }
  308. else if EQ("pushusertag") {
  309. int val = getreg(L, &pc);
  310. int tag = getreg(L, &pc);
  311. lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
  312. (int)lua_getnumber(L, reg[tag]));
  313. }
  314. else if EQ("udataval") {
  315. int n = getreg(L, &pc);
  316. lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
  317. reg[n] = lua_pop(L);
  318. }
  319. else if EQ("settagmethod") {
  320. int n = getreg(L, &pc);
  321. lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
  322. }
  323. else if EQ("beginblock") {
  324. lua_beginblock(L);
  325. }
  326. else if EQ("endblock") {
  327. lua_endblock(L);
  328. }
  329. else if EQ("newstate") {
  330. int stacksize = getnum(&pc);
  331. lua_State *L1 = lua_newstate("stack", stacksize,
  332. "builtin", getnum(&pc), NULL);
  333. lua_pushuserdata(L, L1);
  334. }
  335. else if EQ("closestate") {
  336. lua_close((lua_State *)lua_getuserdata(L, reg[getreg(L, &pc)]));
  337. }
  338. else if EQ("doremote") {
  339. lua_Object ol1 = reg[getreg(L, &pc)];
  340. lua_Object str = reg[getreg(L, &pc)];
  341. lua_State *L1;
  342. lua_Object temp;
  343. int i;
  344. if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
  345. lua_error(L, "bad arguments for `doremote'");
  346. L1 = (lua_State *)lua_getuserdata(L, ol1);
  347. lua_dostring(L1, lua_getstring(L, str));
  348. i = 1;
  349. while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
  350. lua_pushstring(L, lua_getstring(L1, temp));
  351. }
  352. else luaL_verror(L, "unknown command in `testC': %.20s", inst);
  353. }
  354. }
  355. /* }====================================================== */
  356. static const struct luaL_reg tests_funcs[] = {
  357. {"hash", hash_query},
  358. {"limits", get_limits},
  359. {"listcode", listcode},
  360. {"querystr", string_query},
  361. {"querytab", table_query},
  362. {"testC", testC},
  363. {"totalmem", mem_query}
  364. };
  365. void luaB_opentests (lua_State *L) {
  366. luaL_openl(L, tests_funcs);
  367. }
  368. #endif