2
0

ldblib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ** $Id: ldblib.c,v 1.20 2000/09/05 19:33:32 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. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "luadebug.h"
  12. #include "lualib.h"
  13. static void settabss (lua_State *L, const char *i, const char *v) {
  14. lua_pushstring(L, i);
  15. lua_pushstring(L, v);
  16. lua_settable(L, -3);
  17. }
  18. static void settabsi (lua_State *L, const char *i, int v) {
  19. lua_pushstring(L, i);
  20. lua_pushnumber(L, v);
  21. lua_settable(L, -3);
  22. }
  23. static int getinfo (lua_State *L) {
  24. lua_Debug ar;
  25. const char *options = luaL_opt_string(L, 2, "flnSu");
  26. char buff[20];
  27. if (lua_isnumber(L, 1)) {
  28. if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
  29. lua_pushnil(L); /* level out of range */
  30. return 1;
  31. }
  32. }
  33. else if (lua_isfunction(L, 1)) {
  34. lua_pushvalue(L, 1);
  35. sprintf(buff, ">%.10s", options);
  36. options = buff;
  37. }
  38. else
  39. luaL_argerror(L, 1, "function or level expected");
  40. if (!lua_getinfo(L, options, &ar))
  41. luaL_argerror(L, 2, "invalid option");
  42. lua_newtable(L);
  43. for (; *options; options++) {
  44. switch (*options) {
  45. case 'S':
  46. settabss(L, "source", ar.source);
  47. if (ar.source)
  48. settabss(L, "short_src", ar.short_src);
  49. settabsi(L, "linedefined", ar.linedefined);
  50. settabss(L, "what", ar.what);
  51. break;
  52. case 'l':
  53. settabsi(L, "currentline", ar.currentline);
  54. break;
  55. case 'u':
  56. settabsi(L, "nups", ar.nups);
  57. break;
  58. case 'n':
  59. settabss(L, "name", ar.name);
  60. settabss(L, "namewhat", ar.namewhat);
  61. break;
  62. case 'f':
  63. lua_pushstring(L, "func");
  64. lua_pushvalue(L, -3);
  65. lua_settable(L, -3);
  66. break;
  67. }
  68. }
  69. return 1; /* return table */
  70. }
  71. static int getlocal (lua_State *L) {
  72. lua_Debug ar;
  73. const char *name;
  74. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  75. luaL_argerror(L, 1, "level out of range");
  76. name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  77. if (name) {
  78. lua_pushstring(L, name);
  79. lua_pushvalue(L, -2);
  80. return 2;
  81. }
  82. else {
  83. lua_pushnil(L);
  84. return 1;
  85. }
  86. }
  87. static int setlocal (lua_State *L) {
  88. lua_Debug ar;
  89. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  90. luaL_argerror(L, 1, "level out of range");
  91. luaL_checktype(L, 3, "any");
  92. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  93. return 1;
  94. }
  95. /*
  96. ** because of these variables, this module is not reentrant, and should
  97. ** not be used in multiple states
  98. */
  99. static int linehook = LUA_NOREF; /* Lua reference to line hook function */
  100. static int callhook = LUA_NOREF; /* Lua reference to call hook function */
  101. static void linef (lua_State *L, lua_Debug *ar) {
  102. if (linehook != LUA_NOREF) {
  103. lua_getref(L, linehook);
  104. lua_pushnumber(L, ar->currentline);
  105. lua_call(L, 1, 0);
  106. }
  107. }
  108. static void callf (lua_State *L, lua_Debug *ar) {
  109. if (callhook != LUA_NOREF) {
  110. lua_getref(L, callhook);
  111. lua_pushstring(L, ar->event);
  112. lua_call(L, 1, 0);
  113. }
  114. }
  115. static int setcallhook (lua_State *L) {
  116. lua_unref(L, callhook);
  117. if (lua_isnull(L, 1)) {
  118. callhook = LUA_NOREF;
  119. lua_setcallhook(L, NULL);
  120. }
  121. else {
  122. callhook = lua_ref(L, 1);
  123. lua_setcallhook(L, callf);
  124. }
  125. return 0;
  126. }
  127. static int setlinehook (lua_State *L) {
  128. lua_unref(L, linehook);
  129. if (lua_isnull(L, 1)) {
  130. linehook = LUA_NOREF;
  131. lua_setlinehook(L, NULL);
  132. }
  133. else {
  134. linehook = lua_ref(L, 1);
  135. lua_setlinehook(L, linef);
  136. }
  137. return 0;
  138. }
  139. static const struct luaL_reg dblib[] = {
  140. {"getlocal", getlocal},
  141. {"getinfo", getinfo},
  142. {"setcallhook", setcallhook},
  143. {"setlinehook", setlinehook},
  144. {"setlocal", setlocal}
  145. };
  146. void lua_dblibopen (lua_State *L) {
  147. luaL_openl(L, dblib);
  148. }