lua-users home
lua-l archive

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

Hello list,

I'm writing an application that deals with shapes, so there are objects
such as rectangles, polygons, paths etc. These shapes also manage
points which make up the shape. In the beginning I implemented
everything as lua tables, so something like

local rectangle = {
    local points = {
        bl = { x = -10, y = -10 },
        tr = { x =  10, y =  10 }

with an appropriate set of metatables for object methods.

At some point I recognized some performance issues, so I started to
implement some of this stuff in C (for instance, points are now
userdata). Furthermore, I have C code that does not need to interact
with lua, so it is a little bit silly to use the lua memory managment
unit for that. 

I started to change the entire object system to write everything in C,
but I failed at figuring out how to do this structure. My main problem
is that, occasingly, I have to pass objects to lua. What is the best
way to represent a structure such as my rectangle example in C? The
definition in C is something like this:

struct point
    int x, y;

struct rectangle
    point bl;
    point tr;

What is the best approach here regarding memory and lifetime managment?
In short: How to do nested userdata?
My current Idea would be to use a proxy (userdata) which encapsulates
the C data and is resolved on the C side.

Thank you, kind regards,