1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00

new way to handle errors

This commit is contained in:
Roberto Ierusalimschy 2002-04-22 11:40:23 -03:00
parent 30ad4c75db
commit f388ee4a82
8 changed files with 32 additions and 34 deletions

View File

@ -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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -280,7 +280,7 @@ static int aux_unpack (lua_State *L, int arg) {
int n, i; int n, i;
luaL_check_type(L, arg, LUA_TTABLE); luaL_check_type(L, arg, LUA_TTABLE);
n = lua_getn(L, arg); 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] */ for (i=1; i<=n; i++) /* push arg[1...n] */
lua_rawgeti(L, arg, i); lua_rawgeti(L, arg, i);
return n; return n;
@ -299,10 +299,10 @@ static int luaB_call (lua_State *L) {
int status; int status;
int n; int n;
if (!lua_isnone(L, 4)) { /* set new error method */ if (!lua_isnone(L, 4)) { /* set new error method */
lua_getglobal(L, LUA_ERRORMESSAGE); lua_getglobal(L, "_ERRORMESSAGE");
err = lua_gettop(L); /* get index */ err = lua_gettop(L); /* get index */
lua_pushvalue(L, 4); lua_pushvalue(L, 4);
lua_setglobal(L, LUA_ERRORMESSAGE); lua_setglobal(L, "_ERRORMESSAGE");
} }
oldtop = lua_gettop(L); /* top before function-call preparation */ oldtop = lua_gettop(L); /* top before function-call preparation */
/* push function */ /* push function */
@ -311,7 +311,7 @@ static int luaB_call (lua_State *L) {
status = lua_call(L, n, LUA_MULTRET); status = lua_call(L, n, LUA_MULTRET);
if (err != 0) { /* restore old error method */ if (err != 0) { /* restore old error method */
lua_pushvalue(L, err); lua_pushvalue(L, err);
lua_setglobal(L, LUA_ERRORMESSAGE); lua_setglobal(L, "_ERRORMESSAGE");
} }
if (status != 0) { /* error in call? */ if (status != 0) { /* error in call? */
if (strchr(options, 'x')) if (strchr(options, 'x'))
@ -460,7 +460,7 @@ static int luaB_require (lua_State *L) {
static const luaL_reg base_funcs[] = { static const luaL_reg base_funcs[] = {
{LUA_ALERT, luaB__ALERT}, {LUA_ALERT, luaB__ALERT},
{LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, {"_ERRORMESSAGE", luaB__ERRORMESSAGE},
{"error", luaB_error}, {"error", luaB_error},
{"metatable", luaB_metatable}, {"metatable", luaB_metatable},
{"globals", luaB_globals}, {"globals", luaB_globals},

View File

@ -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 ** Debug Interface
** See Copyright Notice in lua.h ** 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) if (ttype(func) == LUA_TFUNCTION)
cl = clvalue(func); cl = clvalue(func);
else { 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 */ cl = NULL; /* to avoid warnings */
} }
if (cl->c.isC) { if (cl->c.isC) {

8
lmem.c
View File

@ -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 ** Interface to Memory Manager
** See Copyright Notice in lua.h ** 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? */ else if (*size >= limit/2) { /* cannot double it? */
if (*size < limit - MINSIZEARRAY) /* try something smaller... */ if (*size < limit - MINSIZEARRAY) /* try something smaller... */
newsize = limit; /* still have at least MINSIZEARRAY free places */ newsize = limit; /* still have at least MINSIZEARRAY free places */
else luaD_error(L, errormsg); else luaD_runerror(L, errormsg);
} }
newblock = luaM_realloc(L, block, newblock = luaM_realloc(L, block,
cast(lu_mem, *size)*cast(lu_mem, size_elems), 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; block = NULL;
} }
else if (size >= MAX_SIZET) else if (size >= MAX_SIZET)
luaD_error(L, "memory allocation error: block too big"); luaD_runerror(L, "memory allocation error: block too big");
else { else {
block = l_realloc(block, oldsize, size); block = l_realloc(block, oldsize, size);
if (block == NULL) { if (block == NULL) {
if (L) 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! */ else return NULL; /* error before creating state! */
} }
} }

View File

@ -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 ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -97,7 +97,7 @@ void luaO_verror (lua_State *L, const char *fmt, ...) {
va_start(argp, fmt); va_start(argp, fmt);
vsprintf(buff, fmt, argp); vsprintf(buff, fmt, argp);
va_end(argp); va_end(argp);
luaD_error(L, buff); luaD_runerror(L, buff);
} }

View File

@ -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 ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -124,7 +124,7 @@ LUA_API lua_State *lua_open (void) {
preinit_state(L); preinit_state(L);
L->l_G = NULL; L->l_G = NULL;
L->next = L->previous = L; 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 */ /* memory allocation error: free partial state */
close_state(L); close_state(L);
L = NULL; L = NULL;

View File

@ -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) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -111,7 +111,7 @@ static int luaH_index (lua_State *L, Table *t, const TObject *key) {
else { else {
const TObject *v = luaH_get(t, key); const TObject *v = luaH_get(t, key);
if (v == &luaO_nilobject) 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) - i = cast(int, (cast(const lu_byte *, v) -
cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node)); cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node));
return i + t->sizearray; /* hash elements are numbered after array ones */ 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; int size;
if (lsize < MINHASHSIZE) lsize = MINHASHSIZE; if (lsize < MINHASHSIZE) lsize = MINHASHSIZE;
else if (lsize > MAXBITS) else if (lsize > MAXBITS)
luaD_error(L, "table overflow"); luaD_runerror(L, "table overflow");
size = twoto(lsize); size = twoto(lsize);
t->node = luaM_newvector(L, size, Node); t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) { 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); settableval(p, val);
} }
else { 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); newkey(L, t, key, val);
} }
t->flags = 0; t->flags = 0;

10
lua.h
View File

@ -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 ** 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: info@lua.org ** e-mail: info@lua.org
@ -22,9 +22,6 @@
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo" #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' */ /* option for multiple returns in `lua_call' */
#define LUA_MULTRET (-1) #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 int lua_call (lua_State *L, int nargs, int nresults);
LUA_API void lua_rawcall (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_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, LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
const char *name); 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, LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name); const char *name);

