[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: how to store a ref to a table without registry?
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Thu, 20 Mar 2008 10:05:57 -0400
joerg piringer wrote:
> hallo all,
> i am pretty new to lua so please forgive me if this question has been
> asked before:
> i'd like to access a function (or rather a method) inside a specific
> table from c++. how can i do that without storing the reference to
> the table in the registry? the problem is when i store the reference
> in the registry it won't get garbage collected, right?
> i have the following code:
> o = TestObject()
> o.testFunc = function() print "test" end
> TestObject() is a c++ function that returns a table with a userdata
> inside (the actual c++ object).
> now i'd like to call my testFunc from the c++ object that's inside
> the table.
> i did it with a reference (in the registry) to self (the table) until
> i tried to garbage collect the objects...
I'm assuming that you don't want the table to be collected as long as
the userdata exists. In that case you have to associate the userdata and
the table somehow. I can see three easy solutions:
- You can associate the userdata and the table in another global table
(in the globals or in the registry). This other table should be weak to
preserve original userdata lifetime. See lua_settable.
- You can set the table as the userdata environment (or as a field in
the userdata environment if it's not shared). See lua_setmetatable.
- You can set the table as the userdata metatable (or as a field in the
userdata metatable if it's not shared). See lua_setfenv.
These three solutions work only with full userdata. Since lightuserdata
are simple values, they don't have a lifetime, so you cannot associate a
table with them.