Sunday, 15 March 2015

c++ - Moving a lua table in C api -


I am trying to move the table into another table using the LuaC API. For example, I have a table with this structure:

  a [b] [c] [d] [e] = value   

I want to move the table to be below Table [B], which I can complete in Lua:

  a [b] [d] = a [b] [c] ] [D] a [b [c] [d] = zero   

My current approach is to load a [B] [C] [D] table pile, so the stack Looks like:

  index value -1 d table-2c table-3b table-4 one table   

then Load a [B] on the stack, so it looks like:

  index value -1 B table-2 one table-3d table-4c table-5b table-6 Then put the table on the pile, and the key of the insert D and table B is table B, so the stack is:  
  index Value-1d table-2d key-3b table-4 one table -5c table -6b table-7 one table   

Then I have the lua_settable (L, -3) B to use [d] = D.

This approach works for non-table keys, but for those who are in the table, fails for them, then something like this will fail:

  a [B] [c] [{}] [d] = value a [b] = a [b] [c] [[} [d]   

note, I know that In the above it will fail in Lea given above because the key will be a new Lea table, I want to make it clear.

I have tried to leave the parents of the table (without any such fate) [B] = B, Lava_System Global (L, A)) Does anyone know where I am wrong Am I

EDIT: How to push keys / values ​​on the stack I have a small code snippet on it here to move a target table from one table structure to another (or as it is in the code)

Solution:

The issue was that there was some metatable function in the table that would prevent the change in the table (briefly) In, the person who created the script was a config table where no The composition was important, thus causing this problem.)

If I understand your description correctly So what does this Lua code want for you:

  local ab = a [b]] al [d], ab [c] [d] = ab [c] [d ],   

To apply in LuaC API, this machine is helpful with translation:

  enum {lc_nformalargs = 0}; Const int lc_nactualargs = lua_gettop (L); Const int lc_nextra = (lc_nactualargs - lc_nformalargs); / * Local AB = A [B] * / lua_getfield (L, LUA_ENVIRONINDEX, "A"); Lua_getfield (L, LUA_ENVIRONINDEX, "B"); Lua_gettable (L, -2); Lua_remove (L, -2); Emphasis (lua_gettop (L) - lc_nextra == 1); / * Ab [d], ab [c] [d] = ab [c] [d], zero * / lua_getfield (l, lu_neviron idx, "c"); Lua_gettable (L, (1+ LC_extractor)); Lua_getfield (L, LUA_ENVIRONINDEX, "D"); Lua_gettable (L, -2); Lua_remove (L, -2); Lua_pushnil (l); Lua_getfield (L, LUA_ENVIRONINDEX, "C"); Lua_gettable (L, (1+ LC_extractor)); Lua_insert (L, -2); Lua_getfield (L, LUA_ENVIRONINDEX, "D"); Lua_insert (L, -2); Lua_settable (L, -3); Lua_pop (L, 1); Lua_getfield (L, LUA_ENVIRONINDEX, "D"); Lua_insert (L, -2); Lua_settable (L, (1+ LC_extractor)); Emphasis (lua_gettop (L) - lc_nextra == 1); Return 0;   

I have yet to develop a readable way of writing stack operations.

No comments:

Post a Comment