c++ - lua_isstring 迭代表中表的问题
问题描述
我尝试将表写入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);
}
推荐阅读
- javascript - 通过移动浏览器扫描条码?
- java - IntelliJ 中的多模块项目
- python-3.x - 在 OpenCV 中查找颜色蒙版线
- python - 使用 JupyterLab 将许多图像、表格和打印语句导出到 Python 中的 RTF 或 DOCX 文件
- google-apps-script - 如何使用 onEdit 函数触发邮件
- asp.net-core - 支持多部分/表单数据时,如何在 net core 5 webapi 中使用默认格式化程序反序列化对象?
- ruby-on-rails - 如何在我的 Rails 项目中安装这个包?
- powerbi - HASONEFILTER SUMX 总计未正确计算
- java - 将缓冲读取器输入存储到多个变量
- python - 使用自定义方法计算 numpy.ndarray 标准差的问题