luaa: add luaA_toudata()

Convert a object to a udata if possible.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-10-19 18:50:55 +02:00
parent e6d644c4fb
commit 148bc7760b
1 changed files with 24 additions and 0 deletions

24
luaa.h
View File

@ -108,6 +108,30 @@ luaA_checkudata(lua_State *L, int ud, const char *tname)
return NULL; 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_toudata(lua_State *L, int ud, const char *tname)
{
void **p = lua_touserdata(L, ud);
if(p && *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? */
{
lua_pop(L, 2); /* remove both metatables */
return p;
}
lua_pop(L, 2); /* remove both metatables */
}
return NULL;
}
static inline bool static inline bool
luaA_checkboolean(lua_State *L, int n) luaA_checkboolean(lua_State *L, int n)
{ {