如何正确使用lua_pop()函数? [英] How to use lua_pop() function correctly?

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

问题描述

任何人都可以告诉我如何在C ++中正确使用lua_pop()函数。


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

    lua_getglobal(L,something);

    lua_pop(L,1) p>




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

解决方案

您可以调用 lua_pop() Lua堆栈。对于简单的函数,这可能是完全没有必要的,因为核心将清理堆栈作为处理返回值的一部分。



对于更复杂的函数,特别是对于C代码这是调用到Lua中,你通常需要从栈中弹出东西来防止堆栈无限增长。


$ b $ < lua_getglobal()函数在调用时向堆栈添加一个项目,如果全局不存在或者全局变量的值为 nil 。在堆栈中拥有该值的副本可以保护其不受垃圾收集器的影响。只要C代码检索到该值,该值就需要保留在堆栈上,因为如果全局被修改,则堆栈上的副本可能是唯一剩下的引用。



因此,使用全局的一般模式如下所示:

  void doMyEvent(lua_State * L ){
lua_getglobal(L,MyEvent);
lua_call(L,0,0); / *弹出函数和0参数,推0结果* /
}

double getGlobalDouble(lua_State * L,const char * name){
double d;
lua_getglobal(L,name);
d = lua_tonumber(L,1); / *提取值,保持堆栈不变* /
lua_pop(L,1); / *弹出的值离开堆栈平衡* /
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;

$ / code>

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



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


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.

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