ldblib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. ** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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, v);
  15. lua_setstr(L, -2, i);
  16. }
  17. static void settabsi (lua_State *L, const char *i, int v) {
  18. lua_pushnumber(L, v);
  19. lua_setstr(L, -2, i);
  20. }
  21. static int getinfo (lua_State *L) {
  22. lua_Debug ar;
  23. const char *options = luaL_opt_string(L, 2, "flnSu");
  24. char buff[20];
  25. if (lua_isnumber(L, 1)) {
  26. if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
  27. lua_pushnil(L); /* level out of range */
  28. return 1;
  29. }
  30. }
  31. else if (lua_isfunction(L, 1)) {
  32. lua_pushvalue(L, 1);
  33. sprintf(buff, ">%.10s", options);
  34. options = buff;
  35. }
  36. else
  37. luaL_argerror(L, 1, "function or level expected");
  38. if (!lua_getinfo(L, options, &ar))
  39. luaL_argerror(L, 2, "invalid option");
  40. lua_newtable(L);
  41. for (; *options; options++) {
  42. switch (*options) {
  43. case 'S':
  44. settabss(L, "source", ar.source);
  45. if (ar.source)
  46. settabss(L, "short_src", ar.short_src);
  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_pushvalue(L, -2);
  62. lua_setstr(L, -2, "func");
  63. break;
  64. }
  65. }
  66. return 1; /* return table */
  67. }
  68. static int getlocal (lua_State *L) {
  69. lua_Debug ar;
  70. const char *name;
  71. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  72. luaL_argerror(L, 1, "level out of range");
  73. name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  74. if (name) {
  75. lua_pushstring(L, name);
  76. lua_pushvalue(L, -2);
  77. return 2;
  78. }
  79. else {
  80. lua_pushnil(L);
  81. return 1;
  82. }
  83. }
  84. static int setlocal (lua_State *L) {
  85. lua_Debug ar;
  86. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  87. luaL_argerror(L, 1, "level out of range");
  88. luaL_check_any(L, 3);
  89. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  90. return 1;
  91. }
  92. #define KEY_CALLHOOK "luadblibCallhook"
  93. #define KEY_LINEHOOK "luadblibLinehook"
  94. static void hookf (lua_State *L, const char *key) {
  95. lua_getstr(L, LUA_REGISTRYINDEX, key);
  96. if (lua_isfunction(L, -1)) {
  97. lua_pushvalue(L, -2); /* original argument (below function) */
  98. lua_rawcall(L, 1, 0);
  99. }
  100. else
  101. lua_pop(L, 1); /* pop result from gettable */
  102. }
  103. static void callf (lua_State *L, lua_Debug *ar) {
  104. lua_pushstring(L, ar->event);
  105. hookf(L, KEY_CALLHOOK);
  106. }
  107. static void linef (lua_State *L, lua_Debug *ar) {
  108. lua_pushnumber(L, ar->currentline);
  109. hookf(L, KEY_LINEHOOK);
  110. }
  111. static void sethook (lua_State *L, const char *key, lua_Hook hook,
  112. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  113. lua_settop(L, 1);
  114. if (lua_isnil(L, 1))
  115. (*sethookf)(L, NULL);
  116. else if (lua_isfunction(L, 1))
  117. (*sethookf)(L, hook);
  118. else
  119. luaL_argerror(L, 1, "function expected");
  120. lua_getstr(L, LUA_REGISTRYINDEX, key); /* get old value */
  121. lua_pushvalue(L, 1);
  122. lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */
  123. }
  124. static int setcallhook (lua_State *L) {
  125. sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
  126. return 1;
  127. }
  128. static int setlinehook (lua_State *L) {
  129. sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
  130. return 1;
  131. }
  132. static const luaL_reg dblib[] = {
  133. {"getlocal", getlocal},
  134. {"getinfo", getinfo},
  135. {"setcallhook", setcallhook},
  136. {"setlinehook", setlinehook},
  137. {"setlocal", setlocal}
  138. };
  139. LUALIB_API int lua_dblibopen (lua_State *L) {
  140. luaL_openl(L, dblib);
  141. return 0;
  142. }