Skip to content

Commit

Permalink
new way to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed May 1, 2002
1 parent b36b2a0 commit 751cd86
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 292 deletions.
118 changes: 75 additions & 43 deletions lapi.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
** $Id: lapi.c,v 1.184 2002/04/16 17:08:28 roberto Exp roberto $
** $Id: lapi.c,v 1.185 2002/04/22 14:40:50 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/


#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "lua.h"
Expand Down Expand Up @@ -516,7 +518,7 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex) {


/*
** `do' functions (run Lua code)
** `load' and `call' functions (run Lua code)
*/

LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
Expand All @@ -529,17 +531,6 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
}


LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
int status;
int errpos = lua_gettop(L) - nargs;
lua_getglobal(L, "_ERRORMESSAGE");
lua_insert(L, errpos); /* put below function and args */
status = lua_pcall(L, nargs, nresults, errpos);
lua_remove(L, errpos);
return status;
}


LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
int status;
const TObject *err;
Expand All @@ -551,31 +542,11 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
}


static int aux_do (lua_State *L, int status) {
if (status == 0) { /* parse OK? */
int err = lua_gettop(L);
lua_getglobal(L, "_ERRORMESSAGE");
lua_insert(L, err);
status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
lua_remove(L, err); /* remove error function */
}
return status;
}


LUA_API int lua_dofile (lua_State *L, const char *filename) {
return aux_do(L, lua_loadfile(L, filename));
}


LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name) {
return aux_do(L, lua_loadbuffer(L, buff, size, name));
}


LUA_API int lua_dostring (lua_State *L, const char *str) {
return lua_dobuffer(L, str, strlen(str), str);
static int errfile (lua_State *L, const char *filename) {
char buff[150];
sprintf(buff, "cannot read file `%.80s' (%.40s)", filename, strerror(errno));
lua_pushstring(L, buff);
return LUA_ERRFILE;
}


Expand All @@ -585,12 +556,12 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) {
int bin; /* flag for file mode */
int nlevel; /* level on the stack of filename */
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
if (f == NULL) return errfile(L, filename); /* unable to open file */
bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
if (bin && f != stdin) {
fclose(f);
f = fopen(filename, "rb"); /* reopen in binary mode */
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
if (f == NULL) return errfile(L, filename); /* unable to reopen file */
}
if (filename == NULL)
lua_pushstring(L, "=stdin");
Expand All @@ -603,7 +574,8 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) {
filename = lua_tostring(L, -1); /* filename = `@'..filename */
luaZ_Fopen(&z, f, filename);
status = luaD_protectedparser(L, &z, bin);
if (ferror(f)) status = LUA_ERRFILE;
if (ferror(f))
return errfile(L, filename);
lua_remove(L, nlevel); /* remove filename */
if (f != stdin)
fclose(f);
Expand Down Expand Up @@ -661,9 +633,10 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
*/


LUA_API void lua_error (lua_State *L, const char *s) {
LUA_API void lua_errorobj (lua_State *L) {
lua_lock(L);
luaD_runerror(L, s);
api_checknelems(L, 1);
luaD_errorobj(L, L->top - 1, LUA_ERRRUN);
lua_unlock(L);
}

Expand Down Expand Up @@ -767,3 +740,62 @@ LUA_API int lua_pushupvalues (lua_State *L) {
}



/*
** {======================================================
** compatibility code
** =======================================================
*/


static void callalert (lua_State *L, int status) {
if (status != 0) {
int top = lua_gettop(L);
lua_getglobal(L, "_ALERT");
lua_insert(L, -2);
lua_pcall(L, 1, 0, 0);
lua_settop(L, top-1);
}
}


LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
int status;
int errpos = lua_gettop(L) - nargs;
lua_getglobal(L, "_ERRORMESSAGE");
lua_insert(L, errpos); /* put below function and args */
status = lua_pcall(L, nargs, nresults, errpos);
lua_remove(L, errpos);
callalert(L, status);
return status;
}

static int aux_do (lua_State *L, int status) {
if (status == 0) { /* parse OK? */
int err = lua_gettop(L);
lua_getglobal(L, "_ERRORMESSAGE");
lua_insert(L, err);
status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
lua_remove(L, err); /* remove error function */
}
callalert(L, status);
return status;
}


LUA_API int lua_dofile (lua_State *L, const char *filename) {
return aux_do(L, lua_loadfile(L, filename));
}


LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name) {
return aux_do(L, lua_loadbuffer(L, buff, size, name));
}


LUA_API int lua_dostring (lua_State *L, const char *str) {
return lua_dobuffer(L, str, strlen(str), str);
}

/* }====================================================== */
16 changes: 2 additions & 14 deletions lauxlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.65 2002/04/04 20:25:55 roberto Exp roberto $
** $Id: lauxlib.c,v 1.66 2002/04/16 12:00:02 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
Expand All @@ -21,19 +21,6 @@
#include "lualib.h"


LUALIB_API const char *luaL_errstr (int errcode) {
static const char *const errstr[] = {
"ok",
"run-time error",
"cannot read file",
"syntax error",
"not enough memory",
"error in error handling"
};
return errstr[errcode];
}


LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
int i;
for (i=0; list[i]; i++)
Expand All @@ -42,6 +29,7 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
return -1; /* name not found */
}


LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
lua_Debug ar;
lua_getstack(L, 0, &ar);
Expand Down
4 changes: 1 addition & 3 deletions lauxlib.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lauxlib.h,v 1.43 2002/03/20 12:54:08 roberto Exp roberto $
** $Id: lauxlib.h,v 1.44 2002/04/02 20:42:49 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -52,8 +52,6 @@ LUALIB_API int luaL_findstring (const char *name,
LUALIB_API int luaL_ref (lua_State *L, int t);
LUALIB_API void luaL_unref (lua_State *L, int t, int ref);

/* error messages corresponding to error codes */
LUALIB_API const char *luaL_errstr (int errcode);


/*
Expand Down
Loading

0 comments on commit 751cd86

Please sign in to comment.