Browse Source

external messages add their own extra information

Roberto Ierusalimschy 23 years ago
parent
commit
9998082839
6 changed files with 40 additions and 19 deletions
  1. 2 2
      lapi.c
  2. 17 1
      lauxlib.c
  3. 2 1
      lauxlib.h
  4. 11 2
      lbaselib.c
  5. 6 11
      ldebug.c
  6. 2 2
      ldebug.h

+ 2 - 2
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 */
 }

+ 17 - 1
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);
 }
 

+ 2 - 1
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, 

+ 11 - 2
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; i<n || n==-1; i++) {  /* push arg[1...n] */
-    luaL_check_stack(L, 1, "table too big to unpack");
+    luaL_check_stack(L, LUA_MINSTACK, "table too big to unpack");
     lua_rawgeti(L, 1, i+1);
     if (n == -1) {  /* no explicit limit? */
       if (lua_isnil(L, -1)) {  /* stop at first `nil' element */

+ 6 - 11
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.129 2002/08/07 14:35:55 roberto Exp roberto $
+** $Id: ldebug.c,v 1.130 2002/08/07 19:22:39 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -518,13 +518,10 @@ int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
 }
 
 
-static void addinfo (lua_State *L, int internal) {
-  const char *msg = svalue(L->top - 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);
 }
 

+ 2 - 2
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);