ldblib.c 3.8 KB

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