lua-users home
lua-l archive

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


Am 02.07.2013 00:42 schröbte Coda Highland:
On Mon, Jul 1, 2013 at 3:34 PM, Xavier Wang <weasley.wx@gmail.com> wrote:

local a = array.new() -- alloc a 0 size of array
local a10 = array.new(10) -- alloc a 10 values of array
a[100] = nil
print(#a) --> 100

it's not a type of lua, it just something only seen in C program, just a
structure like this:

struct lua_Values {
     size_t n;
     TValue values[1];
};

to make a array object, you need a userdata value, and hold the pointer to
lua_Values.

some things we need to solve, how to collect garbage in lua_Values? is there
other thing we need thought?


There's a reason Lua doesn't expose this right now: refcounting.

Python offers this functionality and you have to be extremely cautious
in incrementing and decrementing the refcount on the object. It's a
pain in the butt. One wrong increment/decrement and you're crashing or
leaking.

The usual idiom here is to use luaL_ref() to put a reference to the
object into the registry, and then you have a nice little handle that
manages a refcount.

... or allocate a size_t userdata and store all TValues in a uservalue table (5.1: environment table) --> no need for reference counting, and a table already can hold all possible Lua values ...


/s/ Adam


Philipp