2
0

ltests.c 6.9 KB

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