|
As I said in my earlier post, the original motivation for userdata was to allow Lua to provide opaque storage for C libraries. The operative word here is “opaque” .. Lua can store the data and manage its lifetime (via the GC), but not access it. This provided a string guarantee that the C library could rely on the integrity of the data in the userdata when called, since Lua code could not corrupt it. What you are asking for is transparent userdata; that is a block of userdata that can be processed by Lua code. There are several different approaches to this: 1. “Lift” the entire data into Lua, marshalling it into a Lua-like form (say, a table), and then re-pack it back into binary form when you have finished processing it. This requires some kind of unpacking/packing API, written by you (and probably in C). One advantage is that you can process the data using Lua constructs. The disadvantage is the pack/unpack overhead, plus extra memory involved. 2. Develop a custom C library that provides whatever low-level manipulations of the data you want (enumerate contents, edit values etc), and write Lua code that uses this API. This is probably more efficient that (1) though it needs some overhead when making the calls to the C library. The slice technique you discuss appears to confuse ownership and lifetime management of the dataset being manipulated. If you have only one object in C, then by definition the lifetime of the object is controlled either by C (in which case you should use a light userdata) or by Lua (a full userdata). If you need to create subsidiary objects that share the same lifetime as the “master” object, then you need proxy userdata that can coordinate with the full userdata to track the lifetime of the object. —Tim |