lua-users home
lua-l archive

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


It was thus said that the Great Jim zhang once stated:
> If I care more about the object passing in, and change the code as following:
>  
> static int sumlua(lua_State *L)
> {
> struct A;
> int    x;
> 
> lua_getfield(L,1,"a");
> lua_getfield(L,1,"b");
> 
> A.a = luaL_checkinteger(L,-2);
> A.b = luaL_checkinteger(L,-1);
> A.a = 2*A.a;
> A.b = 2*A.b;
> ---- [[if I want to passing the updated object A back to lua
>  something like  lua_pushinteger(L,A);  what should I do? ]]
>  
> --- to push A back to lua
>   return 1;
> }
>  
> What would I do?

  You could add the following just before the return:

	lua_createtable(L,0,2);
	lua_pushinteger(L,A.a);
	lua_setfield(L,-2,"a");
	lua_pushinteger(L,A.b);
	lua_setfield(L,-2,"b");

  This creates a table, and populates the two fields.  So sumlua() now
returns a new table.  If you don't want to return the new table, then do:

	lua_pushinteger(L,A.a);
	lua_setfield(L,1,"a");
	lua_pushinteger(L,A.b);
	lua_setfield(L,1,"b");

  This updates the passed in table with the new results (and returns the
table passed in).  Which you use depends on your program's needs.

  -spc