ldblib.c 4.2 KB

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