How to create nested Lua tables using the C API

前端 未结 3 1613
北海茫月
北海茫月 2020-12-13 11:20

I want to create a table like

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

using the C AP

相关标签:
3条回答
  • 2020-12-13 12:05

    Here is something generic I came up to solve a similar problem, based on lhf's answer. This will create a Lua table for the form

    {
        {"foo"},
        {"bar", "baz"}
    }
    

    With arbitrary table/sub table length.

    int list_of_lists_to_lua(lua_State* L, const std::vector<std::vector<std::string>>& convertme) {
        lua_newtable(L);
        int counter = 0;
        for (const std::vector<std::string>& list : convertme) {
            lua_pushnumber(L, ++counter);
            lua_newtable(L);
            int counter2 = 0;
            for (const std::string& item : list) {
                lua_pushnumber(L, ++counter2);
                lua_pushstring(L, item);
                lua_settable(L,-3);
            }
            lua_settable(L,-3);
        }
        return 1;
    }
    
    0 讨论(0)
  • 2020-12-13 12:06

    For simple code like the one you gave, my lua2c works fine and generates the code below.

    /* This C code was generated by lua2c from the Lua code below.
    
    myTable = {
        [0] = { ["a"] = 4, ["b"] = 2 },
        [1] = { ["a"] = 13, ["b"] = 37 }
    }
    */
    static int MAIN(lua_State *L)
    {
     lua_newtable(L);
     lua_pushnumber(L,0);
     lua_newtable(L);
     lua_pushliteral(L,"a");
     lua_pushnumber(L,4);
     lua_pushliteral(L,"b");
     lua_pushnumber(L,2);
     lua_settable(L,-5);
     lua_settable(L,-3);
     lua_pushnumber(L,1);
     lua_newtable(L);
     lua_pushliteral(L,"a");
     lua_pushnumber(L,13);
     lua_pushliteral(L,"b");
     lua_pushnumber(L,37);
     lua_settable(L,-5);
     lua_settable(L,-3);
     lua_settable(L,-5);
     lua_settable(L,-3);
     lua_setglobal(L,"myTable");
     return 0;
    }
    
    0 讨论(0)
  • 2020-12-13 12:09

    Here's a full and minimal program demonstrating how to nest tables. Basically what you are missing is the lua_setfield function.

    #include <stdio.h>
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    
    int main()
    {
        int res;
        lua_State *L = lua_open();
        luaL_openlibs(L);
    
        lua_newtable(L); /* bottom table */
    
        lua_newtable(L); /* upper table */
    
        lua_pushinteger(L, 4);
        lua_setfield(L, -2, "four"); /* T[four] = 4 */
        lua_setfield(L, -2, "T");  /* name upper table field T of bottom table */
        lua_setglobal(L, "t"); /* set bottom table as global variable t */
    
        res = luaL_dostring(L, "print(t.T.four == 4)");
        if(res)
        {
            printf("Error: %s\n", lua_tostring(L, -1));
        }
    
        return 0;
    }
    

    The program will simply print true.

    If you need numeric indices, then you continue using lua_settable:

    #include <stdio.h>
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    
    int main()
    {
        int res;
        lua_State *L = lua_open();
        luaL_openlibs(L);
    
        lua_newtable(L); /* bottom table */
    
        lua_newtable(L); /* upper table */
    
        lua_pushinteger(L, 0);
        lua_pushinteger(L, 4);
        lua_settable(L, -3);  /* uppertable[0] = 4; pops 0 and 4 */
        lua_pushinteger(L, 0);
        lua_insert(L, -2); /* swap uppertable and 0 */
        lua_settable(L, -3); /* bottomtable[0] = uppertable */
        lua_setglobal(L, "t"); /* set bottom table as global variable t */
    
        res = luaL_dostring(L, "print(t[0][0] == 4)");
        if(res)
        {
            printf("Error: %s\n", lua_tostring(L, -1));
        }
    
        return 0;
    }
    

    Rather than using absolute indices of 0 like I did, you might want to use lua_objlen to generate the index.

    0 讨论(0)
提交回复
热议问题