在 C/C++ 中引用 Lua 函数

2021-12-30 00:00:00 function c function-pointers lua c++

我有一个函数嵌套在一组表中相对较深.C/C++ 中有没有办法在我需要使用它时获得对该函数的引用"并将其(和参数)推入堆栈?

I have a functions nested relatively deeply in a set of tables. Is there a way in C/C++ to get a "reference" to that function and push that (and args) onto the stack when I need to use it?

推荐答案

这就是 参考系统是为了.函数调用 r = luaL_ref(L, LUA_REGISTRYINDEX) 将值存储在注册表中的栈顶并返回一个整数,该整数可以存储在 C 端并用于检索该值函数调用lua_rawgeti(L, LUA_REGISTRYINDEX, r).

This is what the reference system is for. The function call r = luaL_ref(L, LUA_REGISTRYINDEX) stores the value on the top of the stack in the registry and returns an integer that can be stored on the C side and used to retrieve the value with the function call lua_rawgeti(L, LUA_REGISTRYINDEX, r).

请参阅PiL 章节,以及luaL_ref(), lua_rawgeti()luaL_unref() 了解完整故事.

See the PiL chapter, as well as the documentation of luaL_ref(), lua_rawgeti(), and luaL_unref() for the full story.

相关文章