ldebug.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** $Id: $
  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. int t = L->stack[i].ttype;
  34. if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
  35. if (level-- == 0)
  36. return L->stack+i;
  37. }
  38. return LUA_NOOBJECT;
  39. }
  40. int lua_nups (lua_State *L, lua_Function f) {
  41. UNUSED(L);
  42. return (!f || luaA_normalizedtype(f) != LUA_T_CLOSURE) ? 0 :
  43. f->value.cl->nelems;
  44. }
  45. int lua_currentline (lua_State *L, lua_Function f) {
  46. return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
  47. }
  48. lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
  49. const char **name) {
  50. /* check whether `f' is a Lua function */
  51. if (lua_tag(L, f) != LUA_T_PROTO)
  52. return LUA_NOOBJECT;
  53. else {
  54. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  55. *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
  56. if (*name) {
  57. /* if "*name", there must be a LUA_T_LINE */
  58. /* therefore, f+2 points to function base */
  59. return luaA_putluaObject(L, (f+2)+(local_number-1));
  60. }
  61. else
  62. return LUA_NOOBJECT;
  63. }
  64. }
  65. int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
  66. /* check whether `f' is a Lua function */
  67. if (lua_tag(L, f) != LUA_T_PROTO)
  68. return 0;
  69. else {
  70. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  71. const char *name = luaF_getlocalname(fp, local_number,
  72. lua_currentline(L, f));
  73. luaA_checkCparams(L, 1);
  74. --L->top;
  75. if (name) {
  76. /* if "name", there must be a LUA_T_LINE */
  77. /* therefore, f+2 points to function base */
  78. *((f+2)+(local_number-1)) = *L->top;
  79. return 1;
  80. }
  81. else
  82. return 0;
  83. }
  84. }
  85. void lua_funcinfo (lua_State *L, lua_Object func,
  86. const char **source, int *linedefined) {
  87. if (!lua_isfunction(L, func))
  88. lua_error(L, "API error - `funcinfo' called with a non-function value");
  89. else {
  90. const TObject *f = luaA_protovalue(func);
  91. if (luaA_normalizedtype(f) == LUA_T_PROTO) {
  92. *source = tfvalue(f)->source->str;
  93. *linedefined = tfvalue(f)->lineDefined;
  94. }
  95. else {
  96. *source = "(C)";
  97. *linedefined = -1;
  98. }
  99. }
  100. }
  101. static int checkfunc (lua_State *L, TObject *o) {
  102. return luaO_equalObj(o, L->top);
  103. }
  104. const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
  105. /* try to find a name for given function */
  106. GlobalVar *g;
  107. luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */
  108. for (g=L->rootglobal; g; g=g->next) {
  109. if (checkfunc(L, &g->value)) {
  110. *name = g->name->str;
  111. return "global";
  112. }
  113. }
  114. /* not found: try tag methods */
  115. if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
  116. return "tag-method";
  117. else return ""; /* not found at all */
  118. }