ldblib.c 3.7 KB

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