|
Kriss wrote:
Mark Hamburg wrote:The safe way to do this is to maintain a weak table mapping the userdata pointers as light userdata to the full userdata objects. Pushing then becomes something like the following: lua_pushlightuserdata( L, &myUDMapKey ); lua_gettable( L, LUA_REGISTRYINDEX ); lua_pushlightuserdata( L, theUserData ); lua_gettable( L, -2 ); lua_remove( L, -2 );(I used a light userdata rather than an integer to key the registry becauseit's easier to guarantee unique.)Thanx, that gave me a better idea for how to handle things in my case.I can key my table with light userdata of the same address as the real user data. Easy to lookup from the c side, guaranteed unique keys. A simple function to return a light userdata with the same address of any given userdata will keep it accessible from the lua side when all I have is a userdata to hand.Much simpler.
I did it this way originally and found that the overhead was too high for my app. I am using Lua to interact with a C++ object system, and the Lua userdata is my Lua aspect of a C++ object. That means it gets pushed a lot, so I wanted it to be as efficient as possible. I am using essentially the code above. This is safe for me because the userdata has a metatable with a __gc metamethod. So I know when it is getting collected and can clean up any outstanding references to it.
-- chris marrin chris@marrin.com