ltests.c 10 KB

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