ldblib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ** $Id: ldblib.c,v 1.40 2001/10/26 17:33:30 roberto Exp $
  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. if (ar.source)
  48. settabss(L, "short_src", ar.short_src);
  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_pushliteral(L, "func");
  64. lua_pushvalue(L, -3);
  65. lua_settable(L, -3);
  66. break;
  67. }
  68. }
  69. return 1; /* return table */
  70. }
  71. static int getlocal (lua_State *L) {
  72. lua_Debug ar;
  73. const char *name;
  74. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  75. luaL_argerror(L, 1, "level out of range");
  76. name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  77. if (name) {
  78. lua_pushstring(L, name);
  79. lua_pushvalue(L, -2);
  80. return 2;
  81. }
  82. else {
  83. lua_pushnil(L);
  84. return 1;
  85. }
  86. }
  87. static int setlocal (lua_State *L) {
  88. lua_Debug ar;
  89. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  90. luaL_argerror(L, 1, "level out of range");
  91. luaL_check_any(L, 3);
  92. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  93. return 1;
  94. }
  95. #define KEY_CALLHOOK "luadblibCallhook"
  96. #define KEY_LINEHOOK "luadblibLinehook"
  97. static void hookf (lua_State *L, const char *key) {
  98. lua_pushstring(L, key);
  99. lua_gettable(L, LUA_REGISTRYINDEX);
  100. if (lua_isfunction(L, -1)) {
  101. lua_pushvalue(L, -2); /* original argument (below function) */
  102. lua_rawcall(L, 1, 0);
  103. }
  104. else
  105. lua_pop(L, 1); /* pop result from gettable */
  106. }
  107. static void callf (lua_State *L, lua_Debug *ar) {
  108. lua_pushstring(L, ar->event);
  109. hookf(L, KEY_CALLHOOK);
  110. }
  111. static void linef (lua_State *L, lua_Debug *ar) {
  112. lua_pushnumber(L, ar->currentline);
  113. hookf(L, KEY_LINEHOOK);
  114. }
  115. static void sethook (lua_State *L, const char *key, lua_Hook hook,
  116. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  117. lua_settop(L, 1);
  118. if (lua_isnil(L, 1))
  119. (*sethookf)(L, NULL);
  120. else if (lua_isfunction(L, 1))
  121. (*sethookf)(L, hook);
  122. else
  123. luaL_argerror(L, 1, "function expected");
  124. lua_pushstring(L, key);
  125. lua_gettable(L, LUA_REGISTRYINDEX); /* get old value */
  126. lua_pushstring(L, key);
  127. lua_pushvalue(L, 1);
  128. lua_settable(L, LUA_REGISTRYINDEX); /* set new value */
  129. }
  130. static int setcallhook (lua_State *L) {
  131. sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
  132. return 1;
  133. }
  134. static int setlinehook (lua_State *L) {
  135. sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
  136. return 1;
  137. }
  138. static const luaL_reg dblib[] = {
  139. {"getlocal", getlocal},
  140. {"getinfo", getinfo},
  141. {"setcallhook", setcallhook},
  142. {"setlinehook", setlinehook},
  143. {"setlocal", setlocal}
  144. };
  145. LUALIB_API int lua_dblibopen (lua_State *L) {
  146. luaL_openl(L, dblib);
  147. return 0;
  148. }