ldblib.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. ** $Id: ldblib.c,v 1.1 1999/01/08 16:47:44 roberto Exp roberto $
  3. ** Interface from Lua to its debug API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lauxlib.h"
  9. #include "lua.h"
  10. #include "luadebug.h"
  11. #include "lualib.h"
  12. static void settabss (lua_Object t, char *i, char *v) {
  13. lua_pushobject(t);
  14. lua_pushstring(i);
  15. lua_pushstring(v);
  16. lua_settable();
  17. }
  18. static void settabsi (lua_Object t, char *i, int v) {
  19. lua_pushobject(t);
  20. lua_pushstring(i);
  21. lua_pushnumber(v);
  22. lua_settable();
  23. }
  24. static lua_Object getfuncinfo (lua_Object func) {
  25. lua_Object result = lua_createtable();
  26. char *name;
  27. int line;
  28. lua_funcinfo(func, &name, &line);
  29. if (line == -1) /* C function? */
  30. settabss(result, "kind", "C");
  31. else if (line == 0) { /* "main"? */
  32. settabss(result, "kind", "chunk");
  33. settabss(result, "name", name);
  34. }
  35. else { /* Lua function */
  36. settabss(result, "kind", "Lua");
  37. settabsi(result, "def_line", line);
  38. settabss(result, "def_chunk", name);
  39. }
  40. if (line != 0) { /* is it not a "main"? */
  41. char *kind = lua_getobjname(func, &name);
  42. if (*kind) {
  43. settabss(result, "name", name);
  44. settabss(result, "where", kind);
  45. }
  46. }
  47. return result;
  48. }
  49. static void getstack (void) {
  50. lua_Object func = lua_stackedfunction(luaL_check_int(1));
  51. if (func == LUA_NOOBJECT) /* level out of range? */
  52. return;
  53. else {
  54. lua_Object result = getfuncinfo(func);
  55. int currline = lua_currentline(func);
  56. if (currline > 0)
  57. settabsi(result, "current", currline);
  58. lua_pushobject(result);
  59. }
  60. }
  61. static void funcinfo (void) {
  62. lua_pushobject(getfuncinfo(luaL_functionarg(1)));
  63. }
  64. static int findlocal (lua_Object func, int arg) {
  65. lua_Object v = lua_getparam(arg);
  66. if (lua_isnumber(v))
  67. return (int)lua_getnumber(v);
  68. else {
  69. char *name = luaL_check_string(arg);
  70. int i = 0;
  71. int result = -1;
  72. char *vname;
  73. while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) {
  74. if (strcmp(name, vname) == 0)
  75. result = i; /* keep looping to get the last var with this name */
  76. }
  77. if (result == -1)
  78. luaL_verror("no local variable `%.50s' at given level", name);
  79. return result;
  80. }
  81. }
  82. static void getlocal (void) {
  83. lua_Object func = lua_stackedfunction(luaL_check_int(1));
  84. lua_Object val;
  85. char *name;
  86. if (func == LUA_NOOBJECT) /* level out of range? */
  87. return; /* return nil */
  88. else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */
  89. if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) {
  90. lua_pushobject(val);
  91. lua_pushstring(name);
  92. }
  93. /* else return nil */
  94. }
  95. else { /* collect all locals in a table */
  96. lua_Object result = lua_createtable();
  97. int i;
  98. for (i=1; ;i++) {
  99. if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT)
  100. break;
  101. lua_pushobject(result);
  102. lua_pushstring(name);
  103. lua_pushobject(val);
  104. lua_settable(); /* result[name] = value */
  105. }
  106. lua_pushobject(result);
  107. }
  108. }
  109. static void setlocal (void) {
  110. lua_Object func = lua_stackedfunction(luaL_check_int(1));
  111. int numvar;
  112. luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
  113. numvar = findlocal(func, 2);
  114. lua_pushobject(luaL_nonnullarg(3));
  115. if (!lua_setlocal(func, numvar))
  116. lua_error("no such local variable");
  117. }
  118. static int linehook = -1; /* Lua reference to line hook function */
  119. static int callhook = -1; /* Lua reference to call hook function */
  120. static void dohook (int ref) {
  121. lua_LHFunction oldlinehook = lua_linehook; /* save old hooks */
  122. lua_CHFunction oldcallhook = lua_callhook;
  123. lua_linehook = NULL; lua_callhook = NULL; /* to avoid recusive calls */
  124. lua_callfunction(lua_getref(ref));
  125. lua_linehook = oldlinehook; /* restore old hooks */
  126. lua_callhook = oldcallhook;
  127. }
  128. static void linef (int line) {
  129. lua_pushnumber(line);
  130. dohook(linehook);
  131. }
  132. static void callf (lua_Function func, char *file, int line) {
  133. if (func != LUA_NOOBJECT) {
  134. lua_pushobject(func);
  135. lua_pushstring(file);
  136. lua_pushnumber(line);
  137. }
  138. dohook(callhook);
  139. }
  140. static void setcallhook (void) {
  141. lua_Object f = lua_getparam(1);
  142. lua_unref(callhook);
  143. if (f == LUA_NOOBJECT) {
  144. callhook = -1;
  145. lua_callhook = NULL;
  146. }
  147. else {
  148. lua_pushobject(f);
  149. callhook = lua_ref(1);
  150. lua_callhook = callf;
  151. }
  152. }
  153. static void setlinehook (void) {
  154. lua_Object f = lua_getparam(1);
  155. lua_unref(linehook);
  156. if (f == LUA_NOOBJECT) {
  157. linehook = -1;
  158. lua_linehook = NULL;
  159. }
  160. else {
  161. lua_pushobject(f);
  162. linehook = lua_ref(1);
  163. lua_linehook = linef;
  164. }
  165. }
  166. static struct luaL_reg dblib[] = {
  167. {"funcinfo", funcinfo},
  168. {"getlocal", getlocal},
  169. {"getstack", getstack},
  170. {"setcallhook", setcallhook},
  171. {"setlinehook", setlinehook},
  172. {"setlocal", setlocal}
  173. };
  174. void lua_dblibopen (void) {
  175. luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0])));
  176. }