16
lvm.c
View File

@ -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 ** Lua virtual machine
** See Copyright Notice in lua.h ** 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) if (ttype(tm) == LUA_TFUNCTION)
callTMres(L, tm, t, key, res); callTMres(L, tm, t, key, res);
else { else {
if (++loop == MAXTAGLOOP) luaD_error(L, "loop in gettable"); if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in gettable");
t = tm; t = tm;
goto init; /* return luaV_gettable(L, tm, key, res); */ 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) if (ttype(tm) == LUA_TFUNCTION)
callTM(L, tm, t, key, val); callTM(L, tm, t, key, val);
else { else {
if (++loop == MAXTAGLOOP) luaD_error(L, "loop in settable"); if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in settable");
t = tm; t = tm;
goto init; /* luaV_settable(L, tm, key, val); */ 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; tl += tsvalue(top-n-1)->tsv.len;
n++; 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); buffer = luaO_openspace(L, tl, char);
tl = 0; tl = 0;
for (i=n; i>0; i--) { /* concat all strings */ 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")); setsvalue(&o, luaS_newliteral(L, "pow"));
luaV_gettable(L, gt(L), &o, &f); luaV_gettable(L, gt(L), &o, &f);
if (ttype(&f) != LUA_TFUNCTION) 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); callTMres(L, &f, b, c, ra);
} }
else else
@ -535,11 +535,11 @@ StkId luaV_execute (lua_State *L) {
const TObject *plimit = ra+1; const TObject *plimit = ra+1;
const TObject *pstep = ra+2; const TObject *pstep = ra+2;
if (ttype(ra) != LUA_TNUMBER) 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)) 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)) 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); step = nvalue(pstep);
index = nvalue(ra) + step; /* increment index */ index = nvalue(ra) + step; /* increment index */
limit = nvalue(plimit); limit = nvalue(plimit);