From 2f2b4a42a95c7a96e5e16b74ba1167690fcd6231 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Aug 2005 16:05:04 -0300 Subject: [PATCH] luaL_checkudata raises an error if value is not correct (like other luaL_check functions) --- lauxlib.c | 25 +++++++++++++------------ liolib.c | 20 +++++++++----------- loadlib.c | 8 +++----- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 93cbff51..a828e612 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.143 2005/08/10 18:47:09 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.144 2005/08/15 14:12:32 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -130,18 +130,19 @@ LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - const char *tn; - if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */ - lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */ - tn = lua_tostring(L, -1); - if (tn && (strcmp(tn, tname) == 0)) { - lua_pop(L, 1); - return lua_touserdata(L, ud); - } - else { - lua_pop(L, 1); - return NULL; + void *p = NULL; + if (lua_getmetatable(L, ud)) { + const char *tn; + lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */ + tn = lua_tostring(L, -1); + if (tn && (strcmp(tn, tname) == 0)) { + lua_pop(L, 1); + p = lua_touserdata(L, ud); + } } + if (p == NULL) + luaL_typerror(L, ud, tname); + return p; } 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 */ diff --git a/loadlib.c b/loadlib.c index d50a587a..81d46172 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.37 2005/08/10 18:06:58 roberto Exp roberto $ +** $Id: loadlib.c,v 1.38 2005/08/15 14:12:32 roberto Exp roberto $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -289,10 +289,8 @@ static void **ll_register (lua_State *L, const char *path) { */ static int gctm (lua_State *L) { void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); - if (lib) { - if (*lib) ll_unloadlib(*lib); - *lib = NULL; /* mark library as closed */ - } + if (*lib) ll_unloadlib(*lib); + *lib = NULL; /* mark library as closed */ return 0; }