From bb5627f3a4c9c8bf25b836f07708067b1170dde5 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 22 Feb 1999 11:17:24 -0300
Subject: better control over calls to _ALERT and _ERRORMESSAGE (to avoid error
 loops)

---
 lbuiltin.c | 26 ++++++++++++--------------
 ldo.c      | 11 +++++------
 liolib.c   | 11 +++++++----
 3 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/lbuiltin.c b/lbuiltin.c
index 6c13ad32..db7e473c 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lbuiltin.c,v 1.50 1999/02/08 16:29:35 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.51 1999/02/12 19:23:02 roberto Exp roberto $
 ** Built-in functions
 ** See Copyright Notice in lua.h
 */
@@ -82,9 +82,8 @@ static Hash *gethash (int arg) {
 
 
 /*
-** If your system does not support "stderr", remove this function and
-** define your own "_ALERT" function. You *must* have an _ALERT function
-** defined for Lua to work properly.
+** If your system does not support "stderr", redefine this function, or
+** redefine _ERRORMESSAGE so that it won't need _ALERT.
 */
 static void luaB_alert (void) {
   fputs(luaL_check_string(1), stderr);
@@ -96,10 +95,13 @@ static void luaB_alert (void) {
 ** The library "iolib" redefines _ERRORMESSAGE for better error information.
 */
 static void error_message (void) {
-  char buff[600];
-  sprintf(buff, "lua error: %.500s\n", luaL_check_string(1));
-  lua_pushstring(buff);
-  lua_call("_ALERT");
+  lua_Object al = lua_rawgetglobal("_ALERT");
+  if (lua_isfunction(al)) {  /* avoid error loop if _ALERT is not defined */
+    char buff[600];
+    sprintf(buff, "lua error: %.500s\n", luaL_check_string(1));
+    lua_pushstring(buff);
+    lua_callfunction(al);
+  }
 }
 
 
@@ -301,7 +303,6 @@ static void luaB_call (void) {
 /* }====================================================== */
 
 
-#ifdef	EXTRALIB
 /*
 ** {======================================================
 ** "Extra" functions
@@ -501,7 +502,6 @@ static void luaB_sort (void) {
 }
 
 /* }}===================================================== */
-#endif
 
 
 /*
@@ -709,10 +709,9 @@ static struct luaL_reg builtin_funcs[] = {
   {"tag", luaB_luatag},
   {"tonumber", luaB_tonumber},
   {"tostring", luaB_tostring},
-  {"type", luaB_type}
-#ifdef EXTRALIB
+  {"type", luaB_type},
   /* "Extra" functions */
- ,{"assert", luaB_assert},
+  {"assert", luaB_assert},
   {"foreach", luaB_foreach},
   {"foreachi", luaB_foreachi},
   {"foreachvar", luaB_foreachvar},
@@ -720,7 +719,6 @@ static struct luaL_reg builtin_funcs[] = {
   {"sort", luaB_sort},
   {"tinsert", luaB_tinsert},
   {"tremove", luaB_tremove}
-#endif
 };
 
 
diff --git a/ldo.c b/ldo.c
index 146125a6..b5ae4607 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.32 1999/02/12 19:23:02 roberto Exp roberto $
+** $Id: ldo.c,v 1.33 1999/02/22 13:51:44 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -235,7 +235,8 @@ void luaD_travstack (int (*fn)(TObject *))
 
 static void message (char *s) {
   TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
-  if (ttype(em) != LUA_T_NIL) {
+  if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
+      ttype(em) == LUA_T_CLOSURE) {
     *L->stack.top = *em;
     incr_top;
     lua_pushstring(s);
@@ -246,14 +247,12 @@ static void message (char *s) {
 /*
 ** Reports an error, and jumps up to the available recover label
 */
-void lua_error (char *s)
-{
+void lua_error (char *s) {
   if (s) message(s);
   if (L->errorJmp)
     longjmp(*((jmp_buf *)L->errorJmp), 1);
   else {
-    lua_pushstring("lua: exit(1). Unable to recover.\n");
-    lua_call("_ALERT");
+    message("exit(1). Unable to recover.\n");
     exit(1);
   }
 }
diff --git a/liolib.c b/liolib.c
index 1de1b79f..ed40b1ac 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
 /*
-** $Id: liolib.c,v 1.29 1999/01/04 12:41:12 roberto Exp roberto $
+** $Id: liolib.c,v 1.30 1999/02/05 15:22:43 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 */
@@ -459,7 +459,7 @@ static void errorfb (void) {
   char buff[MAXMESSAGE];
   int level = 1;  /* skip level 0 (it's this function) */
   lua_Object func;
-  sprintf(buff, "lua: %.200s\n", lua_getstring(lua_getparam(1)));
+  sprintf(buff, "lua error: %.200s\n", lua_getstring(lua_getparam(1)));
   while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
     char *name;
     int currentline;
@@ -496,8 +496,11 @@ static void errorfb (void) {
       sprintf(buff+strlen(buff), " [in chunk %.50s]", chunkname);
     strcat(buff, "\n");
   }
-  lua_pushstring(buff);
-  lua_call("_ALERT");
+  func = lua_rawgetglobal("_ALERT");
+  if (lua_isfunction(func)) {  /* avoid error loop if _ALERT is not defined */
+    lua_pushstring(buff);
+    lua_callfunction(func);
+  }
 }
 
 
-- 
cgit v1.2.3-55-g6feb