lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

John Dunn wrote:
> Jerome Vuarand wrote:
>> Just check if there's one in the add_handler, and if not create one.
> What's the best way to check to see if a table has been attached? It
> doesn't look like checking for nil works - it looks like calling
> getfenv returns a shared table if you haven't manually assigned one.  

You can put a sentinel in your own environments. For example (not tested):

-- Lua
if getfenv(object).owner ~= object then
    setfenv(object, {owner=object})

// C, assuming your object is at index 'index'
lua_getfenv(L, index);
lua_getfield(L, -1, "owner");
lua_replace(L, -2);
if (!lua_equal(L, index, -1))
    lua_pop(L, 1);
    lua_createtable(L, 0, 2); // 2 for owner+handler
    lua_pushvalue(L, index);
    lua_setfield(L, -2, "owner");
    lua_setfenv(L, index);
    lua_pop(L, 1);