将luabind派生成员调用为协同程序

Calling luabind derived member as a coroutine

本文关键字:程序 调用 luabind 派生 成员      更新时间:2023-10-16

luabind文档说,要从C++调用Lua派生的虚拟成员,您可以创建一个从luabind::wrap_base派生的包装器类,并调用如下函数:

class BaseWrapper : public Base, public luabind::wrap_base
{
    public:
        virtual void foo()
        {
            call<void>("foo");
        }
};

到目前为止还不错——我还有这么多工作要做。

但是,我如何实现BaseWrapper::foo(),将重写的foo(在Lua侧)作为协程调用(使用resume_function),而不是直接用call调用它?

这就是非成员函数的处理方式:

luabind::object func = luabind::globals(L)["bar"];
luabind::resume_function<void>(func);

我想我需要知道的是如何为foo获得func(由Lua派生类实现),然后我现有的resume_function逻辑应该按原样工作。

所以我已经找到了这个问题的答案。似乎最简单的解决方案是在构建对象时从Lua传递self,然后从其表中查找函数:

在C++方面:

BaseWrapper::BaseWrapper(luabind::object self) : _self(self)
{ }
virtual void BaseWrapper::foo()
{
  luabind::object func = _self["foo"];
  /* now put func in coroutine scheduler queue and when appropriate call: 
     luabind::resume_function<void>(func, _self);
  */
}

在卢阿:

class 'Derived' (Base)
  function Derived:__init()
    Base.__init(self, self)     -- the second self is param to BaseWrapper()
  end
  function Derived:foo()
    -- here is the target function
  end
end