[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Setting metatable to nil?
- From: "King, Mike" <MKing@...>
- Date: Mon, 20 Mar 2006 13:37:18 -0500
Is it okay to set the metatable of a userdata to nil? In my C++
program, a Lua function handles a C++ event. I'm pasting to it a
userdata that has a metatable associated with it. I want to prevent the
functions of the userdata's metatable from being called after the event
has occurred. I'm thinking I can do this by setting the userdata's
metatable to nil after the event has been invoked.
Nriver **pd = (Driver **)lua_newuserdata(_L, sizeof(Driver *));
*pd = driver;
lua_rawgeti(_L, LUA_REGISTRYINDEX, _HandleEvent);
lua_pushvalue(_L, -2);
luaL_getmetatable(_L, "Driver*");
lua_setmetatable(_L, -2);
int t = lua_gettop(_L);
lua_pcall (_L, 1, 0, 0);
lua_settop(_L, t);
lua_pushnil(_L);
lua_setmetatable(_L, -2);
function HandleEvent(driver)
-- the following is dangerous
mydriver = driver
end