ldblib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** $Id: ldblib.c,v 1.9 1999/12/21 18:04:41 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. static int linehook = LUA_NOREF; /* Lua reference to line hook function */
  87. static int callhook = LUA_NOREF; /* Lua reference to call hook function */
  88. static void linef (lua_State *L, lua_Dbgactreg *ar) {
  89. if (linehook != LUA_NOREF) {
  90. lua_pushnumber(L, ar->currentline);
  91. lua_callfunction(L, lua_getref(L, linehook));
  92. }
  93. }
  94. static void callf (lua_State *L, lua_Dbgactreg *ar) {
  95. if (callhook != LUA_NOREF) {
  96. lua_pushstring(L, ar->event);
  97. lua_callfunction(L, lua_getref(L, callhook));
  98. }
  99. }
  100. static void setcallhook (lua_State *L) {
  101. lua_Object f = lua_getparam(L, 1);
  102. lua_unref(L, callhook);
  103. if (f == LUA_NOOBJECT) {
  104. callhook = LUA_NOREF;
  105. lua_setcallhook(L, NULL);
  106. }
  107. else {
  108. lua_pushobject(L, f);
  109. callhook = lua_ref(L, 1);
  110. lua_setcallhook(L, callf);
  111. }
  112. }
  113. static void setlinehook (lua_State *L) {
  114. lua_Object f = lua_getparam(L, 1);
  115. lua_unref(L, linehook);
  116. if (f == LUA_NOOBJECT) {
  117. linehook = LUA_NOREF;
  118. lua_setlinehook(L, NULL);
  119. }
  120. else {
  121. lua_pushobject(L, f);
  122. linehook = lua_ref(L, 1);
  123. lua_setlinehook(L, linef);
  124. }
  125. }
  126. static const struct luaL_reg dblib[] = {
  127. {"getlocal", getlocal},
  128. {"getstack", getstack},
  129. {"setcallhook", setcallhook},
  130. {"setlinehook", setlinehook},
  131. {"setlocal", setlocal}
  132. };
  133. void lua_dblibopen (lua_State *L) {
  134. luaL_openl(L, dblib);
  135. }