lua-users home
lua-l archive

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


Hi,

Last try... I think we missunderstood each other. :-)

So you have a C++ object and an  associated Lua table. From Lua you want
to be able to reference associated table as a full fledged Lua table and
also the C++ object (and for that you need the pointer to the C++ object
somewhere as a userdatum). From C++ you also want to have full access to
everything.

You problem now that there are no weak refs is that if you place the C++
object  userdatum as  a field  of the  associated table  (say, the  self
field) AND store a strong ref to  the associated field in the C++ object
structure they will never be  collected (cycle). Furthermore, in the Lua
side, the object is represented by its associated table.

Is it not  so? If not, ignore the  rest of the message and  I will never
talk about this issue again. :-)

My suggestion  is that you use  the userdatum directly to  reference the
object from Lua.  You can create an associated table  and place a strong
ref  to it  as a  field in  the  C++ object.  You set  the gettable  and
settable methods for  the userdatum to access the  associated table. You
set the garbage collection method  to unref the associated table. (there
is no self field in the associated table, there is no cycle)

Your object will then work as a table for Lua scripts and as a userdatum
for C++ code.  Lua can see the  userdatum value directly and  pass it to
C++ code. C++ code has the ref and  thus the associated table too. It is
fast and clean, I believe.

But you do have to change your code.

Regards,
Diego.