diff options
Diffstat (limited to 'ldebug.c')
-rw-r--r-- | ldebug.c | 66 |
1 files changed, 58 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldebug.c,v 1.1 1999/12/14 18:31:20 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 1.2 1999/12/23 18:19:57 roberto Exp roberto $ |
3 | ** Debug Interface | 3 | ** Debug Interface |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -8,6 +8,8 @@ | |||
8 | #define LUA_REENTRANT | 8 | #define LUA_REENTRANT |
9 | 9 | ||
10 | #include "lapi.h" | 10 | #include "lapi.h" |
11 | #include "lauxlib.h" | ||
12 | #include "ldebug.h" | ||
11 | #include "lfunc.h" | 13 | #include "lfunc.h" |
12 | #include "lobject.h" | 14 | #include "lobject.h" |
13 | #include "lstate.h" | 15 | #include "lstate.h" |
@@ -17,6 +19,10 @@ | |||
17 | #include "luadebug.h" | 19 | #include "luadebug.h" |
18 | 20 | ||
19 | 21 | ||
22 | static int hasdebuginfo (lua_State *L, lua_Function f) { | ||
23 | return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE); | ||
24 | } | ||
25 | |||
20 | 26 | ||
21 | lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) { | 27 | lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) { |
22 | lua_LHFunction old = L->linehook; | 28 | lua_LHFunction old = L->linehook; |
@@ -52,6 +58,29 @@ lua_Function lua_stackedfunction (lua_State *L, int level) { | |||
52 | } | 58 | } |
53 | 59 | ||
54 | 60 | ||
61 | const char *luaG_getname (lua_State *L, const char **name) { | ||
62 | lua_Function f = lua_stackedfunction(L, 0); | ||
63 | if (f == LUA_NOOBJECT || !hasdebuginfo(L, f) || ttype(f+2) == LUA_T_NIL) | ||
64 | return NULL; /* no name available */ | ||
65 | else { | ||
66 | int i = (f+2)->value.i; | ||
67 | if (ttype(f) == LUA_T_LCLMARK) | ||
68 | f = protovalue(f); | ||
69 | LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function"); | ||
70 | LUA_ASSERT(L, ttype(&tfvalue(f)->consts[i]) == LUA_T_STRING, ""); | ||
71 | *name = tsvalue(&tfvalue(f)->consts[i])->str; | ||
72 | switch (ttype(f+2)) { | ||
73 | case LUA_T_NGLOBAL: return "global"; | ||
74 | case LUA_T_NLOCAL: return "local"; | ||
75 | case LUA_T_NDOT: return "field"; | ||
76 | default: | ||
77 | LUA_INTERNALERROR(L, "invalid tag for NAME"); | ||
78 | return NULL; /* unreacheable; to avoid warnings */ | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | |||
55 | int lua_nups (lua_State *L, lua_Function f) { | 84 | int lua_nups (lua_State *L, lua_Function f) { |
56 | UNUSED(L); | 85 | UNUSED(L); |
57 | switch (luaA_normalizedtype(f)) { | 86 | switch (luaA_normalizedtype(f)) { |
@@ -64,7 +93,7 @@ int lua_nups (lua_State *L, lua_Function f) { | |||
64 | 93 | ||
65 | 94 | ||
66 | int lua_currentline (lua_State *L, lua_Function f) { | 95 | int lua_currentline (lua_State *L, lua_Function f) { |
67 | return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1; | 96 | return hasdebuginfo(L, f) ? (f+1)->value.i : -1; |
68 | } | 97 | } |
69 | 98 | ||
70 | 99 | ||
@@ -77,9 +106,10 @@ lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number, | |||
77 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; | 106 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; |
78 | *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f)); | 107 | *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f)); |
79 | if (*name) { | 108 | if (*name) { |
80 | /* if "*name", there must be a LUA_T_LINE */ | 109 | /* if "*name", there must be a LUA_T_LINE and a NAME */ |
81 | /* therefore, f+2 points to function base */ | 110 | /* therefore, f+3 points to function base */ |
82 | return luaA_putluaObject(L, (f+2)+(local_number-1)); | 111 | LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, ""); |
112 | return luaA_putluaObject(L, (f+3)+(local_number-1)); | ||
83 | } | 113 | } |
84 | else | 114 | else |
85 | return LUA_NOOBJECT; | 115 | return LUA_NOOBJECT; |
@@ -98,9 +128,8 @@ int lua_setlocal (lua_State *L, lua_Function f, int local_number) { | |||
98 | luaA_checkCparams(L, 1); | 128 | luaA_checkCparams(L, 1); |
99 | --L->top; | 129 | --L->top; |
100 | if (name) { | 130 | if (name) { |
101 | /* if "name", there must be a LUA_T_LINE */ | 131 | LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, ""); |
102 | /* therefore, f+2 points to function base */ | 132 | *((f+3)+(local_number-1)) = *L->top; |
103 | *((f+2)+(local_number-1)) = *L->top; | ||
104 | return 1; | 133 | return 1; |
105 | } | 134 | } |
106 | else | 135 | else |
@@ -148,3 +177,24 @@ const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) { | |||
148 | else return ""; /* not found at all */ | 177 | else return ""; /* not found at all */ |
149 | } | 178 | } |
150 | 179 | ||
180 | static void call_index_error (lua_State *L, TObject *o, const char *tp, | ||
181 | const char *v) { | ||
182 | const char *name; | ||
183 | const char *kind = luaG_getname(L, &name); | ||
184 | if (kind) { /* is there a name? */ | ||
185 | luaL_verror(L, "%.10s `%.30s' is not a %.10s", kind, name, tp); | ||
186 | } | ||
187 | else { | ||
188 | luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o)); | ||
189 | } | ||
190 | } | ||
191 | |||
192 | |||
193 | void luaG_callerror (lua_State *L, TObject *func) { | ||
194 | call_index_error(L, func, "function", "call"); | ||
195 | } | ||
196 | |||
197 | |||
198 | void luaG_indexerror (lua_State *L, TObject *t) { | ||
199 | call_index_error(L, t, "table", "index"); | ||
200 | } | ||