lua_isstring 在 table 中迭代 table 的问题
Problem with lua_isstring iterating for table in table
我尝试将 table 写入 ini 文件,一切正常,直到我添加一行 lua_tostring(L, -2) 然后 lua_next(L, -2) 开始报错。这条线如何影响,在我的理解中,我只是从堆栈中获取价值,仅此而已。我该如何解决?
{
// Push an initial nil to init lua_next
lua_pushnil(inOutState);
// Parse the table at index
while (lua_next(inOutState, -2))
{
if (lua_isstring(inOutState, -1)) {
string key = lua_tostring(inOutState, -2);
string value = lua_tostring(inOutState, -1);
inIniTree.put(suffix + key, value);
}
else if (lua_istable(inOutState, -1)) {
suffix += lua_tostring(inOutState, -2); !!!!!! without this line function is working well !!!!!!!
setDataInIni(inOutState, inIniTree, suffix);
}
// Pop value, keep key
lua_pop(inOutState, 1);
}
return;
}
如果值不是字符串类型,则 lua_tostring()
替换 堆栈中的值。这意味着您更改了 lua_next()
的密钥。您必须使用 lua_pushvalue()
复制值,然后将其转换为字符串。
if (lua_isstring(inOutState, -1)) {
lua_pushvalue(inOutState, -2);
string key = lua_tostring(inOutState, -1);
lua_pop(L, 1);
string value = lua_tostring(inOutState, -1);
inIniTree.put(suffix + key, value);
}
else if (lua_istable(inOutState, -1)) {
lua_pushvalue(inOutState, -2);
suffix += lua_tostring(inOutState, -1);
lua_pop(L, 1);
setDataInIni(inOutState, inIniTree, suffix);
}
我尝试将 table 写入 ini 文件,一切正常,直到我添加一行 lua_tostring(L, -2) 然后 lua_next(L, -2) 开始报错。这条线如何影响,在我的理解中,我只是从堆栈中获取价值,仅此而已。我该如何解决?
{
// Push an initial nil to init lua_next
lua_pushnil(inOutState);
// Parse the table at index
while (lua_next(inOutState, -2))
{
if (lua_isstring(inOutState, -1)) {
string key = lua_tostring(inOutState, -2);
string value = lua_tostring(inOutState, -1);
inIniTree.put(suffix + key, value);
}
else if (lua_istable(inOutState, -1)) {
suffix += lua_tostring(inOutState, -2); !!!!!! without this line function is working well !!!!!!!
setDataInIni(inOutState, inIniTree, suffix);
}
// Pop value, keep key
lua_pop(inOutState, 1);
}
return;
}
lua_tostring()
替换 堆栈中的值。这意味着您更改了 lua_next()
的密钥。您必须使用 lua_pushvalue()
复制值,然后将其转换为字符串。
if (lua_isstring(inOutState, -1)) {
lua_pushvalue(inOutState, -2);
string key = lua_tostring(inOutState, -1);
lua_pop(L, 1);
string value = lua_tostring(inOutState, -1);
inIniTree.put(suffix + key, value);
}
else if (lua_istable(inOutState, -1)) {
lua_pushvalue(inOutState, -2);
suffix += lua_tostring(inOutState, -1);
lua_pop(L, 1);
setDataInIni(inOutState, inIniTree, suffix);
}