ltests.c 10 KB

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