diff options
Diffstat (limited to 'ldblib.c')
-rw-r--r-- | ldblib.c | 57 |
1 files changed, 25 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldblib.c,v 1.48 2002/04/22 14:40:50 roberto Exp roberto $ | 2 | ** $Id: ldblib.c,v 1.49 2002/05/01 20:40:42 roberto Exp roberto $ |
3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -47,9 +47,9 @@ static int getinfo (lua_State *L) { | |||
47 | options = buff; | 47 | options = buff; |
48 | } | 48 | } |
49 | else | 49 | else |
50 | luaL_argerror(L, 1, "function or level expected"); | 50 | return luaL_argerror(L, 1, "function or level expected"); |
51 | if (!lua_getinfo(L, options, &ar)) | 51 | if (!lua_getinfo(L, options, &ar)) |
52 | luaL_argerror(L, 2, "invalid option"); | 52 | return luaL_argerror(L, 2, "invalid option"); |
53 | lua_newtable(L); | 53 | lua_newtable(L); |
54 | for (; *options; options++) { | 54 | for (; *options; options++) { |
55 | switch (*options) { | 55 | switch (*options) { |
@@ -85,7 +85,7 @@ static int getlocal (lua_State *L) { | |||
85 | lua_Debug ar; | 85 | lua_Debug ar; |
86 | const char *name; | 86 | const char *name; |
87 | if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */ | 87 | if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */ |
88 | luaL_argerror(L, 1, "level out of range"); | 88 | return luaL_argerror(L, 1, "level out of range"); |
89 | name = lua_getlocal(L, &ar, luaL_check_int(L, 2)); | 89 | name = lua_getlocal(L, &ar, luaL_check_int(L, 2)); |
90 | if (name) { | 90 | if (name) { |
91 | lua_pushstring(L, name); | 91 | lua_pushstring(L, name); |
@@ -102,7 +102,7 @@ static int getlocal (lua_State *L) { | |||
102 | static int setlocal (lua_State *L) { | 102 | static int setlocal (lua_State *L) { |
103 | lua_Debug ar; | 103 | lua_Debug ar; |
104 | if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */ | 104 | if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */ |
105 | luaL_argerror(L, 1, "level out of range"); | 105 | return luaL_argerror(L, 1, "level out of range"); |
106 | luaL_check_any(L, 3); | 106 | luaL_check_any(L, 3); |
107 | lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2))); | 107 | lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2))); |
108 | return 1; | 108 | return 1; |
@@ -187,20 +187,19 @@ static int errorfb (lua_State *L) { | |||
187 | int level = 1; /* skip level 0 (it's this function) */ | 187 | int level = 1; /* skip level 0 (it's this function) */ |
188 | int firstpart = 1; /* still before eventual `...' */ | 188 | int firstpart = 1; /* still before eventual `...' */ |
189 | lua_Debug ar; | 189 | lua_Debug ar; |
190 | luaL_Buffer b; | 190 | luaL_check_string(L, 1); |
191 | luaL_buffinit(L, &b); | 191 | lua_settop(L, 1); |
192 | luaL_addstring(&b, luaL_check_string(L, 1)); | 192 | lua_pushliteral(L, "\n"); |
193 | luaL_addstring(&b, "\n"); | ||
194 | while (lua_getstack(L, level++, &ar)) { | 193 | while (lua_getstack(L, level++, &ar)) { |
195 | char buff[120]; /* enough to fit following `sprintf's */ | 194 | char buff[10]; |
196 | if (level == 2) | 195 | if (level == 2) |
197 | luaL_addstring(&b, "stack traceback:\n"); | 196 | lua_pushliteral(L, "stack traceback:\n"); |
198 | else if (level > LEVELS1 && firstpart) { | 197 | else if (level > LEVELS1 && firstpart) { |
199 | /* no more than `LEVELS2' more levels? */ | 198 | /* no more than `LEVELS2' more levels? */ |
200 | if (!lua_getstack(L, level+LEVELS2, &ar)) | 199 | if (!lua_getstack(L, level+LEVELS2, &ar)) |
201 | level--; /* keep going */ | 200 | level--; /* keep going */ |
202 | else { | 201 | else { |
203 | luaL_addstring(&b, " ...\n"); /* too many levels */ | 202 | lua_pushliteral(L, " ...\n"); /* too many levels */ |
204 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ | 203 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ |
205 | level++; | 204 | level++; |
206 | } | 205 | } |
@@ -208,40 +207,34 @@ static int errorfb (lua_State *L) { | |||
208 | continue; | 207 | continue; |
209 | } | 208 | } |
210 | sprintf(buff, "%4d: ", level-1); | 209 | sprintf(buff, "%4d: ", level-1); |
211 | luaL_addstring(&b, buff); | 210 | lua_pushstring(L, buff); |
212 | lua_getinfo(L, "Snl", &ar); | 211 | lua_getinfo(L, "Snl", &ar); |
213 | switch (*ar.namewhat) { | 212 | switch (*ar.namewhat) { |
214 | case 'g': case 'l': /* global, local */ | 213 | case 'g': case 'l': /* global, local */ |
215 | sprintf(buff, "function `%.50s'", ar.name); | 214 | luaL_vstr(L, "function `%s'", ar.name); |
216 | break; | 215 | break; |
217 | case 'f': /* field */ | 216 | case 'f': /* field */ |
218 | sprintf(buff, "method `%.50s'", ar.name); | 217 | case 'm': /* method */ |
219 | break; | 218 | luaL_vstr(L, "method `%s'", ar.name); |
220 | case 't': /* tag method */ | ||
221 | sprintf(buff, "`%.50s' tag method", ar.name); | ||
222 | break; | 219 | break; |
223 | default: { | 220 | default: { |
224 | if (*ar.what == 'm') /* main? */ | 221 | if (*ar.what == 'm') /* main? */ |
225 | sprintf(buff, "main of %.70s", ar.short_src); | 222 | luaL_vstr(L, "main of %s", ar.short_src); |
226 | else if (*ar.what == 'C') /* C function? */ | 223 | else if (*ar.what == 'C') /* C function? */ |
227 | sprintf(buff, "%.70s", ar.short_src); | 224 | luaL_vstr(L, "%s", ar.short_src); |
228 | else | 225 | else |
229 | sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); | 226 | luaL_vstr(L, "function <%d:%s>", ar.linedefined, ar.short_src); |
230 | ar.source = NULL; /* do not print source again */ | 227 | ar.source = NULL; /* do not print source again */ |
231 | } | 228 | } |
232 | } | 229 | } |
233 | luaL_addstring(&b, buff); | 230 | if (ar.currentline > 0) |
234 | if (ar.currentline > 0) { | 231 | luaL_vstr(L, " at line %d", ar.currentline); |
235 | sprintf(buff, " at line %d", ar.currentline); | 232 | if (ar.source) |
236 | luaL_addstring(&b, buff); | 233 | luaL_vstr(L, " [%s]", ar.short_src); |
237 | } | 234 | lua_pushliteral(L, "\n"); |
238 | if (ar.source) { | 235 | lua_concat(L, lua_gettop(L)); |
239 | sprintf(buff, " [%.70s]", ar.short_src); | ||
240 | luaL_addstring(&b, buff); | ||
241 | } | ||
242 | luaL_addstring(&b, "\n"); | ||
243 | } | 236 | } |
244 | luaL_pushresult(&b); | 237 | lua_concat(L, lua_gettop(L)); |
245 | return 1; | 238 | return 1; |
246 | } | 239 | } |
247 | 240 | ||