From 70cbf6bedbda1151b35aae0fa0d4bd87e48792b8 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 9 Apr 2009 13:23:57 +0200 Subject: [PATCH] luaa: implement luaA_toudata() with simple pointer This is needed for new object system. Signed-off-by: Julien Danjou --- luaa.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/luaa.h b/luaa.h index cdc8e09e5..99b3129e8 100644 --- a/luaa.h +++ b/luaa.h @@ -184,6 +184,27 @@ luaA_toudata(lua_State *L, int ud, const char *tname) return NULL; } +/** Convert a object to a udata if possible. + * \param L The Lua VM state. + * \param ud The index. + * \param tname The type name. + * \return A pointer to the object, NULL otherwise. + */ +static inline void * +luaA_toudata2(lua_State *L, int ud, const char *tname) +{ + void *p = lua_touserdata(L, ud); + if(p) /* value is a userdata? */ + if(lua_getmetatable(L, ud)) /* does it have a metatable? */ + { + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ + if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */ + p = NULL; + lua_pop(L, 2); /* remove both metatables */ + } + return p; +} + static inline bool luaA_checkboolean(lua_State *L, int n) {