How to use lua_pop() function correctly?

孤人 提交于 2020-01-21 01:41:33

问题


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

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

    lua_getglobal(L, "something");

    lua_pop(L, 1);

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


回答1:


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.

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.

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;
}

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.

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.



来源:https://stackoverflow.com/questions/1217423/how-to-use-lua-pop-function-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!