lua-users home
lua-l archive

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


Thanks! That did the trick. I'll have to remember to use lua2.c when I need to see how thing's are setup in C when given a senario in script, very handy. From that I just created a few simple helper methods to build a return table.

StartMainTable(), StartSubTable(), PushVal(Index),PushVal(Value),EndSubTable(),EndMainTable().

That works nicely for now and helps me understand how the stack is built more too!

----- Original Message ----- From: "Luiz Henrique de Figueiredo" <lhf@tecgraf.puc-rio.br>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Sunday, October 11, 2009 7:35 AM
Subject: Re: Creating nested tables from C into Lua..


{ {name="Scooby",age=45,
type="Dog",someusertype=<userdata>},{name="Shaggy",age=24,
type="Human",someusertype=<userdata>}}

My lua2c, which is for Lua 4, still works on this piece of code and generates
correct C code for Lua 5.1:

/* This C code was generated by lua2c from the Lua code below.

return { {name="Scooby",age=45,
type="Dog",someusertype="userdata"},{name="Shaggy",age=24,
type="Human",someusertype="userdata"}}

*/
static int MAIN(lua_State *L)
{
lua_newtable(L);
lua_newtable(L);
lua_pushliteral(L,"name");
lua_pushliteral(L,"Scooby");
lua_pushliteral(L,"age");
lua_pushnumber(L,45);
lua_pushliteral(L,"type");
lua_pushliteral(L,"Dog");
lua_pushliteral(L,"someusertype");
lua_pushliteral(L,"userdata");
lua_settable(L,-9);
lua_settable(L,-7);
lua_settable(L,-5);
lua_settable(L,-3);
lua_newtable(L);
lua_pushliteral(L,"name");
lua_pushliteral(L,"Shaggy");
lua_pushliteral(L,"age");
lua_pushnumber(L,24);
lua_pushliteral(L,"type");
lua_pushliteral(L,"Human");
lua_pushliteral(L,"someusertype");
lua_pushliteral(L,"userdata");
lua_settable(L,-9);
lua_settable(L,-7);
lua_settable(L,-5);
lua_settable(L,-3);
lua_rawseti(L,-3,2);
lua_rawseti(L,-2,1);
return 1;
return 0;
}