我说的对读嵌套表卢阿从C函数的参数? [英] Am I right to read a nested lua table as argument from C function?
问题描述
我要实现与C语言的函数,将由Lua的脚本调用。
这个功能应该得到一个Lua表(其中甚至包含数组)作为参数,所以我应该读取table.I领域尝试做如下图所示,但是当我运行我的功能崩溃。谁能帮我找到了问题?
/ *
功能findImage(选项)
的ImagePath = options.imagePath
模糊= options.fuzzy
ignoreColors = options.ignoreColor;
...
结束 调用示例: findImage {
的ImagePath =/无功/ image.png
模糊= 0.5,
ignoreColors = {
0XFFFFFF,
0x0000ff,
0x2b2b2b
}
} * /静态INT findImgProxy(lua_State * L)
{
lua_settop(L,1);
luaL_checktype(L,1,LUA_TTABLE); lua_getfield(L,-1,的ImagePath);
lua_getfield(L,-2,模糊);
lua_getfield(L,-3,ignoreColors); 为const char *的ImagePath = luaL_checkstring(L,-3);
双模糊= luaL_optint(L,-2,-1); 诠释计数= lua_len(L,-1); //如何获得ignoreColor数组的成员数 INT颜色[统计]
的for(int i = 0;我算,我++){
lua_rawgeti(L,4,i)的;
颜色由[i] = luaL_checkinteger(L,-1);
调用lua_pop(L,1);
} 调用lua_pop(L,2); ...
返回1;
}
诠释计数= lua_len(L,-1); //如何获得ignoreColor数组的成员数INT颜色[统计]
的for(int i = 0;我算,我++)
{
颜色由[i] = luaL_checkinteger(L,-1-i)的;
}
这样做的code段看上去不正确(不去管回路中比较缺少运算符)。获取表长度正确的函数是 lua_objlen
。它看起来像你想得出的数字出来ignoreColor',但你有没有先放置在堆栈中。结果 luaL_checkinteger(L,-1-I);
结束访问堆栈上的错指数的
您可能想例如更接近于这样的:
诠释计数= lua_objlen(L,-1);
的std ::矢量<&INT GT;颜色(计数);
对(INT I = 0; I&下;计数;调用lua_pop(L,1))
{
lua_rawgeti(1,4,+ⅰ);
colors.push_back(luaL_checkinteger(L,-1));
}
如果您正在使用的Lua 5.2,替换 lua_objlen
与
诠释计数= lua_rawlen(L,-1);
确认有堆栈上有足够的空间,如果你移动了很多从表元素。例如。 lua_checkstack
I'm going to implement a function with C language and which will be called by Lua script.
This function should receive a lua table(which even contains an array) as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I run it. Can anyone help my find the problem?
/*
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;
}
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);
}
The code segment for this looks incorrect (never mind the missing comparision operator in the loop). The correct function for getting the table length is lua_objlen
. It looks like you're trying to get the numbers out of 'ignoreColor' but you haven't place them on the stack first. As a result luaL_checkinteger(L, -1-i);
ends up accessing the wrong indices on the stack.
You probably want something closer to this for example:
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) );
}
If you're using Lua 5.2, replace lua_objlen
with:
int count = lua_rawlen(L, -1);
Make sure there's enough space on the stack if you're moving a lot of elements from the table. eg. lua_checkstack
这篇关于我说的对读嵌套表卢阿从C函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!