better error messages

This commit is contained in:
Roberto Ierusalimschy 2002-06-05 13:59:37 -03:00
parent 6dd0b6c62b
commit a102221a0b
2 changed files with 20 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.71 2002/05/16 18:39:46 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.72 2002/06/03 20:11:41 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -37,7 +37,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
lua_Debug ar; lua_Debug ar;
lua_getstack(L, 0, &ar); lua_getstack(L, 0, &ar);
lua_getinfo(L, "n", &ar); lua_getinfo(L, "n", &ar);
if (strcmp(ar.namewhat, "method") == 0) narg--; /* do not count `self' */ if (strcmp(ar.namewhat, "method") == 0) {
narg--; /* do not count `self' */
if (narg == 0) /* error is in the self argument itself? */
return luaL_verror(L,
"calling %s on bad self (perhaps using `:' instead of `.')",
ar.name);
}
if (ar.name == NULL) if (ar.name == NULL)
ar.name = "?"; ar.name = "?";
return luaL_verror(L, "bad argument #%d to `%s' (%s)", return luaL_verror(L, "bad argument #%d to `%s' (%s)",
@ -314,6 +320,7 @@ typedef struct LoadF {
static const char *getF (void *ud, size_t *size) { static const char *getF (void *ud, size_t *size) {
LoadF *lf = (LoadF *)ud; LoadF *lf = (LoadF *)ud;
if (feof(lf->f)) return NULL;
*size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f); *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
return (*size > 0) ? lf->buff : NULL; return (*size > 0) ? lf->buff : NULL;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 2.4 2002/05/02 17:12:27 roberto Exp roberto $ ** $Id: liolib.c,v 2.5 2002/05/06 19:05:10 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -60,8 +60,9 @@ static FILE *tofile (lua_State *L, int findex) {
lua_pop(L, 1); lua_pop(L, 1);
return *f; return *f;
} }
luaL_argerror(L, findex, "bad file"); if (findex > 0)
return NULL; /* to avoid warnings */ luaL_argerror(L, findex, "bad file");
return NULL;
} }
@ -133,14 +134,18 @@ static int io_tmpfile (lua_State *L) {
static FILE *getiofile (lua_State *L, const char *name) { static FILE *getiofile (lua_State *L, const char *name) {
FILE *f;
lua_pushstring(L, name); lua_pushstring(L, name);
lua_rawget(L, lua_upvalueindex(1)); lua_rawget(L, lua_upvalueindex(1));
return tofile(L, -1); f = tofile(L, -1);
if (f == NULL)
luaL_verror(L, "%s is closed", name);
return f;
} }
static int g_iofile (lua_State *L, const char *name, const char *mode) { static int g_iofile (lua_State *L, const char *name, const char *mode) {
if (lua_isnone(L, 1)) { if (lua_isnoneornil(L, 1)) {
lua_pushstring(L, name); lua_pushstring(L, name);
lua_rawget(L, lua_upvalueindex(1)); lua_rawget(L, lua_upvalueindex(1));
return 1; return 1;
@ -154,8 +159,8 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) {
newfile(L, f); newfile(L, f);
} }
else { else {
tofile(L, 1); /* check that it's a valid file handle */
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
tofile(L, -1); /* check that it's a valid file handle */
} }
lua_rawset(L, lua_upvalueindex(1)); lua_rawset(L, lua_upvalueindex(1));
return 0; return 0;