我要实现用C语言函数,将由Lua的脚本中调用。
这个功能应该得到一个Lua表(甚至包含数组)作为参数,所以我应该阅读table.I领域尝试做像下面,但是当我运行它,我的功能崩溃。 谁能帮我找到了问题?
/*
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;
}