lua-users home
lua-l archive

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


On Tue, Jun 17, 2003 at 08:48:59PM +0400, Grisha wrote:
> Connecting the C++ object to the
> table isn't difficult - there's just a field in teh table "__handle" that
> stores light user data pointer to the object, 

<OT>
This common method of passing objects directly as light userdatas to
Lua makes me see red. That's one of the reasons I had to write my own
bindings instead of using toLua or SWIG as I didn't want to write extra
wrappers. (Another is generated code size.) What I do is create with 
lua_newuserdata a unique "watch" (watches are used throughout my code;
they are essentially pointers with handlers called before an object is
finalised) that is used to refer to the object indirectly and that gets
reset when the C-side object is destroyed. Of course this adds some
overhead, but it isn't that important in my app and users can't crash
it by carelessness.
</OT>

> but I can't figure a good way
> to connect table to the object, since you can't get table handle from lua.

handle=luaL_ref(st, LUA_REGISTRYINDEX)

and to access the table

lua_rawgeti(st, LUA_REGISTRYINDEX, handle)

-- 
Tuomo