lua-users home
lua-l archive

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


Hi,

as Tim Him proposed, A "empty" value in lua table, makes Lua have a "array" can contain any values.

But I thought more. there are something affects me a lot:
  - we can not "store" lua value in C. we can get pointer of any Lua value, but we can not store it in C and get the lua value it point, currently we can do it with a table to store the map with pointer and value.
  - we can not get a cheap data structure with a/some lua values. sometimes we use table, it's 24 byte without any values. we just want a non-type container, just can hold all values we want, just like a special userdata.

there is a proposal can solve all three (or more) issue: add a type only seen in C or other thing, it's just used to hold Lua values. just like the current Lua stack (internals, Roberto can implement Lua stack with this value).

we can have several APIs to maintain this data-structure, just like this:
lua_Values lua_newvalues(lua_State *L, size_t sz);
void lua_reallocvalues(lua_State *L, lua_Values *v, size_t sz); /* sz == 0 to free it */
void lua_pushvalues(lua_State *L, lua_Values *v);
void lua_valuesadd(lua_State *L, int idx, lua_Values *v);
void lua_valuesset(lua_State *L, lua_Values *v, int idx);

lua_Values has a defined length. the big difference between lua_Values and other thing is, it's just a container of some Lua value, so it can be changed: it's just a piece of memory that holds lua values, so we can grow it or shrink it.

with lua_Values, we can get a fast way to hold lua value in C program, we can implement cheap data structure that can hold all type of Lua value, and we can implement a true array userdata in C, and makes it a standard library of Lua.

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?

hope for comments about this idea.

--
regards,
Xavier Wang.