ltests.c 7.2 KB

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