【发布时间】:2012-12-08 10:04:47
【问题描述】:
诚然,我的问题与此类似: How to create nested Lua tables using the C API
我以为我理解了那个答案,但我仍然有问题。
我有一个我想要返回的对象数组。
const char * pushlist[] = {
"status", "cmdsequence", "timestamp", "gain",
};
int nItems = sizeof(pushlist) / sizeof(char *);
int iDepth = -(1 + nItems);
// //
// What we want to do is essentially push an array of tables.
// The tables have keys (see pushlist above) and values.
// The array is indexed by integers from 1 through N.
//
lua_newtable( L );
for( Json::Value::UInt i = 0; i != totFrames; i++ )
{
lua_pushnumber( L, i + 1 ); // push the array index
lua_newtable( L );
Json::Value frame = params["frameinfo"][i];
// now push the table which will be at array index (i + 1)
for( int n = 0; n < nItems; n++ )
{
lua_pushstring( L, pushlist[n] ); // push key
lua_pushnumber( L, frame[pushlist[n]].asUInt() ); // push value
}
lua_settable(L, iDepth);
lua_settable(L, -3); // (note 1) error here
}
lua_settable(L, iDepth); // (note 2) not clear on the need for this
lua_settable(L, -3);
lua_setglobal( L, "framedata" );
所以我想在 Lua 中看到:
[0] = {["status"] = 1, ["cmdsequence"] = 2, ["timestamp"] = 3, ["gain"] = 4}
...
[totFrames-1] = {["status"] = 5, ["cmdsequence"] = 6, ["timestamp"] = 7, ["gain"] = 8}
我不清楚注释 2 中 2 个 lua_settable 的用途,但我在上面链接到的答案表明它们是必需的。
lua_settable(L, -3)(注 1)出错。我在 C++ 中执行此操作,因此我将该代码括在 try/catch 中。当它第一次击中那个可设置的位置时,它会跳出并接住我。我在想我已经以某种方式损坏了堆栈,但我没有看到它。
感谢@Omri Barel 的出色回答。我仍然不清楚内部“for”循环之后要做什么。
我现在有这个: 常量字符 * pushlist[] = { “状态”,“cmdsequence”,“时间戳”,“增益”, }; int nItems = sizeof(pushlist) / sizeof(char *);
// //
// What we want to do is essentially push an array of tables.
// The tables have keys (see pushlist above) and values.
// The array is indexed by integers from 1 through N.
//
lua_newtable( L );
for( Json::Value::UInt i = 0; i != totFrames; i++ )
{
lua_pushnumber( L, i + 1 ); // push the array index
lua_newtable( L );
Json::Value frame = params["frameinfo"][i];
// now push the table which will be at array index (i + 1)
for( int n = 0; n < nItems; n++ )
{
lua_pushnumber( L, frame[pushlist[n]].asDouble() ); // push value
lua_setfield(L, -2, pushlist[n] );
}
lua_settable(L, -3); // (note 1) error here
}
//lua_settable(L, -3); <<-- not certain that this is required
lua_setglobal( L, "framedata" );
我不再爆炸,但我的 Lua 失败(没有错误消息,它只是退出)。我怀疑我没有损坏堆栈,但不知何故我没有正确完成这个表,所以我的返回很混乱。
我在这个数组之前将其他几个返回值压入 Lua 堆栈,然后再压入一个。
我的 Lua 调用是这样的:
param1,param2,framedata,Err = CCall.ReadFromC( arg, arg );
我终于有这个工作了。它需要进一步测试,但到目前为止似乎是正确的。再次感谢@Omri Barel。这是我最终得到的代码 sn-p。
const char * pushlist[] = {
"status", "cmdsequence", "timestamp", "gain",
};
int nItems = sizeof(pushlist) / sizeof(char *);
// //
// What we want to do is essentially push an array of tables.
// The tables have keys (see pushlist above) and values.
// The array is indexed by integers from 1 through N.
//
lua_newtable( L );
for( Json::Value::UInt i = 0; i != totFrames; i++ )
{
Json::Value frame = params["frameinfo"][i];
// now push the table which will be at array index (i + 1)
lua_newtable( L );
for( int n = 0; n < nItems; n++ )
{
const char * itemName = pushlist[n];
if( frame[itemName].isNull() ) continue;
lua_pushnumber( L, frame[pushlist[n]].asDouble() ); // push value
lua_setfield(L, -2, pushlist[n] );
}
lua_rawseti(L, -2, i + 1);
}
【问题讨论】:
-
当
x[i]=y设置i为数字时,我建议你使用lua_rawseti而不是lua_pushnumber后跟lua_settable。