0

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.

Xtapolapocetl
  • 613
  • 5
  • 21

1 Answers1

0

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I understand how yield works - I have a working coroutine scheduler already. These functions immediately return an object (`ScriptRequest`), which handles passing the correct return value back to `luabind::resume_function` when the time comes. This all works fine with regular functions, I'm just trying to bind it to a property instead. – Xtapolapocetl Nov 14 '11 at 19:31
  • @Xtapolapocetl: Oh. Well, I guess it's just a Luabind bug then. Though it's not surprising, since there is no interface for using different policies with the getter and setter methods. – Nicol Bolas Nov 14 '11 at 19:40
  • A bug in Luabind, or simply missing functionality? This seems like something that should be supported. – Xtapolapocetl Nov 14 '11 at 19:45
  • @Xtapolapocetl: It's a bug, since the `.property` takes a `yield` policy without complaint. If it isn't supposed to be able to yield, it should error if you try to. If it is supposed to be able to yield, then it should do so. – Nicol Bolas Nov 14 '11 at 19:57