ldblib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. ** $Id: ldblib.c,v 1.35 2001/03/07 18:09:25 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. /* dummy variables (to define unique addresses) */
  97. static const l_char key1[] = l_s("ab");
  98. #define KEY_CALLHOOK ((void *)key1)
  99. #define KEY_LINEHOOK ((void *)(key1+1))
  100. static void hookf (lua_State *L, void *key) {
  101. lua_getregistry(L);
  102. lua_pushuserdata(L, key);
  103. lua_gettable(L, -2);
  104. if (lua_isfunction(L, -1)) {
  105. lua_pushvalue(L, -3); /* original argument (below table and function) */
  106. lua_rawcall(L, 1, 0);
  107. }
  108. else
  109. lua_pop(L, 1); /* pop result from gettable */
  110. lua_pop(L, 1); /* pop table */
  111. }
  112. static void callf (lua_State *L, lua_Debug *ar) {
  113. lua_pushstring(L, ar->event);
  114. hookf(L, KEY_CALLHOOK);
  115. }
  116. static void linef (lua_State *L, lua_Debug *ar) {
  117. lua_pushnumber(L, ar->currentline);
  118. hookf(L, KEY_LINEHOOK);
  119. }
  120. static void sethook (lua_State *L, void *key, lua_Hook hook,
  121. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  122. lua_settop(L, 1);
  123. if (lua_isnil(L, 1))
  124. (*sethookf)(L, NULL);
  125. else if (lua_isfunction(L, 1))
  126. (*sethookf)(L, hook);
  127. else
  128. luaL_argerror(L, 1, l_s("function expected"));
  129. lua_getregistry(L);
  130. lua_pushuserdata(L, key);
  131. lua_pushvalue(L, -1); /* dup key */
  132. lua_gettable(L, -3); /* get old value */
  133. lua_pushvalue(L, -2); /* key (again) */
  134. lua_pushvalue(L, 1);
  135. lua_settable(L, -5); /* set new value */
  136. }
  137. static int setcallhook (lua_State *L) {
  138. sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
  139. return 1;
  140. }
  141. static int setlinehook (lua_State *L) {
  142. sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
  143. return 1;
  144. }
  145. static const luaL_reg dblib[] = {
  146. {l_s("getlocal"), getlocal},
  147. {l_s("getinfo"), getinfo},
  148. {l_s("setcallhook"), setcallhook},
  149. {l_s("setlinehook"), setlinehook},
  150. {l_s("setlocal"), setlocal}
  151. };
  152. LUALIB_API int lua_dblibopen (lua_State *L) {
  153. luaL_openl(L, dblib);
  154. return 0;
  155. }