我是否正确地从C函数中读取嵌套的lua表作为参数?

我将用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 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