ldebug.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ** $Id: ldebug.c,v 1.1 1999/12/14 18:31:20 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include "lapi.h"
  8. #include "lfunc.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "ltable.h"
  12. #include "ltm.h"
  13. #include "lua.h"
  14. #include "luadebug.h"
  15. lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
  16. lua_LHFunction old = L->linehook;
  17. L->linehook = func;
  18. return old;
  19. }
  20. lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
  21. lua_CHFunction old = L->callhook;
  22. L->callhook = func;
  23. return old;
  24. }
  25. int lua_setdebug (lua_State *L, int debug) {
  26. int old = L->debug;
  27. L->debug = debug;
  28. return old;
  29. }
  30. lua_Function lua_stackedfunction (lua_State *L, int level) {
  31. int i;
  32. for (i = (L->top-1)-L->stack; i>=0; i--) {
  33. if (is_T_MARK(L->stack[i].ttype)) {
  34. if (level == 0)
  35. return L->stack+i;
  36. level--;
  37. }
  38. }
  39. return LUA_NOOBJECT;
  40. }
  41. int lua_nups (lua_State *L, lua_Function f) {
  42. UNUSED(L);
  43. switch (luaA_normalizedtype(f)) {
  44. case LUA_T_LCLOSURE: case LUA_T_CCLOSURE:
  45. return f->value.cl->nelems;
  46. default:
  47. return 0;
  48. }
  49. }
  50. int lua_currentline (lua_State *L, lua_Function f) {
  51. return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
  52. }
  53. lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
  54. const char **name) {
  55. /* check whether `f' is a Lua function */
  56. if (lua_tag(L, f) != LUA_T_LPROTO)
  57. return LUA_NOOBJECT;
  58. else {
  59. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  60. *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
  61. if (*name) {
  62. /* if "*name", there must be a LUA_T_LINE */
  63. /* therefore, f+2 points to function base */
  64. return luaA_putluaObject(L, (f+2)+(local_number-1));
  65. }
  66. else
  67. return LUA_NOOBJECT;
  68. }
  69. }
  70. int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
  71. /* check whether `f' is a Lua function */
  72. if (lua_tag(L, f) != LUA_T_LPROTO)
  73. return 0;
  74. else {
  75. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  76. const char *name = luaF_getlocalname(fp, local_number,
  77. lua_currentline(L, f));
  78. luaA_checkCparams(L, 1);
  79. --L->top;
  80. if (name) {
  81. /* if "name", there must be a LUA_T_LINE */
  82. /* therefore, f+2 points to function base */
  83. *((f+2)+(local_number-1)) = *L->top;
  84. return 1;
  85. }
  86. else
  87. return 0;
  88. }
  89. }
  90. void lua_funcinfo (lua_State *L, lua_Object func,
  91. const char **source, int *linedefined) {
  92. if (!lua_isfunction(L, func))
  93. lua_error(L, "API error - `funcinfo' called with a non-function value");
  94. else {
  95. const TObject *f = luaA_protovalue(func);
  96. if (luaA_normalizedtype(f) == LUA_T_LPROTO) {
  97. *source = tfvalue(f)->source->str;
  98. *linedefined = tfvalue(f)->lineDefined;
  99. }
  100. else {
  101. *source = "(C)";
  102. *linedefined = -1;
  103. }
  104. }
  105. }
  106. static int checkfunc (lua_State *L, TObject *o) {
  107. return luaO_equalObj(o, L->top);
  108. }
  109. const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
  110. /* try to find a name for given function */
  111. GlobalVar *g;
  112. luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */
  113. for (g=L->rootglobal; g; g=g->next) {
  114. if (checkfunc(L, &g->value)) {
  115. *name = g->name->str;
  116. return "global";
  117. }
  118. }
  119. /* not found: try tag methods */
  120. if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
  121. return "tag-method";
  122. else return ""; /* not found at all */
  123. }