Search code examples
luacoroutineluabind

Can Luabind property getters and setters yield?


Is it possible to create a Luabind property with getters and setters that yield while they wait for the query to be performed in a different thread? The following syntax compiles but doesn't seem to work:

luabind::class_<Foo>("Foo")
  .property("bar", &Foo::getBar, &Foo::setBar, luabind::yield)

Wrapping the object on the Lua side and adding property wrappers around regular functions is not a good option, as I need to define these properties on base classes and this would require much duplication of wrapper code for each derived class.


Solution

  • The following syntax compiles but doesn't seem to work:

    Of course it doesn't work; luabind::yield solves a different problem. yield tells the system to yield after the function completes, not before, and certainly not in the middle of it.

    You can't yield in the middle of C/C++ functions. Lua 5.2 adds the ability to set a "resume" function, but even then, there is significant danger in yielding within C++ code, since Lua generally won't clean up the stack.

    What you want to do is yield before calling the function. It would be the equivalent of this Lua code:

    function myGet(...)
      local tester = StartAsyncAction(...);
      while(~tester:IsFinished()) do
        coroutine.yield();
      end
      return tester:Get(...);
    end
    

    You cannot really mimic that in C/C++; not with Lua 5.2. And Luabind doesn't fully support the new 5.2 features.