ldblib.c 4.2 KB

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