ldblib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ** $Id: ldblib.c,v 1.28 2000/11/06 13:19:08 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. 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_pushstring(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_checkany(L, 3);
  92. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  93. return 1;
  94. }
  95. /* dummy variables (to define unique addresses) */
  96. static char key1, key2;
  97. #define KEY_CALLHOOK (&key1)
  98. #define KEY_LINEHOOK (&key2)
  99. static void hookf (lua_State *L, void *key) {
  100. lua_getregistry(L);
  101. lua_pushuserdata(L, key);
  102. lua_gettable(L, -2);
  103. if (lua_isfunction(L, -1)) {
  104. lua_pushvalue(L, 1);
  105. lua_rawcall(L, 1, 0);
  106. }
  107. else
  108. lua_pop(L, 1); /* pop result from gettable */
  109. lua_pop(L, 1); /* pop table */
  110. }
  111. static void callf (lua_State *L, lua_Debug *ar) {
  112. lua_pushstring(L, ar->event);
  113. hookf(L, KEY_CALLHOOK);
  114. }
  115. static void linef (lua_State *L, lua_Debug *ar) {
  116. lua_pushnumber(L, ar->currentline);
  117. hookf(L, KEY_LINEHOOK);
  118. }
  119. static void sethook (lua_State *L, void *key, lua_Hook hook,
  120. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  121. lua_settop(L, 1);
  122. if (lua_isnil(L, 1))
  123. (*sethookf)(L, NULL);
  124. else if (lua_isfunction(L, 1))
  125. (*sethookf)(L, hook);
  126. else
  127. luaL_argerror(L, 1, "function expected");
  128. lua_getregistry(L);
  129. lua_pushuserdata(L, key);
  130. lua_pushvalue(L, -1); /* dup key */
  131. lua_gettable(L, -3); /* get old value */
  132. lua_pushvalue(L, -2); /* key (again) */
  133. lua_pushvalue(L, 1);
  134. lua_settable(L, -5); /* set new value */
  135. }
  136. static int setcallhook (lua_State *L) {
  137. sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
  138. return 1;
  139. }
  140. static int setlinehook (lua_State *L) {
  141. sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
  142. return 1;
  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. LUALIB_API void lua_dblibopen (lua_State *L) {
  152. luaL_openl(L, dblib);
  153. }