【问题标题】:How to create nested Lua tables using the C API如何使用 C API 创建嵌套 Lua 表
【发布时间】:2010-12-10 10:51:10
【问题描述】:

我想创建一个类似的表

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

使用 C API?

我目前的做法是

lua_createtable(L, 0, 2);
int c = lua_gettop(L);
lua_pushstring(L, "a");
lua_pushnumber(L, 4);
lua_settable(L, c);
lua_pushstring(L, "b");
lua_pushnumber(L, 2);
lua_settable(L, c);

在循环中创建内部表。在此循环之前,我使用

lua_createtable(L, 2, 0);
int outertable = lua_gettop(L);

为 2 个数字槽创建外部表。

但是如何将内表保存到外表?

【问题讨论】:

    标签: c++ lua lua-table lua-api


    【解决方案1】:

    这是一个完整的最小程序,演示了如何嵌套表格。基本上你缺少的是lua_setfield 函数。

    #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;
    }
    

    程序将简单地打印true

    如果您需要数字索引,则继续使用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;
    }
    

    您可能希望使用lua_objlen 来生成索引,而不是像我那样使用 0 的绝对索引。

    【讨论】:

    • 如何使用 lua_setfield 创建数字索引?
    【解决方案2】:

    对于您提供的简单代码,我的lua2c 可以正常工作并生成以下代码。

    /* 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;
    }
    

    【讨论】:

      【解决方案3】:

      根据 lhf 的回答,这是我为解决类似问题而提出的通用方法。这将为表单创建一个 Lua 表

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

      具有任意表/子表长度。

      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;
      }
      

      【讨论】:

      • 看起来不像 C。没有适当的包含也不编译。
      • 这是使用 eris 的 C++。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2012-03-20
      • 1970-01-01
      • 2016-10-17
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多