lua-users home
lua-l archive

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


I’m not clear what your actual problem is, as you seem to be suggesting a solution without really saying what the underlying problem you are trying to solve is. What I *think* you are saying is that you wish to expose two distinct userdata values that have the same lifetime from a GC standpoint, but I’m not clear why you need to do this.

I think this is a common case. 

For example, we have a tree based document in C, (maybe an xml document,  a scene object in 3d engine, or a window in GUI system). and then we want to access a part of it.

We want to write like this in lua :

local t = obj.x.y.z  -- obj is an userdata

We can't create separated userdata stores the separated part of obj , because only one object in C. So create a proxy userdata for obj.x , obj.x.y  and obj.x.y.z is a solution, each proxy referenced a part of obj. 

It's not enough, we also need create a weak table in obj to cache "x" "x.y" and "x.y.z" proxy userdata, because we can't returns different proxy userdata for obj.x when we call obj.x more than once.  (obj.x should be unique, when we need use it as a table key later)

If this tree based document is large and deep, we need create too many small full userdata to access each part of it. It's very expensive now , and not necessary to clone the whole tree from C to lua with lua table.

We had try to use lightuserdata to simulate the idea (userdata slice) above, but gc doesn't mark light userdata,  we should be very careful to manage the lifetime. and lightuserdata has no user type, it's very dangerous to work with other library.