lua-users home
lua-l archive

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

OK, are both "key" and "v.key" references to the same Lua userdata? That is, do they both come from the same invocation of lua_newuserdata?

I'm guessing the answer is no. What you need is a table, probably only referenced in the Lua registry, which maps C++ pointers (as Lua light userdata) to the corresponding full userdata. This table should have weak values; that is, its metatable's "__mode" field should be set to "v". Then, whenever your C++ code needs to send an object to Lua, it should first look up the object's pointer (again, as light userdata) in this cache table. If a corresponding full userdata object is found, the C++ code should send that to Lua. Otherwise, it should create a new full userdata object and map that to the C++ pointer in the cache table before sending it to Lua. Then there will be only one Lua userdata object for each C++ object.