2
0

ldblib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** $Id: ldblib.c,v 1.12 2000/03/30 17:19:48 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_Debug 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_Debug ar;
  67. lua_Localvar 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. else lua_pushnil(L);
  76. }
  77. static void setlocal (lua_State *L) {
  78. lua_Debug ar;
  79. lua_Localvar lvar;
  80. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  81. luaL_argerror(L, 1, "level out of range");
  82. lvar.index = luaL_check_int(L, 2);
  83. lvar.value = luaL_nonnullarg(L, 3);
  84. if (lua_setlocal(L, &ar, &lvar))
  85. lua_pushstring(L, lvar.name);
  86. else lua_pushnil(L);
  87. }
  88. /*
  89. ** because of these variables, this module is not reentrant, and should
  90. ** not be used in multiple states
  91. */
  92. static int linehook = LUA_NOREF; /* Lua reference to line hook function */
  93. static int callhook = LUA_NOREF; /* Lua reference to call hook function */
  94. static void linef (lua_State *L, lua_Debug *ar) {
  95. if (linehook != LUA_NOREF) {
  96. lua_pushnumber(L, ar->currentline);
  97. lua_callfunction(L, lua_getref(L, linehook));
  98. }
  99. }
  100. static void callf (lua_State *L, lua_Debug *ar) {
  101. if (callhook != LUA_NOREF) {
  102. lua_pushstring(L, ar->event);
  103. lua_callfunction(L, lua_getref(L, callhook));
  104. }
  105. }
  106. static void setcallhook (lua_State *L) {
  107. lua_Object f = lua_getparam(L, 1);
  108. lua_unref(L, callhook);
  109. if (f == LUA_NOOBJECT) {
  110. callhook = LUA_NOREF;
  111. lua_setcallhook(L, NULL);
  112. }
  113. else {
  114. lua_pushobject(L, f);
  115. callhook = lua_ref(L, 1);
  116. lua_setcallhook(L, callf);
  117. }
  118. }
  119. static void setlinehook (lua_State *L) {
  120. lua_Object f = lua_getparam(L, 1);
  121. lua_unref(L, linehook);
  122. if (f == LUA_NOOBJECT) {
  123. linehook = LUA_NOREF;
  124. lua_setlinehook(L, NULL);
  125. }
  126. else {
  127. lua_pushobject(L, f);
  128. linehook = lua_ref(L, 1);
  129. lua_setlinehook(L, linef);
  130. }
  131. }
  132. static const struct luaL_reg dblib[] = {
  133. {"getlocal", getlocal},
  134. {"getstack", getstack},
  135. {"setcallhook", setcallhook},
  136. {"setlinehook", setlinehook},
  137. {"setlocal", setlocal}
  138. };
  139. void lua_dblibopen (lua_State *L) {
  140. luaL_openl(L, dblib);
  141. }