c++ - 在 C++ 中读取 Lua 嵌套表

标签 c++ c lua lua-table nested-table

我正在创建一个将从 Lua 调用的 C/C++ 函数。我的函数必须调用签名如下的库函数:

void libFunction( int val1, int val2, tSETTINGS * pSettings );

我得到了这些 C/C++ 结构:

typedef struct
{
    int cmd;
    int arg;
} tCOMMAND;
typedef struct
{
    int numberCommands;
    int id;
    tCOMMAND commands[1];
} tSETTINGS;

也许我的想法在这方面是错误的,但是从 Lua 我是这样调用的:

id   = 42
val1 = 1
val2 = 2
cmd1 = { 3, 4 }
cmd2 = { 5, 6 }
commands = { cmd1, cmd2 }
settings = { #commands, id, commands }
mycfunction( val1, val2, settings )

我确定我仍然不理解从 C++ 引用的 Lua 堆栈,因为我正在尝试的方法不起作用。我的解决方案能够检索 val1val2#commandsid,但是当我尝试检索 commands[0]commands[1] 我分别得到 {1, 2}{2, 42} .

我的 C++ 本质上是这样的(对于这个示例,我丢弃了值)。我已经检索到 val1val2:

int stkNdx = 1;
lua_rawgeti(L, 3, stkNdx++ );
int numcmds = lua_tointeger(L, -1);  // this successfully retrieves numberCommands 2
lua_pop(L, 1);
lua_rawgeti(L, 3, stkNdx++ );
int id = lua_tointeger(L, -1);       // this successfully retrieves id 42
lua_pop(L, 1);

lua_pushvalue(L, -1 );
lua_pushnil(L);
int cmdNbr = 0;
for( lua_next(L, -2); cmdNbr < numcmds; cmdNbr++ )
{
    lua_pushvalue(L, -2);
    int cmd = lua_tointeger(L, -1);
    int arg = lua_tointeger(L, -1);
    lua_pop(L, 2);
    lua_next(L, -2);
}
lua_pop(L, 1);

我已经尝试了 lua_rawgeti() 的各种排列,然后是 lua_tonumber()lua_pop(),结果基本相同。

这似乎类似于 this question ,而我的解决方案是仿照它而没有成功的。

实验更多我插入这个:

lua_pushnil(L);
while( lua_next(L, -2) )
{
    if( ! lua_istable(L, -1) )
    {
        int v = lua_tointeger(L, -1);
    }
    lua_pop(L, 1);
}

这个循环执行了 4 次。前 2 次将值 2 和 42 分配给 v。接下来的 2 次迭代跳过赋值(lua_istable 返回 true)。所以看起来虽然我已经检索了 numcmdsid,但它们仍然在堆栈中。我也显然不明白如何在遇到子表时对其进行迭代。

最佳答案

Lua 表索引范围从 [1 .. N] 而不是 [0 .. N-1]。

你的循环应该是:

int cmdNbr = 1;
for( lua_next(L, -2); cmdNbr <= numcmds; cmdNbr++ )
{
  ...
}

或者我更喜欢:

lua_rawgeti(L, 3, 2 );
int id = lua_tointeger(L, -1);       // this successfully retrieves id 42
lua_pop(L, 1);

lua_rawgeti(L, 3, 3);
{
    // commands table at stack top
    size_t N = lua_objlen(L,-1); // size of the table
    
    for (int i = 1; i <= N; ++i)
    {
        lua_rawgeti(L,-1, i); // cmd# at stack top
        {
            lua_rawgeti(L,-1,1); // first entry
            int cmd = lua_tointeger(L,-1);
            lua_pop(L,1);
            
            lua_rawgeti(L,-1,2); // second entry
            int arg = lua_tointeger(L,-1);
            lua_pop(L,1);
        }
        lua_pop(L, 1); // pop cmd#
    }
}
lua_pop(L, 1); // pop commands table

请注意,使用函数 lua_objlen(L,idx),无需传递 numcmds

关于c++ - 在 C++ 中读取 Lua 嵌套表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16975760/

相关文章:

c++ - 将字符打印到控制台应用程序

c++ - 对话框关闭时 Win32 应用程序立即退出

C语言这段代码是什么意思? if(按钮 & 1)==1

c++ - 如何从 C++ 中清除 lua 堆栈中的所有内容?

Lua:我可以在这里使用冒号语法吗?

c++ - c++中的加法ASCII

c++ - 使用 this->,::and ordering members 会提高编译速度吗?

c - 函数调用是现代平台的有效内存屏障吗?

c++ - 将flex生成的代码放到一个普通的C++程序中

lua - 3d 中圆上的最近点。少了什么东西?