lua-users home
lua-l archive

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


Why not just use Alien?

alien.luaforge.net/


On Tue, Aug 18, 2015, 11:19 PM 云风 Cloud Wu <cloudwu@gmail.com> wrote:
Lua is a embedded language, it uses powerful userdata to wrap the c object as the lua object. But sometimes it's not enough.

For example, if we have a C struct like this :

struct foo {
  struct foo1 foo1;
  struct foo2 *foo2;
};

We can use userdata to store struct foo in lua variables : 

struct foo * f = lua_newuserdata(L, sizeof(*f));

How to reference sub struct foo1/foo2 in lua ? maybe we can give the userdata a metatable, implementing an __index metamethod.

But foo.foo1 or foo.foo2 should be a new userdata. It needs more complex and expensive way :

Creating a proxy userdata to access a part of struct foo, put the reference of proxy userdata in uservalue of userdata foo to prevent collecting foo before foo1/foo2 proxy, etc.

So I suggest introduce a new mechanism of userdata to simplify them. I call it userdata slice (maybe it's not a good name).

If we can associate an integer to the userdata value, the userdata whould be more flexible. It is different from uservalue, because uservalue is in userdata object, not in value.

An userdata object has only one uservalue , but each userdata value can has an unique associated integer.

Two C API needed:

int lua_getuserslice(lua_State *L, int index);
void lua_setuserslice(lua_State *L, int index, int slice);

Any value with type LUA_TUSERDATA in lua is an two 32bit integer tuple ( or two 16bit integer for small lua), one is the userdata (32bit int) handle , another is the associated integer.

Using handle instead of really address of userdata object because we can put two 32bit integer in 64bit lua value.

I think it's easy to implement and keep the c api compatibility with the previous version. 

Storing C/C++ objects in GUI or 3d engines could be more simple : OBJECT.X and OBJECT.X.Y can be the same userdata with OBJECT without creating new userdata, only the slice is different.