lua-users home
lua-l archive

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


This is more or less the way I ended up doing object pushes from
Objective-C. The one enhancement I came up with was to use a light userdata
instead of a pointer to get to the table.

    lua_pushlightuserdata(L, kObjectsTableKey);
        // kObjectsTableKey just needs to be a unique pointer
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_pushlightuserdata(L, this);
    lua_gettable(-2);
    lua_remove(-2);

Still not exactly cheap.

Mark

on 5/16/05 11:53 AM, Chris Marrin at chris@marrin.com wrote:

> But such a thing does not exist. I can understand why. You don't want to
> go pushing some random pointer and then expect Lua to try to access its
> metatable and then crash! To solve this, I created a global table called
> __objects, with weak values. The key to this table is the this pointer,
> as a lightuserdata, and the value is the userdata. Making the values
> weak will allow these objects to get collected even if they are in this
> table. So now I can simply do this:
> 
>    lua_pushstring(L, "__objects");
>    lua_gettable(L, LUA_GLOBALSINDEX);
>    lua_pushlightuserdata(L, this);
>    lua_gettable(-2);
>    lua_remove(-2);
> 
> and voila' I have the userdata on the stack. The problem is this is very
> slow, requiring two table accesses and stack fiddling every time.
> 
> Am I missing something in the public API, or is there really no more
> efficient way to move between C++ and Lua?