ltests.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** $Id: ltests.c,v 1.2 1999/12/23 18:19:57 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 <stdlib.h>
  8. #define LUA_REENTRANT
  9. #include "lapi.h"
  10. #include "lauxlib.h"
  11. #include "lmem.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "ltable.h"
  15. #include "lua.h"
  16. #include "luadebug.h"
  17. void luaB_opentests (lua_State *L);
  18. /*
  19. ** The whole module only makes sense with DEBUG on
  20. */
  21. #ifdef DEBUG
  22. static void mem_query (lua_State *L) {
  23. lua_pushnumber(L, totalmem);
  24. lua_pushnumber(L, numblocks);
  25. }
  26. static void hash_query (lua_State *L) {
  27. lua_Object o = luaL_nonnullarg(L, 1);
  28. if (lua_getparam(L, 2) == LUA_NOOBJECT) {
  29. luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected");
  30. lua_pushnumber(L, tsvalue(o)->hash);
  31. }
  32. else {
  33. const Hash *t = avalue(luaL_tablearg(L, 2));
  34. lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
  35. }
  36. }
  37. static void table_query (lua_State *L) {
  38. const Hash *t = avalue(luaL_tablearg(L, 1));
  39. int i = luaL_opt_int(L, 2, -1);
  40. if (i == -1) {
  41. lua_pushnumber(L, t->size);
  42. lua_pushnumber(L, t->firstfree - t->node);
  43. }
  44. else if (i < t->size) {
  45. luaA_pushobject(L, &t->node[i].key);
  46. luaA_pushobject(L, &t->node[i].val);
  47. if (t->node[i].next)
  48. lua_pushnumber(L, t->node[i].next - t->node);
  49. }
  50. }
  51. static void query_strings (lua_State *L) {
  52. int h = luaL_check_int(L, 1) - 1;
  53. int s = luaL_opt_int(L, 2, 0) - 1;
  54. if (s==-1) {
  55. if (h < NUM_HASHS) {
  56. lua_pushnumber(L, L->string_root[h].nuse);
  57. lua_pushnumber(L, L->string_root[h].size);
  58. }
  59. }
  60. else {
  61. TaggedString *ts = L->string_root[h].hash[s];
  62. for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
  63. if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
  64. else lua_pushstring(L, ts->str);
  65. }
  66. }
  67. }
  68. static const char *delimits = " \t\n,;";
  69. static void skip (const char **pc) {
  70. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  71. }
  72. static int getnum (const char **pc) {
  73. int res = 0;
  74. skip(pc);
  75. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  76. return res;
  77. }
  78. static int getreg (lua_State *L, const char **pc) {
  79. skip(pc);
  80. if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
  81. return getnum(pc);
  82. }
  83. static const char *getname (const char **pc) {
  84. static char buff[30];
  85. int i = 0;
  86. skip(pc);
  87. while (**pc != '\0' && !strchr(delimits, **pc))
  88. buff[i++] = *(*pc)++;
  89. buff[i] = '\0';
  90. return buff;
  91. }
  92. #define EQ(s1) (strcmp(s1, inst) == 0)
  93. /*
  94. ** function to test the API with C. It interprets a kind of "assembler"
  95. ** language with calls to the API, so the test can be driven by Lua code
  96. */
  97. static void testC (lua_State *L) {
  98. lua_Object reg[10];
  99. const char *pc = luaL_check_string(L, 1);
  100. for (;;) {
  101. const char *inst = getname(&pc);
  102. if EQ("") return;
  103. else if EQ("pushnum") {
  104. lua_pushnumber(L, getnum(&pc));
  105. }
  106. else if EQ("createtable") {
  107. reg[getreg(L, &pc)] = lua_createtable(L);
  108. }
  109. else if EQ("closure") {
  110. lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
  111. lua_pushcclosure(L, f, getnum(&pc));
  112. }
  113. else if EQ("pop") {
  114. reg[getreg(L, &pc)] = lua_pop(L);
  115. }
  116. else if EQ("getglobal") {
  117. int n = getreg(L, &pc);
  118. reg[n] = lua_getglobal(L, getname(&pc));
  119. }
  120. else if EQ("rawgetglobal") {
  121. int n = getreg(L, &pc);
  122. reg[n] = lua_rawgetglobal(L, getname(&pc));
  123. }
  124. else if EQ("ref") {
  125. lua_pushnumber(L, lua_ref(L, 0));
  126. reg[getreg(L, &pc)] = lua_pop(L);
  127. }
  128. else if EQ("reflock") {
  129. lua_pushnumber(L, lua_ref(L, 1));
  130. reg[getreg(L, &pc)] = lua_pop(L);
  131. }
  132. else if EQ("getref") {
  133. int n = getreg(L, &pc);
  134. reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  135. }
  136. else if EQ("unref") {
  137. lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  138. }
  139. else if EQ("getparam") {
  140. int n = getreg(L, &pc);
  141. reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the command itself */
  142. }
  143. else if EQ("getresult") {
  144. int n = getreg(L, &pc);
  145. reg[n] = lua_getparam(L, getnum(&pc));
  146. }
  147. else if EQ("setglobal") {
  148. lua_setglobal(L, getname(&pc));
  149. }
  150. else if EQ("rawsetglobal") {
  151. lua_rawsetglobal(L, getname(&pc));
  152. }
  153. else if EQ("pushstring") {
  154. lua_pushstring(L, getname(&pc));
  155. }
  156. else if EQ("pushreg") {
  157. lua_pushobject(L, reg[getreg(L, &pc)]);
  158. }
  159. else if EQ("call") {
  160. lua_call(L, getname(&pc));
  161. }
  162. else if EQ("gettable") {
  163. reg[getreg(L, &pc)] = lua_gettable(L);
  164. }
  165. else if EQ("rawgettable") {
  166. reg[getreg(L, &pc)] = lua_rawgettable(L);
  167. }
  168. else if EQ("settable") {
  169. lua_settable(L);
  170. }
  171. else if EQ("rawsettable") {
  172. lua_rawsettable(L);
  173. }
  174. else if EQ("tag") {
  175. lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)]));
  176. }
  177. else if EQ("type") {
  178. lua_pushstring(L, lua_type(L, reg[getreg(L, &pc)]));
  179. }
  180. else if EQ("nextvar") {
  181. lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)])));
  182. }
  183. else if EQ("next") {
  184. int n = getreg(L, &pc);
  185. n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
  186. lua_pushnumber(L, n);
  187. }
  188. else if EQ("equal") {
  189. int n1 = getreg(L, &pc);
  190. int n2 = getreg(L, &pc);
  191. lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
  192. }
  193. else if EQ("pushusertag") {
  194. int val = getreg(L, &pc);
  195. int tag = getreg(L, &pc);
  196. lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
  197. (int)lua_getnumber(L, reg[tag]));
  198. }
  199. else if EQ("udataval") {
  200. int n = getreg(L, &pc);
  201. lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
  202. reg[n] = lua_pop(L);
  203. }
  204. else if EQ("settagmethod") {
  205. int n = getreg(L, &pc);
  206. lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
  207. }
  208. else if EQ("getfunc") {
  209. int n = getreg(L, &pc);
  210. reg[n] = lua_stackedfunction(L, getnum(&pc));
  211. }
  212. else if EQ("beginblock") {
  213. lua_beginblock(L);
  214. }
  215. else if EQ("endblock") {
  216. lua_endblock(L);
  217. }
  218. else if EQ("newstate") {
  219. int stacksize = getnum(&pc);
  220. lua_State *L1 = lua_newstate("stack", stacksize,
  221. "builtin", getnum(&pc), NULL);
  222. lua_pushuserdata(L, L1);
  223. }
  224. else if EQ("closestate") {
  225. lua_close(lua_getuserdata(L, reg[getreg(L, &pc)]));
  226. }
  227. else if EQ("doremote") {
  228. lua_Object ol1 = reg[getreg(L, &pc)];
  229. lua_Object str = reg[getreg(L, &pc)];
  230. lua_State *L1;
  231. lua_Object temp;
  232. int i;
  233. if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
  234. lua_error(L, "bad arguments for `doremote'");
  235. L1 = lua_getuserdata(L, ol1);
  236. lua_dostring(L1, lua_getstring(L, str));
  237. i = 1;
  238. while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
  239. lua_pushstring(L, lua_getstring(L1, temp));
  240. }
  241. else luaL_verror(L, "unknown command in `testC': %.20s", inst);
  242. }
  243. }
  244. static const struct luaL_reg tests_funcs[] = {
  245. {"hash", hash_query},
  246. {"querystr", query_strings},
  247. {"querytab", table_query},
  248. {"testC", testC},
  249. {"totalmem", mem_query}
  250. };
  251. void luaB_opentests (lua_State *L) {
  252. luaL_openl(L, tests_funcs);
  253. }
  254. #endif