From 027e9e99ca99cb572398d18d1c61a1ea2e804b71 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Aug 2005 17:36:26 -0300 Subject: simpler implementation for userdata types --- lauxlib.c | 22 +++++----------------- lauxlib.h | 5 +++-- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 90bd3668..95c33218 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.146 2005/08/17 20:09:31 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -117,28 +117,16 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { lua_newtable(L); /* create metatable */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ - lua_pushvalue(L, -1); - lua_pushstring(L, tname); - lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */ return 1; } -LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { - lua_getfield(L, LUA_REGISTRYINDEX, tname); -} - - LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *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] */ + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ + if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) + luaL_typerror(L, ud, tname); + lua_pop(L, 2); /* remove both metatables */ return p; } diff --git a/lauxlib.h b/lauxlib.h index 5cc0afba..b31b818e 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.80 2005/07/13 19:02:42 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.81 2005/08/15 14:12:32 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -60,7 +60,6 @@ LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); LUALIB_API void (luaL_checkany) (lua_State *L, int narg); LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void (luaL_getmetatable) (lua_State *L, const char *tname); LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_where) (lua_State *L, int lvl); @@ -114,6 +113,8 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx, #define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)) +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + /* ** {====================================================== -- cgit v1.2.3-55-g6feb