how to pass C structure to Lua

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to pass C structure to Lua

Honglei Tu
Hello,
If I want to use Lua 4.0 and will pass C structure to Lua,
how should I do ?
I am doing the code like this:
struct {
	int red;
	int green;
	int blue;
} aTest;

lua_pushnumber(L, 1);
lua_pushnumber(L, aTest.red);

lua_pushnumber(L, 2);
lua_pushnumber(L, aTest.green);

lua_pushnumber(L, 3);
lua_pushnumber(L, aTest.blue);

lua_settable(L, 1);

I get an error in lua_settable(L, 1).
It seems that the index is wrong.
Can anyone tell me how to do?

Honglei

Reply | Threaded
Open this post in threaded view
|

Re: how to pass C structure to Lua

Luiz Henrique de Figueiredo
>If I want to use Lua 4.0 and will pass C structure to Lua,
>how should I do ?

I assume you want to create a table that mirrors the C struct.
Int this case, the code is:

lua_newtable(L);
lua_pushstring(L,"red"); lua_pushnumber(L,aTest.red); lua_settable(L,-3);
lua_pushstring(L,"green"); lua_pushnumber(L,aTest.green); lua_settable(L,-3);
lua_pushstring(L,"blue"); lua_pushnumber(L,aTest.blue); lua_settable(L,-3);

The table is left on the top of the stack.
--lhf