From 9998082839bfffe01d7b614ab61ad35629a2c356 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 Aug 2002 17:08:41 -0300 Subject: [PATCH] external messages add their own extra information --- lapi.c | 4 ++-- lauxlib.c | 18 +++++++++++++++++- lauxlib.h | 3 ++- lbaselib.c | 13 +++++++++++-- ldebug.c | 17 ++++++----------- ldebug.h | 4 ++-- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lapi.c b/lapi.c index 4902be21..d87bf134 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.209 2002/08/06 18:54:18 roberto Exp roberto $ +** $Id: lapi.c,v 1.210 2002/08/07 14:24:24 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -695,7 +695,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) { LUA_API int lua_error (lua_State *L) { lua_lock(L); api_checknelems(L, 1); - luaG_errormsg(L, 0); + luaG_errormsg(L); lua_unlock(L); return 0; /* to avoid warnings */ } diff --git a/lauxlib.c b/lauxlib.c index c37135e2..4ad7e0a2 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.81 2002/08/06 17:26:45 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.82 2002/08/06 18:01:50 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -62,11 +62,27 @@ static void tag_error (lua_State *L, int narg, int tag) { } +LUALIB_API void luaL_where (lua_State *L, int level) { + lua_Debug ar; + if (lua_getstack(L, level, &ar)) { /* check function at level */ + lua_getinfo(L, "Snl", &ar); /* get info about it */ + if (ar.currentline > 0) { /* is there info? */ + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); + return; + } + } + lua_pushliteral(L, ""); /* else, no information available... */ +} + + LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); + luaL_where(L, 1); lua_pushvfstring(L, fmt, argp); va_end(argp); + lua_pushliteral(L, "\n"); + lua_concat(L, 3); return lua_error(L); } diff --git a/lauxlib.h b/lauxlib.h index e61e509a..f3010626 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.50 2002/06/25 19:15:21 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.51 2002/07/01 19:23:58 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -44,6 +44,7 @@ LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *msg); LUALIB_API void luaL_check_type (lua_State *L, int narg, int t); LUALIB_API void luaL_check_any (lua_State *L, int narg); +LUALIB_API void luaL_where (lua_State *L, int level); LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); LUALIB_API int luaL_findstring (const char *name, diff --git a/lbaselib.c b/lbaselib.c index 01091602..16f84ec4 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.95 2002/08/06 18:01:50 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.96 2002/08/06 18:54:18 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -75,7 +75,16 @@ static int luaB_tonumber (lua_State *L) { static int luaB_error (lua_State *L) { + int level = luaL_opt_int(L, 2, 1); luaL_check_any(L, 1); + if (!lua_isstring(L, 1) || level == 0) + lua_pushvalue(L, 1); /* propagate error mesage without changes */ + else { /* add extra information */ + luaL_where(L, level); + lua_pushvalue(L, 1); + lua_pushliteral(L, "\n"); + lua_concat(L, 3); + } return lua_error(L); } @@ -285,7 +294,7 @@ static int luaB_unpack (lua_State *L) { lua_rawget(L, 1); n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1; for (i=0; itop - 1); +static void addinfo (lua_State *L, const char *msg) { CallInfo *ci = L->ci; - if (!internal && ci > L->base_ci) ci--; - if (strchr(msg, '\n')) return; /* message already `formatted' */ if (!isLua(ci)) { /* no Lua code? */ - luaO_pushfstring(L, "%s\n", msg); /* no extra info */ + luaO_pushfstring(L, "%s\n", msg); /* no extra info; just add '\n' */ } else { /* add file:line information */ char buff[LUA_IDSIZE]; @@ -535,9 +532,7 @@ static void addinfo (lua_State *L, int internal) { } -void luaG_errormsg (lua_State *L, int internal) { - if (ttisstring(L->top - 1)) - addinfo(L, internal); +void luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); @@ -553,8 +548,8 @@ void luaG_errormsg (lua_State *L, int internal) { void luaG_runerror (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); - luaO_pushvfstring(L, fmt, argp); + addinfo(L, luaO_pushvfstring(L, fmt, argp)); va_end(argp); - luaG_errormsg(L, 1); + luaG_errormsg(L); } diff --git a/ldebug.h b/ldebug.h index 19a96be4..fe9eb48a 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 1.27 2002/08/06 15:32:22 roberto Exp roberto $ +** $Id: ldebug.h,v 1.28 2002/08/06 18:01:50 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -27,7 +27,7 @@ void luaG_concaterror (lua_State *L, StkId p1, StkId p2); void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2); int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2); void luaG_runerror (lua_State *L, const char *fmt, ...); -void luaG_errormsg (lua_State *L, int internal); +void luaG_errormsg (lua_State *L); int luaG_checkcode (const Proto *pt);