调用Lua:使用setField来指向自身
C++ Call Lua: use setField to point itself?
在c++中是这样的:
l_newTable(L); //Stack: newtable
l_getGlobal(L, "A"); //Stack: newtable A
l_setfiled(-2, "__index") //Stack: newtable => newtable.__index = A
这是正常的。然而,如果我想设置一个新的表的__newindex本身,如何编写代码?它需要将__newindex设置为newTable本身,但使用setfield将调用Pop,这意味着:
l_newTable(L); //Stack: newtable
l_setfield(-1, "__newindex") //Stack: -1 => newtable will pop out
你应该在newtable
的值从堆栈中弹出之前使用lua_pushvalue来复制它:
lua_newTable(L); //Stack: newtable
lua_pushvalue(L, -1); //Stack: newtable, newtable
lua_setfield(-2, "__newindex") //Stack: newtable => newtable.__newindex = newtable
相关文章:
- C++Lua用户数据扰乱了Mathfu浮点值
- 使用 LuaBridge 或 Lua 将对象传递给 Lua 函数C++
- 将 Lua 函数块转换为 C 字符串
- 我可以从 lua 5.0.2 加载用 c++ 编写的 lua 5.3.5 dll 吗?
- 使用 lua 协程 API 和lua_close时出现分段错误
- 使用 C++ 自动 Lua 绑定
- 调用 lua 函数的地址为 C/C++?
- lua 5.0.2 模块和 5.3.5 有什么区别?
- 如何将 Lua 添加到C++项目
- 使用 Lua 中的C++库对象
- 如何在C++中将用户数据从一个 Lua 块传递到另一个块
- 如何使用重新定义的打印函数打印Lua表?
- Lua table in C++ dll
- 在运行时环境中使用 C++ w/ Lua
- 将shared_ptr传递给Lua
- luaL_len is missing in Lua 5.1
- 将向量传递给<struct> Lua 表
- 将 Lua 与 C++ DLL 结合使用
- Lua C API - 从C++分配和使用类的对象成员
- 调用Lua:使用setField来指向自身