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:
parent
e6d644c4fb
commit
148bc7760b
24
luaa.h
24
luaa.h
|
@ -108,6 +108,30 @@ luaA_checkudata(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_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
|
||||
luaA_checkboolean(lua_State *L, int n)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue