lua-users home
lua-l archive

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


I would like to access the following C++ types from lua:

struct A { int x, y; };
struct B { A a; }

and then in lua do:

b = B:new()
var = b.a.x

So the best way I can explain this is that I want to have "nested" userdata. Here's what I have so far:

static int B_new (lua_State *L)
{
  B *b = (B*)lua_newuserdata(L, sizeof(B));
  luaL_getmetatable(L, "B");
  lua_setmetatable(L, -2);
  return 1;
}

static int A_index (lua_State *L)
{
    // push 'x' or 'y' depending on key
}

static int B_index (lua_State *L)
{
    // what do I push if the key is 'a'? I don't want to allocate new userdata, but I also don't want light user data because I need the metatable.

    if (lua_tostring(L, 2) == "a")
       // .. ? ..
}