commit f388ee4a822b3d8027ed7c28aa21e9406e4a11eb
parent 30ad4c75db38639f52fb1c8be1c5a3f5b58e776f
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 22 Apr 2002 11:40:01 -0300
new way to handle errors
Diffstat:
8 files changed, 32 insertions(+), 34 deletions(-)
diff --git a/lbaselib.c b/lbaselib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbaselib.c,v 1.67 2002/04/12 19:57:29 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.68 2002/04/15 20:54:41 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -280,7 +280,7 @@ static int aux_unpack (lua_State *L, int arg) {
int n, i;
luaL_check_type(L, arg, LUA_TTABLE);
n = lua_getn(L, arg);
- luaL_check_stack(L, n, "table too big to unpack");
+ luaL_check_stack(L, n+LUA_MINSTACK, "table too big to unpack");
for (i=1; i<=n; i++) /* push arg[1...n] */
lua_rawgeti(L, arg, i);
return n;
@@ -299,10 +299,10 @@ static int luaB_call (lua_State *L) {
int status;
int n;
if (!lua_isnone(L, 4)) { /* set new error method */
- lua_getglobal(L, LUA_ERRORMESSAGE);
+ lua_getglobal(L, "_ERRORMESSAGE");
err = lua_gettop(L); /* get index */
lua_pushvalue(L, 4);
- lua_setglobal(L, LUA_ERRORMESSAGE);
+ lua_setglobal(L, "_ERRORMESSAGE");
}
oldtop = lua_gettop(L); /* top before function-call preparation */
/* push function */
@@ -311,7 +311,7 @@ static int luaB_call (lua_State *L) {
status = lua_call(L, n, LUA_MULTRET);
if (err != 0) { /* restore old error method */
lua_pushvalue(L, err);
- lua_setglobal(L, LUA_ERRORMESSAGE);
+ lua_setglobal(L, "_ERRORMESSAGE");
}
if (status != 0) { /* error in call? */
if (strchr(options, 'x'))
@@ -460,7 +460,7 @@ static int luaB_require (lua_State *L) {
static const luaL_reg base_funcs[] = {
{LUA_ALERT, luaB__ALERT},
- {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
+ {"_ERRORMESSAGE", luaB__ERRORMESSAGE},
{"error", luaB_error},
{"metatable", luaB_metatable},
{"globals", luaB_globals},
diff --git a/ldebug.c b/ldebug.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldebug.c,v 1.107 2002/04/09 19:47:44 roberto Exp roberto $
+** $Id: ldebug.c,v 1.108 2002/04/10 12:11:07 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@@ -144,7 +144,7 @@ static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
if (ttype(func) == LUA_TFUNCTION)
cl = clvalue(func);
else {
- luaD_error(L, "value for `lua_getinfo' is not a function");
+ luaD_runerror(L, "value for `lua_getinfo' is not a function");
cl = NULL; /* to avoid warnings */
}
if (cl->c.isC) {
diff --git a/lmem.c b/lmem.c
@@ -1,5 +1,5 @@
/*
-** $Id: lmem.c,v 1.51 2001/10/25 19:13:33 roberto Exp $
+** $Id: lmem.c,v 1.52 2001/11/28 20:13:13 roberto Exp roberto $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
@@ -34,7 +34,7 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
else if (*size >= limit/2) { /* cannot double it? */
if (*size < limit - MINSIZEARRAY) /* try something smaller... */
newsize = limit; /* still have at least MINSIZEARRAY free places */
- else luaD_error(L, errormsg);
+ else luaD_runerror(L, errormsg);
}
newblock = luaM_realloc(L, block,
cast(lu_mem, *size)*cast(lu_mem, size_elems),
@@ -53,12 +53,12 @@ void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
block = NULL;
}
else if (size >= MAX_SIZET)
- luaD_error(L, "memory allocation error: block too big");
+ luaD_runerror(L, "memory allocation error: block too big");
else {
block = l_realloc(block, oldsize, size);
if (block == NULL) {
if (L)
- luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
+ luaD_error(L, NULL, LUA_ERRMEM); /* break run without error message */
else return NULL; /* error before creating state! */
}
}
diff --git a/lobject.c b/lobject.c
@@ -1,5 +1,5 @@
/*
-** $Id: lobject.c,v 1.75 2002/02/07 17:25:12 roberto Exp roberto $
+** $Id: lobject.c,v 1.76 2002/04/05 18:54:31 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@@ -97,7 +97,7 @@ void luaO_verror (lua_State *L, const char *fmt, ...) {
va_start(argp, fmt);
vsprintf(buff, fmt, argp);
va_end(argp);
- luaD_error(L, buff);
+ luaD_runerror(L, buff);
}
diff --git a/lstate.c b/lstate.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.c,v 1.88 2002/03/20 12:52:32 roberto Exp roberto $
+** $Id: lstate.c,v 1.89 2002/04/16 17:08:28 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -124,7 +124,7 @@ LUA_API lua_State *lua_open (void) {
preinit_state(L);
L->l_G = NULL;
L->next = L->previous = L;
- if (luaD_runprotected(L, f_luaopen, NULL) != 0) {
+ if (luaD_runprotected(L, f_luaopen, &luaO_nilobject, NULL) != 0) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;
diff --git a/ltable.c b/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 1.102 2002/03/18 18:18:35 roberto Exp roberto $
+** $Id: ltable.c,v 1.103 2002/04/05 18:54:31 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -111,7 +111,7 @@ static int luaH_index (lua_State *L, Table *t, const TObject *key) {
else {
const TObject *v = luaH_get(t, key);
if (v == &luaO_nilobject)
- luaD_error(L, "invalid key for `next'");
+ luaD_runerror(L, "invalid key for `next'");
i = cast(int, (cast(const lu_byte *, v) -
cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node));
return i + t->sizearray; /* hash elements are numbered after array ones */
@@ -220,7 +220,7 @@ static void setnodevector (lua_State *L, Table *t, int lsize) {
int size;
if (lsize < MINHASHSIZE) lsize = MINHASHSIZE;
else if (lsize > MAXBITS)
- luaD_error(L, "table overflow");
+ luaD_runerror(L, "table overflow");
size = twoto(lsize);
t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) {
@@ -441,7 +441,7 @@ void luaH_set (lua_State *L, Table *t, const TObject *key, const TObject *val) {
settableval(p, val);
}
else {
- if (ttype(key) == LUA_TNIL) luaD_error(L, "table index is nil");
+ if (ttype(key) == LUA_TNIL) luaD_runerror(L, "table index is nil");
newkey(L, t, key, val);
}
t->flags = 0;
diff --git a/lua.h b/lua.h
@@ -1,5 +1,5 @@
/*
-** $Id: lua.h,v 1.126 2002/04/05 18:54:31 roberto Exp roberto $
+** $Id: lua.h,v 1.127 2002/04/16 17:08:28 roberto Exp roberto $
** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org
@@ -22,9 +22,6 @@
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
-/* name of global variable with error handler */
-#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
-
/* option for multiple returns in `lua_call' */
#define LUA_MULTRET (-1)
@@ -173,11 +170,12 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex);
*/
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
+LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf);
LUA_API int lua_loadfile (lua_State *L, const char *filename);
-LUA_API int lua_dofile (lua_State *L, const char *filename);
-LUA_API int lua_dostring (lua_State *L, const char *str);
LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
const char *name);
+LUA_API int lua_dofile (lua_State *L, const char *filename);
+LUA_API int lua_dostring (lua_State *L, const char *str);
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name);
diff --git a/lvm.c b/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 1.224 2002/04/09 19:47:44 roberto Exp roberto $
+** $Id: lvm.c,v 1.225 2002/04/10 12:11:07 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -134,7 +134,7 @@ void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
if (ttype(tm) == LUA_TFUNCTION)
callTMres(L, tm, t, key, res);
else {
- if (++loop == MAXTAGLOOP) luaD_error(L, "loop in gettable");
+ if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in gettable");
t = tm;
goto init; /* return luaV_gettable(L, tm, key, res); */
}
@@ -164,7 +164,7 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
if (ttype(tm) == LUA_TFUNCTION)
callTM(L, tm, t, key, val);
else {
- if (++loop == MAXTAGLOOP) luaD_error(L, "loop in settable");
+ if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in settable");
t = tm;
goto init; /* luaV_settable(L, tm, key, val); */
}
@@ -241,7 +241,7 @@ void luaV_strconc (lua_State *L, int total, int last) {
tl += tsvalue(top-n-1)->tsv.len;
n++;
}
- if (tl > MAX_SIZET) luaD_error(L, "string size overflow");
+ if (tl > MAX_SIZET) luaD_runerror(L, "string size overflow");
buffer = luaO_openspace(L, tl, char);
tl = 0;
for (i=n; i>0; i--) { /* concat all strings */
@@ -266,7 +266,7 @@ static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
setsvalue(&o, luaS_newliteral(L, "pow"));
luaV_gettable(L, gt(L), &o, &f);
if (ttype(&f) != LUA_TFUNCTION)
- luaD_error(L, "`pow' (for `^' operator) is not a function");
+ luaD_runerror(L, "`pow' (for `^' operator) is not a function");
callTMres(L, &f, b, c, ra);
}
else
@@ -535,11 +535,11 @@ StkId luaV_execute (lua_State *L) {
const TObject *plimit = ra+1;
const TObject *pstep = ra+2;
if (ttype(ra) != LUA_TNUMBER)
- luaD_error(L, "`for' initial value must be a number");
+ luaD_runerror(L, "`for' initial value must be a number");
if (!tonumber(plimit, ra+1))
- luaD_error(L, "`for' limit must be a number");
+ luaD_runerror(L, "`for' limit must be a number");
if (!tonumber(pstep, ra+2))
- luaD_error(L, "`for' step must be a number");
+ luaD_runerror(L, "`for' step must be a number");
step = nvalue(pstep);
index = nvalue(ra) + step; /* increment index */
limit = nvalue(plimit);