lua-users home
lua-l archive

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


On 10/11/2021 09.56, Patrick Kurth wrote:
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?

If the types are strict, i.e. a certain field will _always_ be userdata,
and you're using full userdata / Lua is managing your memory, then you
can remove these fields from the `struct` and store them as "user
values" - https://www.lua.org/manual/5.4/manual.html#lua_newuserdatauv

These "user value" slots can store regular Lua values like tables... or
other user data, and Lua will keep track of them for GC purposes.

If you don't want Lua managing your memory, then Viacheslav described
some other alternatives.

-- nobody