ldebug.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ** $Id: ldebug.c,v 1.3 1999/12/29 16:31:15 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include "lapi.h"
  8. #include "lauxlib.h"
  9. #include "ldebug.h"
  10. #include "lfunc.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "ltable.h"
  14. #include "ltm.h"
  15. #include "lua.h"
  16. #include "luadebug.h"
  17. static int hasdebuginfo (lua_State *L, lua_Function f) {
  18. return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE);
  19. }
  20. lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
  21. lua_LHFunction old = L->linehook;
  22. L->linehook = func;
  23. return old;
  24. }
  25. lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
  26. lua_CHFunction old = L->callhook;
  27. L->callhook = func;
  28. return old;
  29. }
  30. int lua_setdebug (lua_State *L, int debug) {
  31. int old = L->debug;
  32. L->debug = debug;
  33. return old;
  34. }
  35. static lua_Function aux_stackedfunction (lua_State *L, int level, StkId top) {
  36. int i;
  37. for (i = (top-1)-L->stack; i>=0; i--) {
  38. if (is_T_MARK(L->stack[i].ttype)) {
  39. if (level == 0)
  40. return L->stack+i;
  41. level--;
  42. }
  43. }
  44. return LUA_NOOBJECT;
  45. }
  46. lua_Function lua_stackedfunction (lua_State *L, int level) {
  47. return aux_stackedfunction(L, level, L->top);
  48. }
  49. static const char *luaG_getname (lua_State *L, const char **name, StkId top) {
  50. lua_Function f = aux_stackedfunction(L, 0, top);
  51. if (f == LUA_NOOBJECT || !hasdebuginfo(L, f) || ttype(f+2) == LUA_T_NIL)
  52. return ""; /* no name available */
  53. else {
  54. int i = (f+2)->value.i;
  55. if (ttype(f) == LUA_T_LCLMARK)
  56. f = protovalue(f);
  57. LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function");
  58. LUA_ASSERT(L, ttype(&tfvalue(f)->consts[i]) == LUA_T_STRING, "");
  59. *name = tsvalue(&tfvalue(f)->consts[i])->str;
  60. return luaO_typename(f+2);
  61. }
  62. }
  63. int lua_nups (lua_State *L, lua_Function f) {
  64. UNUSED(L);
  65. switch (luaA_normalizedtype(f)) {
  66. case LUA_T_LCLOSURE: case LUA_T_CCLOSURE:
  67. return f->value.cl->nelems;
  68. default:
  69. return 0;
  70. }
  71. }
  72. int lua_currentline (lua_State *L, lua_Function f) {
  73. return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
  74. }
  75. lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
  76. const char **name) {
  77. /* check whether `f' is a Lua function */
  78. if (lua_tag(L, f) != LUA_T_LPROTO)
  79. return LUA_NOOBJECT;
  80. else {
  81. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  82. *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
  83. if (*name) {
  84. /* if "*name", there must be a LUA_T_LINE and a NAME */
  85. /* therefore, f+3 points to function base */
  86. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  87. return luaA_putluaObject(L, (f+3)+(local_number-1));
  88. }
  89. else
  90. return LUA_NOOBJECT;
  91. }
  92. }
  93. int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
  94. /* check whether `f' is a Lua function */
  95. if (lua_tag(L, f) != LUA_T_LPROTO)
  96. return 0;
  97. else {
  98. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  99. const char *name = luaF_getlocalname(fp, local_number,
  100. lua_currentline(L, f));
  101. luaA_checkCparams(L, 1);
  102. --L->top;
  103. if (name) {
  104. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  105. *((f+3)+(local_number-1)) = *L->top;
  106. return 1;
  107. }
  108. else
  109. return 0;
  110. }
  111. }
  112. void lua_funcinfo (lua_State *L, lua_Object func,
  113. const char **source, int *linedefined) {
  114. if (!lua_isfunction(L, func))
  115. lua_error(L, "API error - `funcinfo' called with a non-function value");
  116. else {
  117. const TObject *f = luaA_protovalue(func);
  118. if (luaA_normalizedtype(f) == LUA_T_LPROTO) {
  119. *source = tfvalue(f)->source->str;
  120. *linedefined = tfvalue(f)->lineDefined;
  121. }
  122. else {
  123. *source = "(C)";
  124. *linedefined = -1;
  125. }
  126. }
  127. }
  128. static int checkfunc (lua_State *L, TObject *o) {
  129. return luaO_equalObj(o, L->top);
  130. }
  131. const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
  132. GlobalVar *g;
  133. if (is_T_MARK(ttype(o))) { /* `o' is an active function? */
  134. /* look for caller debug information */
  135. const char *kind = luaG_getname(L, name, o);
  136. if (*kind) return kind;
  137. /* else go through */
  138. }
  139. /* try to find a name for given function */
  140. luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */
  141. for (g=L->rootglobal; g; g=g->next) {
  142. if (checkfunc(L, &g->value)) {
  143. *name = g->name->str;
  144. return "global";
  145. }
  146. }
  147. /* not found: try tag methods */
  148. if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
  149. return "tag-method";
  150. else return ""; /* not found at all */
  151. }
  152. static void call_index_error (lua_State *L, TObject *o, const char *tp,
  153. const char *v) {
  154. const char *name;
  155. const char *kind = luaG_getname(L, &name, L->top);
  156. if (*kind) { /* is there a name? */
  157. luaL_verror(L, "%.10s `%.30s' is not a %.10s", kind, name, tp);
  158. }
  159. else {
  160. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  161. }
  162. }
  163. void luaG_callerror (lua_State *L, TObject *func) {
  164. call_index_error(L, func, "function", "call");
  165. }
  166. void luaG_indexerror (lua_State *L, TObject *t) {
  167. call_index_error(L, t, "table", "index");
  168. }