本文介绍了如何正确使用 lua_pop() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在 C++ 中正确使用 lua_pop() 函数.

Can anyone pls tell me that how to use lua_pop() function correctly in C++.

  • 我应该在使用 lua_get*() 函数时调用它吗?喜欢.

  • Should I call it when I use a lua_get*() function ? like.

lua_getglobal(L, "something");

lua_getglobal(L, "something");

lua_pop(L, 1);

lua_pop(L, 1);

或者如何使用它?垃圾收集器会在阈值后清除那些东西吗?谢谢.

or how to use it ? Will the garbage collector clear those stuff after the threshold ? Thanks.

推荐答案

您调用 lua_pop() 从 Lua 堆栈中删除项目.对于简单的函数,这完全没有必要,因为内核会在处理返回值时清理堆栈.

You call lua_pop() to remove items from the Lua stack. For simple functions, this can be entirely unnecessary since the core will clean up the stack as part of handling the return values.

对于更复杂的函数,尤其是调用 Lua 的 C 代码,你经常需要从栈中弹出东西以防止栈无限增长.

For more complex functions, and especially for C code that is calling into Lua, you will often need to pop things from the stack to prevent the stack from growing indefinitely.

lua_getglobal() 函数在调用时向堆栈添加一项,如果全局不存在,则为 nil 或命名的全局变量的值.只要它在堆栈上,在堆栈上有一个该值的副本就可以保护它免受垃圾收集器的影响.只要检索它的 C 代码正在使用该值,该值就需要保留在堆栈中,因为如果修改了全局变量,则堆栈上的副本可能是唯一剩余的引用.

The lua_getglobal() function adds one item to the stack when called, which is either nil if the global doesn't exist or the value of the named global variable. Having a copy of that value on the stack protects it from the garbage collector as long as it is there. That value needs to remain on the stack as long as it is in use by the C code that retrieved it, because if the global were modified, the copy on the stack might be the only remaining reference.

所以使用全局的一般模式是这样的:

So the general patterns for using a global are something like these:

void doMyEvent(lua_State *L) {
    lua_getglobal(L, "MyEvent");
    lua_call(L, 0, 0);  /* pops the function and 0 parameters, pushes 0 results */
}

double getGlobalDouble(lua_State *L, const char *name) {
    double d;
    lua_getglobal(L,name);
    d = lua_tonumber(L,1); /* extracts the value, leaves stack unchanged */
    lua_pop(L,1);          /* pop the value to leave stack balanced */
    return d;
}

char *copyGlobalString(lua_State *L, const char *name) {
    char *s = NULL;
    lua_getglobal(L,name);
    if (!lua_isnil(L,-1))
        s = strdup(lua_tostring(L,-1));
    lua_pop(L,1);
    return s;
}

在最后一个例子中,我很小心地复制了字符串的内容,因为lua_tostring()返回的指针只有在值保留在堆栈中时才保证有效.要求 copyGlobalString() 的调用者负责稍后调用 free().

In the last example, I am careful to copy the content of the string because the pointer returned by lua_tostring() is only guaranteed to be valid as long as the value remains on the stack. The requires that a caller of copyGlobalString() is responsible for calling free() later.

另请注意,Lua 手册 的最新版本包含一个符号以及每个函数标识消耗的堆栈条目的数量,以及推送的数量.这有助于避免意外的堆栈增长.

Note too that recent editions of the Lua manual include a notation along with each function that identifies the number of stack entries consumed, and the number pushed. This helps avoid unexpected stack growth.

这篇关于如何正确使用 lua_pop() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:09