ltests.c 7.2 KB

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