[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 11:15:38 -0700
Warning untested (and uncompiled) code:
LUALIB_API int luaL_newmetatable_ud (lua_State *L, void *tident) {
lua_pushlightuserdata(L, tident);
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[ tident ] */
if (!lua_isnil(L, -1)) /* tident already in use? */
return 0; /* leave previous value on top, but return 0 */
lua_pop(L, 1);
lua_newtable(L); /* create metatable */
lua_pushlightuserdata(L, tident);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX); /* registry[ tident ] = metatable */
lua_pushvalue(L, -1);
lua_pushlightuserdata(L, tident);
lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = tident */
return 1;
}
LUALIB_API void luaL_getmetatable_ud (lua_State *L, void *tident) {
lua_pushlightuserdata(L, tident);
lua_rawget(L, LUA_REGISTRYINDEX);
}
LUALIB_API void *luaL_checkudata_ud (lua_State *L, int ud, void *tident) {
if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
if (lua_touserdata(L, -1) == tident) {
lua_pop(L, 1);
return lua_touserdata(L, ud);
}
else {
lua_pop(L, 1);
return NULL;
}
}
(Interesting. luaL_checkudata doesn't throw on errors. It should probably be
named luaL_toudata. If the check routine did throw, one might then want a
name either at creation or check time but just for constructing the error
message.)
Mark