Browse Source

error inside an error method could break the stack.

Roberto Ierusalimschy 28 years ago
parent
commit
4321fde2a7
4 changed files with 41 additions and 28 deletions
  1. 6 21
      fallback.c
  2. 1 2
      fallback.h
  3. 31 4
      opcode.c
  4. 3 1
      opcode.h

+ 6 - 21
fallback.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
  
-char *rcs_fallback="$Id: fallback.c,v 2.8 1997/06/17 17:27:07 roberto Exp roberto $";
+char *rcs_fallback="$Id: fallback.c,v 2.10 1997/07/03 22:06:06 roberto Exp $";
 
 #include <stdio.h>
 #include <string.h>
@@ -238,34 +238,19 @@ void luaI_settagmethod (void)
 }
 
 
-static void stderrorim (void)
-{
-  lua_Object s = lua_getparam(1);
-  if (lua_isstring(s))
-    fprintf(stderr, "lua: %s\n", lua_getstring(s));
-}
-
-static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
-
-
-TObject *luaI_geterrorim (void)
-{
-  return &errorim;
-}
-
 void luaI_seterrormethod (void)
 {
   lua_Object func = lua_getparam(1);
   luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
                  1, "function expected");
-  luaI_pushobject(&errorim);
-  errorim = *luaI_Address(func);
+  luaI_pushobject(&luaI_errorim);
+  luaI_errorim = *luaI_Address(func);
 }
 
 char *luaI_travfallbacks (int (*fn)(TObject *))
 {
   int e;
-  if (fn(&errorim))
+  if (fn(&luaI_errorim))
     return "error";
   for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {  /* ORDER IM */
     int t;
@@ -322,8 +307,8 @@ void luaI_setfallback (void)
   luaL_arg_check(lua_isfunction(func), 2, "function expected");
   switch (luaI_findstring(name, oldnames)) {
     case 0:  /* old error fallback */
-      oldfunc = errorim;
-      errorim = *luaI_Address(func);
+      oldfunc = luaI_errorim;
+      luaI_errorim = *luaI_Address(func);
       replace = errorFB;
       break;
     case 1:  /* old getglobal fallback */

+ 1 - 2
fallback.h

@@ -1,5 +1,5 @@
 /*
-** $Id: fallback.h,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $
+** $Id: fallback.h,v 1.24 1997/07/03 22:06:06 roberto Exp $
 */
  
 #ifndef fallback_h
@@ -54,7 +54,6 @@ char *luaI_travfallbacks (int (*fn)(TObject *));
 
 void luaI_settag (int tag, TObject *o);
 void luaI_realtag (int tag);
-TObject *luaI_geterrorim (void);
 int luaI_efectivetag (TObject *o);
 void luaI_settagmethod (void);
 void luaI_gettagmethod (void);

+ 31 - 4
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 4.15 1997/06/26 21:40:57 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 4.17 1997/07/03 22:06:06 roberto Exp $";
 
 #include <setjmp.h>
 #include <stdio.h>
@@ -465,12 +465,39 @@ void lua_travstack (int (*fn)(TObject *))
 ** Error messages and debug functions
 */
 
+
+static void auxerrorim (char *form)
+{
+  lua_Object s = lua_getparam(1);
+  if (lua_isstring(s))
+    fprintf(stderr, form, lua_getstring(s));
+}
+
+
+static void emergencyerrorf (void)
+{
+  auxerrorim("WARNING - THERE WAS AN ERROR INSIDE AN ERROR METHOD:\n%s\n");
+}
+
+
+static void stderrorim (void)
+{
+  auxerrorim("lua: %s\n");
+}
+
+
+TObject luaI_errorim = {LUA_T_CFUNCTION, {stderrorim}};
+
+
 static void lua_message (char *s)
 {
-  TObject *im = luaI_geterrorim();
-  if (ttype(im) != LUA_T_NIL) {
+  TObject im = luaI_errorim;
+  if (ttype(&im) != LUA_T_NIL) {
+    luaI_errorim.ttype = LUA_T_CFUNCTION;
+    luaI_errorim.value.f = emergencyerrorf;
     lua_pushstring(s);
-    callIM(im, 1, 0);
+    callIM(&im, 1, 0);
+    luaI_errorim = im;
   }
 }
 

+ 3 - 1
opcode.h

@@ -1,6 +1,6 @@
 /*
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.33 1997/04/11 21:34:53 roberto Exp roberto $
+** $Id: opcode.h,v 3.35 1997/07/03 22:06:06 roberto Exp $
 */
 
 #ifndef opcode_h
@@ -168,4 +168,6 @@ void    luaI_gcIM       (TObject *o);
 int     luaI_dorun (TFunc *tf);
 int     lua_domain (void);
 
+extern TObject luaI_errorim;
+
 #endif