我的问题与此类似: 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] = {[“状态”] = 5,[“cmdsequence”] = 6,[“时间戳”] = 7,[“增益”] = 8}
我不清楚注释 2 中 2 个 lua_settable 的用途,但我在上面链接到的答案表明它们是必需的。
lua_settable(L, -3)(注 1)出错了。我在 C++ 中执行此操作,因此我将该代码括在 try/catch 中。当它第一次击中那个可设置的位置时,它会跳出并接住我。我在想我已经以某种方式破坏了堆栈,但我没有看到它。
感谢@Omri Barel 的出色回答。我仍然不清楚内部“for”循环之后要做什么。
我现在有这个: 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++ )
{
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。这是我最终得到的代码片段。
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);
}