From 68548a02d39861176b59f58be93315b51eafdbe2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Aug 2005 17:09:31 -0300 Subject: [PATCH] fancier code ;) --- lauxlib.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index a828e612..698fba24 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.144 2005/08/15 14:12:32 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.145 2005/08/17 19:05:04 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -130,18 +130,15 @@ LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - 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); + void *p = lua_touserdata(L, ud); + const char *tn; + if (p == NULL || /* if is not a userdata? */ + !lua_getmetatable(L, ud) || /* has no metatable? */ + (lua_rawget(L, LUA_REGISTRYINDEX), /* get registry[metatable] */ + (tn = lua_tostring(L, -1)) == NULL) || /* metatable not registered? */ + (strcmp(tn, tname) != 0)) /* or wrong? */ + luaL_typerror(L, ud, tname); /* then error */ + lua_pop(L, 1); /* remove registry[metatable] */ return p; }