[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5 and tags
- From: Mark Hamburg <mhamburg@...>
- Date: Sun, 25 Jul 2004 21:48:24 -0700
on 7/25/04 2:16 PM, Vincent Penne at ziggy@sashipa.com wrote:
> Here is the code that do what I suggest :
>
> void *luaL_checkudata_ref(lua_State *L, int ud, int ref, char *msg)
> {
> void *u;
> if (lua_getmetatable(L, ud)) && (lua_getref(L, ref), 1) &&
> lua_rawequal(L, -1, -2) &&
> (u = lua_touserdata(L, ud))) {
> lua_pop(L, 2);
> return u;
> }
> luaL_argerror(L, 0, msg); /* Never returns. */
> return NULL;
> }
The only problem with this code is that if you create multiple Lua universes
-- i.e., independent Lua states as opposed to multiple Lua threads -- then
you can't rely on sharing a C global for the ref to the metatable.
One other approach would be to assume that you could use location 1 in the
metatable and do a lua_rawgeti to fetch it and test that it held an
appropriate userdata value.
Untested...
void *luaL_checkudata_ud1 (lua_State *L, int ud, void *tident, char *msg)
{
void *u = lua_touserdata(L, ud);
if( u && lua_getmetatable(L, ud) ) {
lua_rawgeti(L, -1, 1);
if( lua_touserdata(L, -1) == tident ) {
lua_pop(L, 2); /* Restore stack before fetching */
return u;
}
}
luaL_argerror(L, ud, msg);
return NULL;
}
Mark