我是否正确地将嵌套的 lua 表作为 C 函数的参数



我将用C语言实现一个函数,该函数将由Lua脚本调用。

这个函数应该接收一个lua表(甚至包含一个数组)作为参数,所以我应该读取表中的字段。我尝试像下面那样做,但是当我运行它时我的函数崩溃了。谁能帮我找到问题?


/*
 function findImage(options)
    imagePath = options.imagePath
    fuzzy = options.fuzzy
    ignoreColors = options.ignoreColor;
    ...
 end
 Call Example:
 findImage {
              imagePath="/var/image.png", 
              fuzzy=0.5,
              ignoreColors={
                             0xffffff, 
                             0x0000ff, 
                             0x2b2b2b
                           }
            }
 */
static int findImgProxy(lua_State *L)
{
    lua_settop(L, 1);
    luaL_checktype(L, 1, LUA_TTABLE);
    lua_getfield(L, -1, "imagePath");
    lua_getfield(L, -2, "fuzzy");
    lua_getfield(L, -3, "ignoreColors");
    const char *imagePath = luaL_checkstring(L, -3);
    double fuzzy    = luaL_optint(L, -2, -1);
    int count  = lua_len(L, -1); // how to get the member count of ignoreColor array
    int colors[count];
    for (int i=0; i count; i++) {
        lua_rawgeti(L, 4, i);
        colors[i] = luaL_checkinteger(L, -1);
        lua_pop(L, 1);
    }
    lua_pop(L, 2);
    ...
    return 1;
}

lua_len 不返回任何内容,它只推动堆栈上的长度。使用此代码段获取表长度:

lua_len(L, -1);
int count = luaL_checkinteger(L, -1);
lua_pop(L, 1);
int count  = lua_len(L, -1); // how to get the member count of ignoreColor array
int colors[count];
for (int i=0; i count; i++)
{
    colors[i] = luaL_checkinteger(L, -1-i);
}

此代码段看起来不正确(不要介意循环中缺少的比较运算符)。获取表长度的正确函数是 lua_objlen 。看起来您正在尝试从"ignoreColor"中获取数字,但您没有首先将它们放在堆栈上。结果luaL_checkinteger(L, -1-i);最终访问堆栈上的错误索引

例如,您可能想要更接近此的内容:

int count  = lua_objlen(L, -1);
std::vector<int> colors(count);
for (int i = 0; i < count; lua_pop(L, 1))
{
  lua_rawgeti(L, 4, ++i);
  colors.push_back( luaL_checkinteger(L, -1) );
}

如果您使用的是 Lua 5.2,请将lua_objlen替换为:

int count  = lua_rawlen(L, -1);
如果您要

从表中移动大量元素,请确保堆栈上有足够的空间。 lua_checkstack

最新更新