如何从 C++ 创建 Lua C++ 包装。

我有一些 Lua "对象",它们是 C++ 对象的包装器,它们保存了对 C++ 对象的本地引用并调用它。

现在我想在 C++ 中返回这些包装器的一些函数,所以我需要调用这个 lua 函数,然后在它上面设置 C++ 对象。

我经历了崩溃,怀疑我没有正确处理 lua 堆栈。例如,如果我在创建包装器 + c++ 对象的函数退出之前询问 lua_top,我将得到5作为结果,如果我只返回一个对象,它不应该是1吗?

所以这就是我所做的事情,也许我做错了,也许有更好的方法来做到这一点。

c++,.h:

#define gLuaGet(L, var, type) \
    if (lua_istable(L, 1)) {\
        lua_getfield(L, 1, "CObj");\
        lua_replace(L, 1);\
    }\
    type& var = *(type*)lua_touserdata(L, 1);

#define gLuaCreate(L, type) new (lua_newuserdata(L, sizeof(type))) type();

class MyObject {
    public:
       MyObject();

       int somefunc();
};

int MyObjectCreate(lua_State *L);
int MyObjectCallSomefunc(lua_State *L);

c++,.cpp:

int MyObject::somefunc() {
    std::cerr << "in c++ function" << std::endl;
    return 123;
}

int MyObjectCreate(lua_State *L) {
    gLuaCreate(L, MyObject);
    return 1;
}

int MyObjectCallSomefunc(lua_State *L) {
    gLuaGet(L, obj, MyObject);
    int r = obj.somefunc();
    lua_checkstack(L, 1);
    lua_pushnumber(L, r);
    return 1;
}

lua 包装器:

function MyObject(donotinit)
    self = {}
    self.CObj = nil

    if (donotinit == nil) then
        self.CObj = MyObjectCreate()
    end

    self.setCObject = function(obj)
        self.CObj = obj
    end

    self.somefunc = function()
        return MyObjectCallSomeFunc(self)
    end

    return self
end

现在我想让另一个包装器返回一个在 c++ 中创建的 MyObject,因此这是从新包装器调用的 c++ 代码(为了更好地阅读,我删除了 lua_pcall 的健全性检查):

int returnLuaMyObject(lua_State *L) {
    gLuaGet(L, obj, MyOtherObject);
    MyObject *myObject = obj.getMyObject(); // get c++ part
    lua_getglobal(L, "MyObject"); // create lua part
    lua_pushnumber(L, 1); // and tell it not to initialize the self.CObj
    lua_pcall(L, 1, 1, 0);
    lua_getfield(L, -1, "setCObject"); // call the setCObject function
    lua_pushlightuserdata(L, myObject); // give c++ object as param
    lua_pcall(L, 1, 0, 0);
    // at this point lua_gettop(L); returns 5, can this be correct?
    return 1;
}

好吧,如果我现在通过一个 lua 包装器调用这个函数几次,一切似乎都很好,但是如果我在 while 循环中调用它,例如50次,它会在随机时间崩溃(但是总是在相同的 c++ 行上)

我在这里做错了什么?lua 堆栈顶部在这一点上是5,而它只返回一个对象,这可以吗?

点赞
用户938694
用户938694

在 Lua 中,堆栈的顶端为 -1 而不是 1(或 lua_gettop(state))。另外,你应该真正使用模板而不是宏来实现这个。或者更好的是,除非你有理由不这样做,你可以使用 luabindtolua++。我正在编写一个与 luabind 实质上相同的东西,但使用了 C++11 特性来消除 boost 的依赖,尽管它还远未完成。

2013-03-17 12:21:59
用户63791
用户63791

如果我理解正确,每个函数/宏调用后,您的Lua堆栈将如下所示:

int returnLuaMyObject(lua_State *L) {
    // arg
    gLuaGet(L, obj, MyOtherObject); // arg.CObj
    MyObject *myObject = obj.getMyObject();
    lua_getglobal(L, "MyObject"); // arg.CObj _G.MyObject
    lua_pushnumber(L, 1); // arg.CObj _G.MyObject 1
    lua_pcall(L, 1, 1, 0); // arg.CObj obj
    lua_getfield(L, -1, "setCObject"); // arg.CObj obj obj.setCObject
    lua_pushlightuserdata(L, myObject); // arg.CObj obj obj.setCObject myObject
    lua_pcall(L, 1, 0, 0); // arg.CObj obj

    // 在这一点上,lua_gettop(L); 返回2。
    // 如果您以return 1结束此函数,则只返回obj到Lua,其他所有内容都被丢弃。

    返回1;
}

(值得一提的是,在编写Lua代码时,我会对每个对Lua堆栈进行操作的行都进行注释,以便我始终知道我正在处理什么。一旦您记住了Lua函数的副作用,就会使错误排查变得非常容易)

假设returnLuaMyObject是从Lua中调用的,这种方法应该没问题。但如果你在C++中循环调用它,你的堆栈就会混乱,因为在堆栈上有两个东西,而且你的一些函数是硬编码的,要操作第一个索引。

更好的方法是像Cubic建议的那样使用一些模板而不是宏。您还应该在可能的情况下避免使用硬编码的索引,以便您可以在对象在堆栈上的不同位置的情况下重用您的函数。例如,您的gLuaGet应该接受一个索引作为参数,以便您可以在任何地方使用它。 (我还会去掉obj参数并删除宏的最后一行,这样就不清楚变量obj在哪里声明了。

我为自己编写了一个库(巧合的是称为LuaWrapper,位于[此处](https://bitbucket.org/alexames/luawrapper/src)),它允许您在不使用许多麻烦的情况下像数字或字符串一样从Lua中推动和获取指针。我只是在这里提供它,因为我比Luabind或toLua ++更喜欢它。

2013-03-17 18:19:15