lua-users home
lua-l archive

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


> I know that deep copy function. I need a function in C. I'm going to be
>  passing lua tables between pthreads, these are separate lua states.
> So i have to take a table of one stack copy and push it onto another stack.

Here's is totally untested code, just to give you an idea.

static int xcopy1(lua_State *L, lua_State *T, int n)
{
 switch (lua_type(L,n))
 {
  case LUA_TNIL:
    lua_pushnil(T);
    break;
  case LUA_TBOOLEAN:
    lua_pushboolean(T,lua_toboolean(L,n));
    break;
  case LUA_TNUMBER:
    lua_pushnumber(T,lua_tonumber(L,n));
    break;
  case LUA_TSTRING:
    lua_pushlstring(T,lua_tostring(L,n),lua_strlen(L,n));
    break;
  case LUA_TLIGHTUSERDATA:
    lua_pushlightuserdata(T,(void*)lua_touserdata(L,n));
    break;
  default:
    assert(0);
    break;
 }
}

/* table is in the stack at index 't' */
static int xcopy(lua_State *L, lua_State *T, int t)
{
 int w;
 lua_newtable(T);
 w=lua_gettop(T);
 lua_pushnil(L);  /* first key */
 while (lua_next(L, t) != 0) {
	xcopy1(L,T,-2);
	if (lua_type(L,-1)==LUA_TTABLE)
		xcopy(L,T,lua_gettop(L));
	else
		xcopy1(L,T,-1);
	lua_settable(T,w);
	lua_pop(L,1);
 }
}