better error messages

This commit is contained in:
Roberto Ierusalimschy
1999-12-28 09:52:49 -02:00
parent acdb0b741e
commit fb60283974
2 changed files with 48 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.22 1999/12/20 13:09:45 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.23 1999/12/27 17:33:22 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
*/ */
@@ -30,69 +30,79 @@ int luaL_findstring (const char *name, const char *const list[]) {
return -1; /* name not found */ return -1; /* name not found */
} }
void luaL_argerror (lua_State *L, int numarg, const char *extramsg) { void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
lua_Function f = lua_stackedfunction(L, 0); lua_Function f = lua_stackedfunction(L, 0);
const char *funcname; const char *funcname;
lua_getobjname(L, f, &funcname); lua_getobjname(L, f, &funcname);
numarg -= lua_nups(L, f); narg -= lua_nups(L, f);
if (funcname == NULL) if (funcname == NULL)
funcname = "?"; funcname = "?";
luaL_verror(L, "bad argument #%d to function `%.50s' (%.100s)", luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
numarg, funcname, extramsg); narg, funcname, extramsg);
} }
static const char *checkstr (lua_State *L, lua_Object o, int n, long *len) {
static void type_error (lua_State *L, int narg, const char *typename,
lua_Object o) {
char buff[100];
const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o);
sprintf(buff, "%.10s expected, got %.10s", typename, otype);
luaL_argerror(L, narg, buff);
}
static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) {
const char *s = lua_getstring(L, o); const char *s = lua_getstring(L, o);
luaL_arg_check(L, s, n, "string expected"); if (!s) type_error(L, narg, "string", o);
if (len) *len = lua_strlen(L, o); if (len) *len = lua_strlen(L, o);
return s; return s;
} }
const char *luaL_check_lstr (lua_State *L, int n, long *len) { const char *luaL_check_lstr (lua_State *L, int narg, long *len) {
return checkstr(L, lua_getparam(L, n), n, len); return checkstr(L, lua_getparam(L, narg), narg, len);
} }
const char *luaL_opt_lstr (lua_State *L, int n, const char *def, long *len) { const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) {
lua_Object o = lua_getparam(L, n); lua_Object o = lua_getparam(L, narg);
if (o == LUA_NOOBJECT) { if (o == LUA_NOOBJECT) {
if (len) *len = def ? strlen(def) : 0; if (len) *len = def ? strlen(def) : 0;
return def; return def;
} }
else return checkstr(L, o, n, len); else return checkstr(L, o, narg, len);
} }
double luaL_check_number (lua_State *L, int n) { double luaL_check_number (lua_State *L, int narg) {
lua_Object o = lua_getparam(L, n); lua_Object o = lua_getparam(L, narg);
luaL_arg_check(L, lua_isnumber(L, o), n, "number expected"); if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
return lua_getnumber(L, o); return lua_getnumber(L, o);
} }
double luaL_opt_number (lua_State *L, int n, double def) { double luaL_opt_number (lua_State *L, int narg, double def) {
lua_Object o = lua_getparam(L, n); lua_Object o = lua_getparam(L, narg);
if (o == LUA_NOOBJECT) return def; if (o == LUA_NOOBJECT) return def;
else { else {
luaL_arg_check(L, lua_isnumber(L, o), n, "number expected"); if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
return lua_getnumber(L, o); return lua_getnumber(L, o);
} }
} }
lua_Object luaL_tablearg (lua_State *L, int arg) { lua_Object luaL_tablearg (lua_State *L, int narg) {
lua_Object o = lua_getparam(L, arg); lua_Object o = lua_getparam(L, narg);
luaL_arg_check(L, lua_istable(L, o), arg, "table expected"); if (!lua_istable(L, o)) type_error(L, narg, "table", o);
return o; return o;
} }
lua_Object luaL_functionarg (lua_State *L, int arg) { lua_Object luaL_functionarg (lua_State *L, int narg) {
lua_Object o = lua_getparam(L, arg); lua_Object o = lua_getparam(L, narg);
luaL_arg_check(L, lua_isfunction(L, o), arg, "function expected"); if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
return o; return o;
} }
lua_Object luaL_nonnullarg (lua_State *L, int n) { lua_Object luaL_nonnullarg (lua_State *L, int narg) {
lua_Object o = lua_getparam(L, n); lua_Object o = lua_getparam(L, narg);
luaL_arg_check(L, o != LUA_NOOBJECT, n, "value expected"); luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
return o; return o;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.52 1999/11/22 17:39:51 roberto Exp roberto $ ** $Id: liolib.c,v 1.53 1999/12/27 13:04:53 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
*/ */
@@ -395,20 +395,17 @@ static void io_write (lua_State *L) {
FILE *f = getfileparam(L, FOUTPUT, &arg); FILE *f = getfileparam(L, FOUTPUT, &arg);
int status = 1; int status = 1;
lua_Object o; lua_Object o;
while ((o = lua_getparam(L, arg++)) != LUA_NOOBJECT) { while ((o = lua_getparam(L, arg)) != LUA_NOOBJECT) {
switch (lua_type(L, o)[2]) { if (lua_type(L, o)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
case 'r': { /* stRing? */ /* optimization: could be done exactly as for strings */
long l = lua_strlen(L, o); status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
status = status &&
((long)fwrite(lua_getstring(L, o), sizeof(char), l, f) == l);
break;
}
case 'm': /* nuMber? */ /* LUA_NUMBER */
/* optimization: could be done exactly as for strings */
status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
break;
default: luaL_argerror(L, arg-1, "string expected");
} }
else {
long l;
const char *s = luaL_check_lstr(L, arg, &l);
status = status && ((long)fwrite(s, sizeof(char), l, f) == l);
}
arg++;
} }
pushresult(L, status); pushresult(L, status);
} }