diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-18 12:19:27 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-18 12:19:27 -0300 |
commit | 1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b (patch) | |
tree | e845de9ee24b94362146e38410ae0670df18526b /lauxlib.c | |
parent | 8f080fd683d63b0cd4b38380f6a5bdae5d6e2584 (diff) | |
download | lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.tar.gz lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.tar.bz2 lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.zip |
new protocol for error handling
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 49 |
1 files changed, 14 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.73 2002/06/05 16:59:37 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.74 2002/06/13 13:44:50 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -40,13 +40,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { | |||
40 | if (strcmp(ar.namewhat, "method") == 0) { | 40 | if (strcmp(ar.namewhat, "method") == 0) { |
41 | narg--; /* do not count `self' */ | 41 | narg--; /* do not count `self' */ |
42 | if (narg == 0) /* error is in the self argument itself? */ | 42 | if (narg == 0) /* error is in the self argument itself? */ |
43 | return luaL_verror(L, | 43 | return luaL_error(L, |
44 | "calling %s on bad self (perhaps using `:' instead of `.')", | 44 | "calling %s on bad self (perhaps using `:' instead of `.')", |
45 | ar.name); | 45 | ar.name); |
46 | } | 46 | } |
47 | if (ar.name == NULL) | 47 | if (ar.name == NULL) |
48 | ar.name = "?"; | 48 | ar.name = "?"; |
49 | return luaL_verror(L, "bad argument #%d to `%s' (%s)", | 49 | return luaL_error(L, "bad argument #%d to `%s' (%s)", |
50 | narg, ar.name, extramsg); | 50 | narg, ar.name, extramsg); |
51 | } | 51 | } |
52 | 52 | ||
@@ -63,19 +63,12 @@ static void tag_error (lua_State *L, int narg, int tag) { | |||
63 | } | 63 | } |
64 | 64 | ||
65 | 65 | ||
66 | LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { | 66 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { |
67 | lua_Debug ar; | ||
68 | const char *msg; | ||
69 | va_list argp; | 67 | va_list argp; |
70 | va_start(argp, fmt); | 68 | va_start(argp, fmt); |
71 | msg = lua_pushvfstring(L, fmt, argp); | 69 | lua_pushvfstring(L, fmt, argp); |
72 | va_end(argp); | 70 | va_end(argp); |
73 | if (lua_getstack(L, 1, &ar)) { /* check calling function */ | 71 | return lua_error(L); |
74 | lua_getinfo(L, "Snl", &ar); | ||
75 | if (ar.currentline > 0) | ||
76 | lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg); | ||
77 | } | ||
78 | return lua_errorobj(L); | ||
79 | } | 72 | } |
80 | 73 | ||
81 | /* }====================================================== */ | 74 | /* }====================================================== */ |
@@ -92,7 +85,7 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | |||
92 | 85 | ||
93 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { | 86 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { |
94 | if (!lua_checkstack(L, space)) | 87 | if (!lua_checkstack(L, space)) |
95 | luaL_verror(L, "stack overflow (%s)", mes); | 88 | luaL_error(L, "stack overflow (%s)", mes); |
96 | } | 89 | } |
97 | 90 | ||
98 | 91 | ||
@@ -397,35 +390,21 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, | |||
397 | 390 | ||
398 | static void callalert (lua_State *L, int status) { | 391 | static void callalert (lua_State *L, int status) { |
399 | if (status != 0) { | 392 | if (status != 0) { |
400 | int top = lua_gettop(L); | 393 | int top; |
394 | if (status == LUA_ERRRUN) | ||
395 | lua_concat(L, 2); /* concat error message and traceback */ | ||
396 | top = lua_gettop(L); | ||
401 | lua_getglobal(L, "_ALERT"); | 397 | lua_getglobal(L, "_ALERT"); |
402 | lua_insert(L, -2); | 398 | lua_insert(L, -2); |
403 | lua_pcall(L, 1, 0, 0); | 399 | lua_pcall(L, 1, 0); |
404 | lua_settop(L, top-1); | 400 | lua_settop(L, top-1); |
405 | } | 401 | } |
406 | } | 402 | } |
407 | 403 | ||
408 | 404 | ||
409 | LUALIB_API int lua_call (lua_State *L, int nargs, int nresults) { | ||
410 | int status; | ||
411 | int errpos = lua_gettop(L) - nargs; | ||
412 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
413 | lua_insert(L, errpos); /* put below function and args */ | ||
414 | status = lua_pcall(L, nargs, nresults, errpos); | ||
415 | lua_remove(L, errpos); | ||
416 | callalert(L, status); | ||
417 | return status; | ||
418 | } | ||
419 | |||
420 | |||
421 | static int aux_do (lua_State *L, int status) { | 405 | static int aux_do (lua_State *L, int status) { |
422 | if (status == 0) { /* parse OK? */ | 406 | if (status == 0) /* parse OK? */ |
423 | int err = lua_gettop(L); | 407 | status = lua_pcall(L, 0, LUA_MULTRET); /* call main */ |
424 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
425 | lua_insert(L, err); | ||
426 | status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */ | ||
427 | lua_remove(L, err); /* remove error function */ | ||
428 | } | ||
429 | callalert(L, status); | 408 | callalert(L, status); |
430 | return status; | 409 | return status; |
431 | } | 410 | } |