ldblib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** $Id: ldblib.c,v 1.10 2000/01/19 12:00:45 roberto Exp roberto $
  3. ** Interface from Lua to its debug API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define LUA_REENTRANT
  9. #include "lauxlib.h"
  10. #include "lua.h"
  11. #include "luadebug.h"
  12. #include "lualib.h"
  13. static void settabss (lua_State *L, lua_Object t, const char *i, const char *v) {
  14. lua_pushobject(L, t);
  15. lua_pushstring(L, i);
  16. lua_pushstring(L, v);
  17. lua_settable(L);
  18. }
  19. static void settabsi (lua_State *L, lua_Object t, const char *i, int v) {
  20. lua_pushobject(L, t);
  21. lua_pushstring(L, i);
  22. lua_pushnumber(L, v);
  23. lua_settable(L);
  24. }
  25. static void settabso (lua_State *L, lua_Object t, const char *i, lua_Object v) {
  26. lua_pushobject(L, t);
  27. lua_pushstring(L, i);
  28. lua_pushobject(L, v);
  29. lua_settable(L);
  30. }
  31. static void getstack (lua_State *L) {
  32. lua_Dbgactreg ar;
  33. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  34. return;
  35. else {
  36. const char *options = luaL_check_string(L, 2);
  37. lua_Object res = lua_createtable(L);
  38. if (!lua_getinfo(L, options, &ar))
  39. luaL_argerror(L, 2, "invalid option");
  40. for (; *options; options++) {
  41. switch (*options) {
  42. case 'S':
  43. settabss(L, res, "source", ar.source);
  44. settabsi(L, res, "linedefined", ar.linedefined);
  45. settabss(L, res, "what", ar.what);
  46. break;
  47. case 'l':
  48. settabsi(L, res, "currentline", ar.currentline);
  49. break;
  50. case 'u':
  51. settabsi(L, res, "nups", ar.nups);
  52. break;
  53. case 'n':
  54. settabss(L, res, "name", ar.name);
  55. settabss(L, res, "namewhat", ar.namewhat);
  56. break;
  57. case 'f':
  58. settabso(L, res, "func", ar.func);
  59. break;
  60. }
  61. }
  62. lua_pushobject(L, res);
  63. }
  64. }
  65. static void getlocal (lua_State *L) {
  66. lua_Dbgactreg ar;
  67. lua_Dbglocvar lvar;
  68. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  69. luaL_argerror(L, 1, "level out of range");
  70. lvar.index = luaL_check_int(L, 2);
  71. if (lua_getlocal(L, &ar, &lvar)) {
  72. lua_pushstring(L, lvar.name);
  73. lua_pushobject(L, lvar.value);
  74. }
  75. }
  76. static void setlocal (lua_State *L) {
  77. lua_Dbgactreg ar;
  78. lua_Dbglocvar lvar;
  79. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  80. luaL_argerror(L, 1, "level out of range");
  81. lvar.index = luaL_check_int(L, 2);
  82. lvar.value = luaL_nonnullarg(L, 3);
  83. if (lua_setlocal(L, &ar, &lvar))
  84. lua_pushstring(L, lvar.name);
  85. }
  86. /*
  87. ** because of these variables, this module is not reentrant, and should
  88. ** not be used in multiple states
  89. */
  90. static int linehook = LUA_NOREF; /* Lua reference to line hook function */
  91. static int callhook = LUA_NOREF; /* Lua reference to call hook function */
  92. static void linef (lua_State *L, lua_Dbgactreg *ar) {
  93. if (linehook != LUA_NOREF) {
  94. lua_pushnumber(L, ar->currentline);
  95. lua_callfunction(L, lua_getref(L, linehook));
  96. }
  97. }
  98. static void callf (lua_State *L, lua_Dbgactreg *ar) {
  99. if (callhook != LUA_NOREF) {
  100. lua_pushstring(L, ar->event);
  101. lua_callfunction(L, lua_getref(L, callhook));
  102. }
  103. }
  104. static void setcallhook (lua_State *L) {
  105. lua_Object f = lua_getparam(L, 1);
  106. lua_unref(L, callhook);
  107. if (f == LUA_NOOBJECT) {
  108. callhook = LUA_NOREF;
  109. lua_setcallhook(L, NULL);
  110. }
  111. else {
  112. lua_pushobject(L, f);
  113. callhook = lua_ref(L, 1);
  114. lua_setcallhook(L, callf);
  115. }
  116. }
  117. static void setlinehook (lua_State *L) {
  118. lua_Object f = lua_getparam(L, 1);
  119. lua_unref(L, linehook);
  120. if (f == LUA_NOOBJECT) {
  121. linehook = LUA_NOREF;
  122. lua_setlinehook(L, NULL);
  123. }
  124. else {
  125. lua_pushobject(L, f);
  126. linehook = lua_ref(L, 1);
  127. lua_setlinehook(L, linef);
  128. }
  129. }
  130. static const struct luaL_reg dblib[] = {
  131. {"getlocal", getlocal},
  132. {"getstack", getstack},
  133. {"setcallhook", setcallhook},
  134. {"setlinehook", setlinehook},
  135. {"setlocal", setlocal}
  136. };
  137. void lua_dblibopen (lua_State *L) {
  138. luaL_openl(L, dblib);
  139. }