Browse Source

`panic' function configurable via API

Roberto Ierusalimschy 23 years ago
parent
commit
c11d374c59
5 changed files with 29 additions and 7 deletions
  1. 10 1
      lapi.c
  2. 2 3
      ldo.c
  3. 12 1
      lstate.c
  4. 2 1
      lstate.h
  5. 3 1
      lua.h

+ 10 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.182 2002/04/02 20:43:18 roberto Exp roberto $
+** $Id: lapi.c,v 1.183 2002/04/05 18:54:31 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -98,6 +98,15 @@ LUA_API int lua_checkstack (lua_State *L, int size) {
 }
 }
 
 
 
 
+LUA_API lua_CFunction lua_setpanicf (lua_State *L, lua_CFunction panicf) {
+  lua_CFunction old;
+  lua_lock(L);
+  old = G(L)->panic;
+  G(L)->panic = panicf;
+  lua_unlock(L);
+  return old;
+}
+
 
 
 /*
 /*
 ** basic stack manipulation
 ** basic stack manipulation

+ 2 - 3
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 1.169 2002/04/10 12:10:54 roberto Exp roberto $
+** $Id: ldo.c,v 1.170 2002/04/15 19:34:42 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -501,8 +501,7 @@ void luaD_breakrun (lua_State *L, int errcode) {
     longjmp(L->errorJmp->b, 1);
     longjmp(L->errorJmp->b, 1);
   }
   }
   else {
   else {
-    if (errcode != LUA_ERRMEM && errcode != LUA_ERRERR)
-      message(L, "unable to recover; exiting\n");
+    G(L)->panic(L);
     exit(EXIT_FAILURE);
     exit(EXIT_FAILURE);
   }
   }
 }
 }

+ 12 - 1
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 1.87 2002/03/11 12:45:00 roberto Exp roberto $
+** $Id: lstate.c,v 1.88 2002/03/20 12:52:32 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -25,6 +25,16 @@
 static void close_state (lua_State *L);
 static void close_state (lua_State *L);
 
 
 
 
+/*
+** you can change this function through the official API
+** call `lua_setpanicf'
+*/
+static int default_panic (lua_State *L) {
+  fprintf(stderr, "unable to recover; exiting\n");
+  return 0;
+}
+
+
 static void stack_init (lua_State *L, lua_State *OL) {
 static void stack_init (lua_State *L, lua_State *OL) {
   L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
   L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
   L->stacksize = BASIC_STACK_SIZE;
   L->stacksize = BASIC_STACK_SIZE;
@@ -52,6 +62,7 @@ static void f_luaopen (lua_State *L, void *ud) {
   G(L)->strt.hash = NULL;
   G(L)->strt.hash = NULL;
   G(L)->Mbuffer = NULL;
   G(L)->Mbuffer = NULL;
   G(L)->Mbuffsize = 0;
   G(L)->Mbuffsize = 0;
+  G(L)->panic = &default_panic;
   G(L)->rootproto = NULL;
   G(L)->rootproto = NULL;
   G(L)->rootcl = NULL;
   G(L)->rootcl = NULL;
   G(L)->roottable = NULL;
   G(L)->roottable = NULL;

+ 2 - 1
lstate.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.h,v 1.81 2002/03/26 20:46:10 roberto Exp roberto $
+** $Id: lstate.h,v 1.82 2002/04/10 12:11:07 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -112,6 +112,7 @@ typedef struct global_State {
   size_t Mbuffsize;  /* size of Mbuffer */
   size_t Mbuffsize;  /* size of Mbuffer */
   lu_mem GCthreshold;
   lu_mem GCthreshold;
   lu_mem nblocks;  /* number of `bytes' currently allocated */
   lu_mem nblocks;  /* number of `bytes' currently allocated */
+  lua_CFunction panic;  /* to be called in unprotected errors */
   TString *tmname[TM_N];  /* array with tag-method names */
   TString *tmname[TM_N];  /* array with tag-method names */
 } global_State;
 } global_State;
 
 

+ 3 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.125 2002/03/27 15:30:41 roberto Exp roberto $
+** $Id: lua.h,v 1.126 2002/04/05 18:54:31 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -100,6 +100,8 @@ LUA_API void       lua_close (lua_State *L);
 LUA_API lua_State *lua_newthread (lua_State *L);
 LUA_API lua_State *lua_newthread (lua_State *L);
 LUA_API void       lua_closethread (lua_State *L, lua_State *thread);
 LUA_API void       lua_closethread (lua_State *L, lua_State *thread);
 
 
+LUA_API lua_CFunction lua_setpanicf (lua_State *L, lua_CFunction panicf);
+
 
 
 /*
 /*
 ** basic stack manipulation
 ** basic stack manipulation