ldblib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ** $Id: ldblib.c,v 1.36 2001/03/26 14:31:49 roberto Exp roberto $
  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 LUA_PRIVATE
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "luadebug.h"
  13. #include "lualib.h"
  14. static void settabss (lua_State *L, const l_char *i, const l_char *v) {
  15. lua_pushstring(L, i);
  16. lua_pushstring(L, v);
  17. lua_settable(L, -3);
  18. }
  19. static void settabsi (lua_State *L, const l_char *i, int v) {
  20. lua_pushstring(L, i);
  21. lua_pushnumber(L, v);
  22. lua_settable(L, -3);
  23. }
  24. static int getinfo (lua_State *L) {
  25. lua_Debug ar;
  26. const l_char *options = luaL_opt_string(L, 2, l_s("flnSu"));
  27. l_char buff[20];
  28. if (lua_isnumber(L, 1)) {
  29. if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
  30. lua_pushnil(L); /* level out of range */
  31. return 1;
  32. }
  33. }
  34. else if (lua_isfunction(L, 1)) {
  35. lua_pushvalue(L, 1);
  36. sprintf(buff, l_s(">%.10s"), options);
  37. options = buff;
  38. }
  39. else
  40. luaL_argerror(L, 1, l_s("function or level expected"));
  41. if (!lua_getinfo(L, options, &ar))
  42. luaL_argerror(L, 2, l_s("invalid option"));
  43. lua_newtable(L);
  44. for (; *options; options++) {
  45. switch (*options) {
  46. case l_c('S'):
  47. settabss(L, l_s("source"), ar.source);
  48. if (ar.source)
  49. settabss(L, l_s("short_src"), ar.short_src);
  50. settabsi(L, l_s("linedefined"), ar.linedefined);
  51. settabss(L, l_s("what"), ar.what);
  52. break;
  53. case l_c('l'):
  54. settabsi(L, l_s("currentline"), ar.currentline);
  55. break;
  56. case l_c('u'):
  57. settabsi(L, l_s("nups"), ar.nups);
  58. break;
  59. case l_c('n'):
  60. settabss(L, l_s("name"), ar.name);
  61. settabss(L, l_s("namewhat"), ar.namewhat);
  62. break;
  63. case l_c('f'):
  64. lua_pushliteral(L, l_s("func"));
  65. lua_pushvalue(L, -3);
  66. lua_settable(L, -3);
  67. break;
  68. }
  69. }
  70. return 1; /* return table */
  71. }
  72. static int getlocal (lua_State *L) {
  73. lua_Debug ar;
  74. const l_char *name;
  75. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  76. luaL_argerror(L, 1, l_s("level out of range"));
  77. name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  78. if (name) {
  79. lua_pushstring(L, name);
  80. lua_pushvalue(L, -2);
  81. return 2;
  82. }
  83. else {
  84. lua_pushnil(L);
  85. return 1;
  86. }
  87. }
  88. static int setlocal (lua_State *L) {
  89. lua_Debug ar;
  90. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  91. luaL_argerror(L, 1, l_s("level out of range"));
  92. luaL_checkany(L, 3);
  93. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  94. return 1;
  95. }
  96. #define KEY_CALLHOOK l_s("luadblibCallhook")
  97. #define KEY_LINEHOOK l_s("luadblibLinehook")
  98. static void hookf (lua_State *L, const l_char *key) {
  99. lua_getregistry(L);
  100. lua_pushstring(L, key);
  101. lua_gettable(L, -2);
  102. if (lua_isfunction(L, -1)) {
  103. lua_pushvalue(L, -3); /* original argument (below table and function) */
  104. lua_rawcall(L, 1, 0);
  105. }
  106. else
  107. lua_pop(L, 1); /* pop result from gettable */
  108. lua_pop(L, 1); /* pop table */
  109. }
  110. static void callf (lua_State *L, lua_Debug *ar) {
  111. lua_pushstring(L, ar->event);
  112. hookf(L, KEY_CALLHOOK);
  113. }
  114. static void linef (lua_State *L, lua_Debug *ar) {
  115. lua_pushnumber(L, ar->currentline);
  116. hookf(L, KEY_LINEHOOK);
  117. }
  118. static void sethook (lua_State *L, const l_char *key, lua_Hook hook,
  119. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  120. lua_settop(L, 1);
  121. if (lua_isnil(L, 1))
  122. (*sethookf)(L, NULL);
  123. else if (lua_isfunction(L, 1))
  124. (*sethookf)(L, hook);
  125. else
  126. luaL_argerror(L, 1, l_s("function expected"));
  127. lua_getregistry(L);
  128. lua_pushstring(L, key);
  129. lua_pushvalue(L, -1); /* dup key */
  130. lua_gettable(L, -3); /* get old value */
  131. lua_pushvalue(L, -2); /* key (again) */
  132. lua_pushvalue(L, 1);
  133. lua_settable(L, -5); /* set new value */
  134. }
  135. static int setcallhook (lua_State *L) {
  136. sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
  137. return 1;
  138. }
  139. static int setlinehook (lua_State *L) {
  140. sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
  141. return 1;
  142. }
  143. static const luaL_reg dblib[] = {
  144. {l_s("getlocal"), getlocal},
  145. {l_s("getinfo"), getinfo},
  146. {l_s("setcallhook"), setcallhook},
  147. {l_s("setlinehook"), setlinehook},
  148. {l_s("setlocal"), setlocal}
  149. };
  150. LUALIB_API int lua_dblibopen (lua_State *L) {
  151. luaL_openl(L, dblib);
  152. return 0;
  153. }