diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-14 16:31:20 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-14 16:31:20 -0200 |
| commit | c16c63cc592e78f06b5514485c6bd4eb1e7a14b8 (patch) | |
| tree | 8238fc3badaec7865b5260912a5ba55f4cbaf2d4 | |
| parent | dea54a7c719d27aad8c8fde2b234077310eab980 (diff) | |
| download | lua-c16c63cc592e78f06b5514485c6bd4eb1e7a14b8.tar.gz lua-c16c63cc592e78f06b5514485c6bd4eb1e7a14b8.tar.bz2 lua-c16c63cc592e78f06b5514485c6bd4eb1e7a14b8.zip | |
new module with the debug API functions
| -rw-r--r-- | ldebug.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/ldebug.c b/ldebug.c new file mode 100644 index 00000000..88f06563 --- /dev/null +++ b/ldebug.c | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: $ | ||
| 3 | ** Debug Interface | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | |||
| 8 | #define LUA_REENTRANT | ||
| 9 | |||
| 10 | #include "lapi.h" | ||
| 11 | #include "lfunc.h" | ||
| 12 | #include "lobject.h" | ||
| 13 | #include "lstate.h" | ||
| 14 | #include "ltable.h" | ||
| 15 | #include "ltm.h" | ||
| 16 | #include "lua.h" | ||
| 17 | #include "luadebug.h" | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) { | ||
| 22 | lua_LHFunction old = L->linehook; | ||
| 23 | L->linehook = func; | ||
| 24 | return old; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) { | ||
| 29 | lua_CHFunction old = L->callhook; | ||
| 30 | L->callhook = func; | ||
| 31 | return old; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | int lua_setdebug (lua_State *L, int debug) { | ||
| 36 | int old = L->debug; | ||
| 37 | L->debug = debug; | ||
| 38 | return old; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | lua_Function lua_stackedfunction (lua_State *L, int level) { | ||
| 43 | int i; | ||
| 44 | for (i = (L->top-1)-L->stack; i>=0; i--) { | ||
| 45 | int t = L->stack[i].ttype; | ||
| 46 | if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) | ||
| 47 | if (level-- == 0) | ||
| 48 | return L->stack+i; | ||
| 49 | } | ||
| 50 | return LUA_NOOBJECT; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | int lua_nups (lua_State *L, lua_Function f) { | ||
| 55 | UNUSED(L); | ||
| 56 | return (!f || luaA_normalizedtype(f) != LUA_T_CLOSURE) ? 0 : | ||
| 57 | f->value.cl->nelems; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | int lua_currentline (lua_State *L, lua_Function f) { | ||
| 62 | return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number, | ||
| 67 | const char **name) { | ||
| 68 | /* check whether `f' is a Lua function */ | ||
| 69 | if (lua_tag(L, f) != LUA_T_PROTO) | ||
| 70 | return LUA_NOOBJECT; | ||
| 71 | else { | ||
| 72 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; | ||
| 73 | *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f)); | ||
| 74 | if (*name) { | ||
| 75 | /* if "*name", there must be a LUA_T_LINE */ | ||
| 76 | /* therefore, f+2 points to function base */ | ||
| 77 | return luaA_putluaObject(L, (f+2)+(local_number-1)); | ||
| 78 | } | ||
| 79 | else | ||
| 80 | return LUA_NOOBJECT; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | int lua_setlocal (lua_State *L, lua_Function f, int local_number) { | ||
| 86 | /* check whether `f' is a Lua function */ | ||
| 87 | if (lua_tag(L, f) != LUA_T_PROTO) | ||
| 88 | return 0; | ||
| 89 | else { | ||
| 90 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; | ||
| 91 | const char *name = luaF_getlocalname(fp, local_number, | ||
| 92 | lua_currentline(L, f)); | ||
| 93 | luaA_checkCparams(L, 1); | ||
| 94 | --L->top; | ||
| 95 | if (name) { | ||
| 96 | /* if "name", there must be a LUA_T_LINE */ | ||
| 97 | /* therefore, f+2 points to function base */ | ||
| 98 | *((f+2)+(local_number-1)) = *L->top; | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | else | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | void lua_funcinfo (lua_State *L, lua_Object func, | ||
| 108 | const char **source, int *linedefined) { | ||
| 109 | if (!lua_isfunction(L, func)) | ||
| 110 | lua_error(L, "API error - `funcinfo' called with a non-function value"); | ||
| 111 | else { | ||
| 112 | const TObject *f = luaA_protovalue(func); | ||
| 113 | if (luaA_normalizedtype(f) == LUA_T_PROTO) { | ||
| 114 | *source = tfvalue(f)->source->str; | ||
| 115 | *linedefined = tfvalue(f)->lineDefined; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | *source = "(C)"; | ||
| 119 | *linedefined = -1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | static int checkfunc (lua_State *L, TObject *o) { | ||
| 126 | return luaO_equalObj(o, L->top); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) { | ||
| 131 | /* try to find a name for given function */ | ||
| 132 | GlobalVar *g; | ||
| 133 | luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */ | ||
| 134 | for (g=L->rootglobal; g; g=g->next) { | ||
| 135 | if (checkfunc(L, &g->value)) { | ||
| 136 | *name = g->name->str; | ||
| 137 | return "global"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | /* not found: try tag methods */ | ||
| 141 | if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL) | ||
| 142 | return "tag-method"; | ||
| 143 | else return ""; /* not found at all */ | ||
| 144 | } | ||
| 145 | |||
