/script_binding/lua/lua/ldblib.c

http://ftk.googlecode.com/ · C · 397 lines · 330 code · 61 blank · 6 comment · 75 complexity · 02e4c11fecbdf5466991910c50b549f7 MD5 · raw file

  1. /*
  2. ** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $
  3. ** Interface from Lua to its debug API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define ldblib_c
  10. #define LUA_LIB
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. static int db_getregistry (lua_State *L) {
  15. lua_pushvalue(L, LUA_REGISTRYINDEX);
  16. return 1;
  17. }
  18. static int db_getmetatable (lua_State *L) {
  19. luaL_checkany(L, 1);
  20. if (!lua_getmetatable(L, 1)) {
  21. lua_pushnil(L); /* no metatable */
  22. }
  23. return 1;
  24. }
  25. static int db_setmetatable (lua_State *L) {
  26. int t = lua_type(L, 2);
  27. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  28. "nil or table expected");
  29. lua_settop(L, 2);
  30. lua_pushboolean(L, lua_setmetatable(L, 1));
  31. return 1;
  32. }
  33. static int db_getfenv (lua_State *L) {
  34. lua_getfenv(L, 1);
  35. return 1;
  36. }
  37. static int db_setfenv (lua_State *L) {
  38. luaL_checktype(L, 2, LUA_TTABLE);
  39. lua_settop(L, 2);
  40. if (lua_setfenv(L, 1) == 0)
  41. luaL_error(L, LUA_QL("setfenv")
  42. " cannot change environment of given object");
  43. return 1;
  44. }
  45. static void settabss (lua_State *L, const char *i, const char *v) {
  46. lua_pushstring(L, v);
  47. lua_setfield(L, -2, i);
  48. }
  49. static void settabsi (lua_State *L, const char *i, int v) {
  50. lua_pushinteger(L, v);
  51. lua_setfield(L, -2, i);
  52. }
  53. static lua_State *getthread (lua_State *L, int *arg) {
  54. if (lua_isthread(L, 1)) {
  55. *arg = 1;
  56. return lua_tothread(L, 1);
  57. }
  58. else {
  59. *arg = 0;
  60. return L;
  61. }
  62. }
  63. static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
  64. if (L == L1) {
  65. lua_pushvalue(L, -2);
  66. lua_remove(L, -3);
  67. }
  68. else
  69. lua_xmove(L1, L, 1);
  70. lua_setfield(L, -2, fname);
  71. }
  72. static int db_getinfo (lua_State *L) {
  73. lua_Debug ar;
  74. int arg;
  75. lua_State *L1 = getthread(L, &arg);
  76. const char *options = luaL_optstring(L, arg+2, "flnSu");
  77. if (lua_isnumber(L, arg+1)) {
  78. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  79. lua_pushnil(L); /* level out of range */
  80. return 1;
  81. }
  82. }
  83. else if (lua_isfunction(L, arg+1)) {
  84. lua_pushfstring(L, ">%s", options);
  85. options = lua_tostring(L, -1);
  86. lua_pushvalue(L, arg+1);
  87. lua_xmove(L, L1, 1);
  88. }
  89. else
  90. return luaL_argerror(L, arg+1, "function or level expected");
  91. if (!lua_getinfo(L1, options, &ar))
  92. return luaL_argerror(L, arg+2, "invalid option");
  93. lua_createtable(L, 0, 2);
  94. if (strchr(options, 'S')) {
  95. settabss(L, "source", ar.source);
  96. settabss(L, "short_src", ar.short_src);
  97. settabsi(L, "linedefined", ar.linedefined);
  98. settabsi(L, "lastlinedefined", ar.lastlinedefined);
  99. settabss(L, "what", ar.what);
  100. }
  101. if (strchr(options, 'l'))
  102. settabsi(L, "currentline", ar.currentline);
  103. if (strchr(options, 'u'))
  104. settabsi(L, "nups", ar.nups);
  105. if (strchr(options, 'n')) {
  106. settabss(L, "name", ar.name);
  107. settabss(L, "namewhat", ar.namewhat);
  108. }
  109. if (strchr(options, 'L'))
  110. treatstackoption(L, L1, "activelines");
  111. if (strchr(options, 'f'))
  112. treatstackoption(L, L1, "func");
  113. return 1; /* return table */
  114. }
  115. static int db_getlocal (lua_State *L) {
  116. int arg;
  117. lua_State *L1 = getthread(L, &arg);
  118. lua_Debug ar;
  119. const char *name;
  120. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  121. return luaL_argerror(L, arg+1, "level out of range");
  122. name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
  123. if (name) {
  124. lua_xmove(L1, L, 1);
  125. lua_pushstring(L, name);
  126. lua_pushvalue(L, -2);
  127. return 2;
  128. }
  129. else {
  130. lua_pushnil(L);
  131. return 1;
  132. }
  133. }
  134. static int db_setlocal (lua_State *L) {
  135. int arg;
  136. lua_State *L1 = getthread(L, &arg);
  137. lua_Debug ar;
  138. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  139. return luaL_argerror(L, arg+1, "level out of range");
  140. luaL_checkany(L, arg+3);
  141. lua_settop(L, arg+3);
  142. lua_xmove(L, L1, 1);
  143. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  144. return 1;
  145. }
  146. static int auxupvalue (lua_State *L, int get) {
  147. const char *name;
  148. int n = luaL_checkint(L, 2);
  149. luaL_checktype(L, 1, LUA_TFUNCTION);
  150. if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
  151. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  152. if (name == NULL) return 0;
  153. lua_pushstring(L, name);
  154. lua_insert(L, -(get+1));
  155. return get + 1;
  156. }
  157. static int db_getupvalue (lua_State *L) {
  158. return auxupvalue(L, 1);
  159. }
  160. static int db_setupvalue (lua_State *L) {
  161. luaL_checkany(L, 3);
  162. return auxupvalue(L, 0);
  163. }
  164. static const char KEY_HOOK = 'h';
  165. static void hookf (lua_State *L, lua_Debug *ar) {
  166. static const char *const hooknames[] =
  167. {"call", "return", "line", "count", "tail return"};
  168. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  169. lua_rawget(L, LUA_REGISTRYINDEX);
  170. lua_pushlightuserdata(L, L);
  171. lua_rawget(L, -2);
  172. if (lua_isfunction(L, -1)) {
  173. lua_pushstring(L, hooknames[(int)ar->event]);
  174. if (ar->currentline >= 0)
  175. lua_pushinteger(L, ar->currentline);
  176. else lua_pushnil(L);
  177. lua_assert(lua_getinfo(L, "lS", ar));
  178. lua_call(L, 2, 0);
  179. }
  180. }
  181. static int makemask (const char *smask, int count) {
  182. int mask = 0;
  183. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  184. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  185. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  186. if (count > 0) mask |= LUA_MASKCOUNT;
  187. return mask;
  188. }
  189. static char *unmakemask (int mask, char *smask) {
  190. int i = 0;
  191. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  192. if (mask & LUA_MASKRET) smask[i++] = 'r';
  193. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  194. smask[i] = '\0';
  195. return smask;
  196. }
  197. static void gethooktable (lua_State *L) {
  198. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  199. lua_rawget(L, LUA_REGISTRYINDEX);
  200. if (!lua_istable(L, -1)) {
  201. lua_pop(L, 1);
  202. lua_createtable(L, 0, 1);
  203. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  204. lua_pushvalue(L, -2);
  205. lua_rawset(L, LUA_REGISTRYINDEX);
  206. }
  207. }
  208. static int db_sethook (lua_State *L) {
  209. int arg, mask, count;
  210. lua_Hook func;
  211. lua_State *L1 = getthread(L, &arg);
  212. if (lua_isnoneornil(L, arg+1)) {
  213. lua_settop(L, arg+1);
  214. func = NULL; mask = 0; count = 0; /* turn off hooks */
  215. }
  216. else {
  217. const char *smask = luaL_checkstring(L, arg+2);
  218. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  219. count = luaL_optint(L, arg+3, 0);
  220. func = hookf; mask = makemask(smask, count);
  221. }
  222. gethooktable(L);
  223. lua_pushlightuserdata(L, L1);
  224. lua_pushvalue(L, arg+1);
  225. lua_rawset(L, -3); /* set new hook */
  226. lua_pop(L, 1); /* remove hook table */
  227. lua_sethook(L1, func, mask, count); /* set hooks */
  228. return 0;
  229. }
  230. static int db_gethook (lua_State *L) {
  231. int arg;
  232. lua_State *L1 = getthread(L, &arg);
  233. char buff[5];
  234. int mask = lua_gethookmask(L1);
  235. lua_Hook hook = lua_gethook(L1);
  236. if (hook != NULL && hook != hookf) /* external hook? */
  237. lua_pushliteral(L, "external hook");
  238. else {
  239. gethooktable(L);
  240. lua_pushlightuserdata(L, L1);
  241. lua_rawget(L, -2); /* get hook */
  242. lua_remove(L, -2); /* remove hook table */
  243. }
  244. lua_pushstring(L, unmakemask(mask, buff));
  245. lua_pushinteger(L, lua_gethookcount(L1));
  246. return 3;
  247. }
  248. static int db_debug (lua_State *L) {
  249. for (;;) {
  250. char buffer[250];
  251. fputs("lua_debug> ", stderr);
  252. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  253. strcmp(buffer, "cont\n") == 0)
  254. return 0;
  255. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  256. lua_pcall(L, 0, 0, 0)) {
  257. fputs(lua_tostring(L, -1), stderr);
  258. fputs("\n", stderr);
  259. }
  260. lua_settop(L, 0); /* remove eventual returns */
  261. }
  262. }
  263. #define LEVELS1 12 /* size of the first part of the stack */
  264. #define LEVELS2 10 /* size of the second part of the stack */
  265. static int db_errorfb (lua_State *L) {
  266. int level;
  267. int firstpart = 1; /* still before eventual `...' */
  268. int arg;
  269. lua_State *L1 = getthread(L, &arg);
  270. lua_Debug ar;
  271. if (lua_isnumber(L, arg+2)) {
  272. level = (int)lua_tointeger(L, arg+2);
  273. lua_pop(L, 1);
  274. }
  275. else
  276. level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
  277. if (lua_gettop(L) == arg)
  278. lua_pushliteral(L, "");
  279. else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
  280. else lua_pushliteral(L, "\n");
  281. lua_pushliteral(L, "stack traceback:");
  282. while (lua_getstack(L1, level++, &ar)) {
  283. if (level > LEVELS1 && firstpart) {
  284. /* no more than `LEVELS2' more levels? */
  285. if (!lua_getstack(L1, level+LEVELS2, &ar))
  286. level--; /* keep going */
  287. else {
  288. lua_pushliteral(L, "\n\t..."); /* too many levels */
  289. while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
  290. level++;
  291. }
  292. firstpart = 0;
  293. continue;
  294. }
  295. lua_pushliteral(L, "\n\t");
  296. lua_getinfo(L1, "Snl", &ar);
  297. lua_pushfstring(L, "%s:", ar.short_src);
  298. if (ar.currentline > 0)
  299. lua_pushfstring(L, "%d:", ar.currentline);
  300. if (*ar.namewhat != '\0') /* is there a name? */
  301. lua_pushfstring(L, " in function " LUA_QS, ar.name);
  302. else {
  303. if (*ar.what == 'm') /* main? */
  304. lua_pushfstring(L, " in main chunk");
  305. else if (*ar.what == 'C' || *ar.what == 't')
  306. lua_pushliteral(L, " ?"); /* C function or tail call */
  307. else
  308. lua_pushfstring(L, " in function <%s:%d>",
  309. ar.short_src, ar.linedefined);
  310. }
  311. lua_concat(L, lua_gettop(L) - arg);
  312. }
  313. lua_concat(L, lua_gettop(L) - arg);
  314. return 1;
  315. }
  316. static const luaL_Reg dblib[] = {
  317. {"debug", db_debug},
  318. {"getfenv", db_getfenv},
  319. {"gethook", db_gethook},
  320. {"getinfo", db_getinfo},
  321. {"getlocal", db_getlocal},
  322. {"getregistry", db_getregistry},
  323. {"getmetatable", db_getmetatable},
  324. {"getupvalue", db_getupvalue},
  325. {"setfenv", db_setfenv},
  326. {"sethook", db_sethook},
  327. {"setlocal", db_setlocal},
  328. {"setmetatable", db_setmetatable},
  329. {"setupvalue", db_setupvalue},
  330. {"traceback", db_errorfb},
  331. {NULL, NULL}
  332. };
  333. LUALIB_API int luaopen_debug (lua_State *L) {
  334. luaL_register(L, LUA_DBLIBNAME, dblib);
  335. return 1;
  336. }