lua-users home
lua-l archive

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


Hi Lua-l list,


On Wed, Sep 23, 2015 at 10:35:21AM +0200, Michal Kottman wrote:
> 
> Instead, take a look at Programming in Lua, especially
> http://www.lua.org/pil/24.2.html to learn about accessing Lua from C using
> the stack. The book is written for Lua 5.0, but still very relevant even
> with 5.3.

so, I've read the documents above - that's very-very clear. Now I
think I understand the stack - it's not just very funny, it has
cold logic, and pure implementation.

Here is the sample code, what I tries first - and there is the
state of stack, to help me to clarify the working of Lua stack.

Please review and comment it - em I understanding right it?

/*
 myTable = {
    [0] = { ["a"] = 4, ["b"] = 2 },
    [1] = { ["a"] = 13, ["b"] = 37 }
} */

 1. lua_newtable(L);         // -1: myTable

 2. lua_pushnumber(L,0);     // myTyble 0. index
                             // -2: myTable, -1: 0

 3. lua_newtable(L);         // myTable[0], -3: myTable, -2: 0, newtable0: -1
 4. lua_pushliteral(L,"a");  // -4: myTable, ..., -2: newtable0, -1: "a"
 5. lua_pushnumber(L,4);     // -5: myTable, ... -1: 4
 6. lua_pushliteral(L,"b");  // -6: myTable, ... -1: "b"
 7. lua_pushnumber(L,2);     // -7: myTable, ... -5: newtable0, -1: 2
 8. lua_settable(L,-5);      // set k=v pairs to newtable0 from top, on index -5
                             // items -1 and -2 will popped
                             // -5: myTable, -4: 0, -3: newtable0
 9. lua_settable(L,-3);      // set k=v pairs to newtable0 from top, on index -3
                             // items -1 and -2 will popped
                             // -3: myTable, -2: 0, -1: newtable0
10. lua_pushnumber(L,1);     // -4: myTable, -3: 0, -2: newtable0, -1: 1
11. lua_newtable(L);         // -5: myTable, -4: 0, -3: newtable0, -2: 1, -1: newtable1
12. lua_pushliteral(L,"a");  // -6: myTable, ... -1: "a"
13. lua_pushnumber(L,13);    // -7: myTable, ... -2: "a", -1: 13
14. lua_pushliteral(L,"b");  // -8: myTable, ... -1: "b"
15. lua_pushnumber(L,37);    // -9: myTable, ... -2: "b", -1: 37
16. lua_settable(L,-5);      // -10: myTable, -9: index 0, -8: newtable0, ... -5: newtable1
                             // set k=v pairs to newtable1 on index -5
                             // items -1 and -2 will popped
                             // -7: myTable, ..., -3: newtable1, -2: index a, -1: 13
17. lua_settable(L,-3);      // set k=v pairs to newtable1 from top, on index -3
                             // items -1 and -2 will popped
                             // -5: myTable, -4: 0, -3: newtable0, -2: 1, -1: newtable1
18. lua_settable(L,-5);      // set k=v pairs from top, in this case 1=newtable1
                             // -3: myTable, -2: 0, -1: newtable0
19. lua_settable(L,-3);      // set k=v pairs from top, 0=newtable0
20. lua_setglobal(L,"t");    // set table above to "t" variable in Lua, pop it from stack
                             // stack is empty

Note, that if I think it right, in the step 8 (lua_settable(L, -5))
I could use this: lua_settable(L, 3), because the newtable0 is
5th element from top, and 3rd element from bottom? Em I right?


Thanks for your pations, and for your helps,


a.