lua_getglobal(L, t);
int index = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, index)) {
/* 此时栈上 -1 处为 value, -2 处为 key */
lua_pop(L, 1);
}
lua_next
函数针对 -2 处(参数指定)的 Table 进行遍历。弹出 -1 处(栈顶)的值作为上一个 key(为 nil 时视为请求首个 key),压入 Table 中的下一个 key 和 value。返回值表示是否存在下一个 key。
另外在循环中处理值时要记得随时清理栈,否则 Table 就不在 -2 了。(也可以考虑在 lua_getglobal
后用lua_gettop
存下 Table 的正数索引。)
Study:
遍历Table参考: https://www.cnblogs.com/hangj/p/5192588.html
API讲解参考:https://www.cnblogs.com/lsgxeva/
Windows下的设置参考:https://www.cnblogs.com/fkpj/p/4495913.html
C/C++编写Lua扩展插件:https://blog.csdn.net/yue7603835/article/details/41878905
互调参考实例:https://shaomeng.blog.csdn.net/article/details/82945645
Lua与C++相互调用参考:https://blog.csdn.net/wangbin_jxust/article/details/38794111