lua-users home
lua-l archive

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


On 10/11/2021 08: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 you're holding the data C-side, maybe it's might be beter to treat it
'functionally' and just present 'for_each', 'for_all_rects',
'for_all_matching' type functions to Lua that take a function as an
argument and run it on all matching items. Could be designed to avoid
creating any userdata objects, just pass object data as arguments and
take the return values a updated data.

It's hard to say without knowing the full use case. I think anything
that you might come up with would result in lots of (possibly temporary)
userdata objects being created/cached/destroyed, unless you stick with
holding everything on the Lua side.

For example, if everything happens in one go, it may make sense to load
the data into Lua, run the script, then read the result back into C.
Gives you the benefit of atomicity in your actions.

Scott