From 2f2b4a42a95c7a96e5e16b74ba1167690fcd6231 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Aug 2005 16:05:04 -0300 Subject: luaL_checkudata raises an error if value is not correct (like other luaL_check functions) --- liolib.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'liolib.c') diff --git a/liolib.c b/liolib.c index a029190d..ceee5371 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.64 2005/07/12 14:32:08 roberto Exp roberto $ +** $Id: liolib.c,v 2.65 2005/08/15 14:12:32 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -50,17 +50,17 @@ static void fileerror (lua_State *L, int arg, const char *filename) { } -static FILE **topfile (lua_State *L) { - FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); - if (f == NULL) luaL_argerror(L, 1, "bad file"); - return f; -} +#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) static int io_type (lua_State *L) { - FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); - if (f == NULL) lua_pushnil(L); - else if (*f == NULL) + void *ud; + luaL_checkany(L, 1); + ud = lua_touserdata(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); + if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) + lua_pushnil(L); /* not a file */ + else if (*((FILE **)ud) == NULL) lua_pushliteral(L, "closed file"); else lua_pushliteral(L, "file"); @@ -173,7 +173,6 @@ static int io_tmpfile (lua_State *L) { static FILE *getiofile (lua_State *L, int findex) { FILE *f; lua_rawgeti(L, LUA_ENVIRONINDEX, findex); - lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); f = *(FILE **)lua_touserdata(L, -1); if (f == NULL) luaL_error(L, "standard %s file is closed", fnames[findex - 1]); @@ -194,7 +193,6 @@ static int g_iofile (lua_State *L, int f, const char *mode) { tofile(L); /* check that it's a valid file handle */ lua_pushvalue(L, 1); } - lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); lua_rawseti(L, LUA_ENVIRONINDEX, f); } /* return current value */ -- cgit v1.2.3-55-g6feb