Luabind and coroutines

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Luabind and coroutines

Alexander Gladysh
Hi, all!

1. I have some troubles with understanding of coroutines part of
Luabind documentation. I think it could benefit from some elaboration,
at least can some more examples be added there?

When should I call luabind::resume_function() and when
luabind::resume() for example?

2. Can I create coroutine in Lua, pass it to C++, and call resume on
it like on coroutine I created from C?

[lua]
local f = function()
  while true do
    coroutine.yield()
    print("!!!")
  end
end

BindCoroutine(coroutine.create(f))
[/lua]

[cpp]
class foo
{
public:
  void BindCoroutine(luabind::object obj)
  {
    if (luabind::type(obj) != LUA_TTHREAD)
    {
      lua_pushstring(obj.interpreter(), "Expected coroutine");
      lua_error(obj.interpreter());
    }
    m_Coroutine = obj;
  }

  void Update()
  {
    m_Coroutine.push(m_Coroutine.interpreter());
    lua_State * pThread = lua_tothread(m_Coroutine.interpreter(), -1);
    lua_pop(m_Coroutine.interpreter(), 1);

    luabind::resume<void>(pThread);
  }

private:
  luabind::object m_Coroutine;
};
[/cpp]

Is this the correct way? Am I missing something?

I'm getting the same assertion on luabind::resume() call at line 264
of call_function.hpp in my actual code. (I can make a minimal
compiling example if needed.)

Thanks in advance,
Alexander.

P.S. Docs say luabind::object::state() to have no arguments, but it
accepts lua_State * in current code.


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind and coroutines

Arvid Norberg

On Feb 27, 2006, at 06:46, Alexander Gladysh wrote:

> Hi, all!
>
> 1. I have some troubles with understanding of coroutines part of
> Luabind documentation. I think it could benefit from some elaboration,
> at least can some more examples be added there?
>
> When should I call luabind::resume_function() and when
> luabind::resume() for example?
>
> 2. Can I create coroutine in Lua, pass it to C++, and call resume on
> it like on coroutine I created from C?
>
> [lua]
> local f = function()
>   while true do
>     coroutine.yield()
>     print("!!!")
>   end
> end
>
> BindCoroutine(coroutine.create(f))
> [/lua]
>
> [cpp]
> class foo
> {
> public:
>   void BindCoroutine(luabind::object obj)
>   {
>     if (luabind::type(obj) != LUA_TTHREAD)
>     {
>       lua_pushstring(obj.interpreter(), "Expected coroutine");
>       lua_error(obj.interpreter());
>     }
>     m_Coroutine = obj;
>   }
>
>   void Update()
>   {
>     m_Coroutine.push(m_Coroutine.interpreter());
>     lua_State * pThread = lua_tothread(m_Coroutine.interpreter(), -1);
>     lua_pop(m_Coroutine.interpreter(), 1);
>
>     luabind::resume<void>(pThread);
>   }
>
> private:
>   luabind::object m_Coroutine;
> };
> [/cpp]
>
> Is this the correct way? Am I missing something?

This looks correct.

> I'm getting the same assertion on luabind::resume() call at line 264
> of call_function.hpp in my actual code. (I can make a minimal
> compiling example if needed.)

If you could make a testcase that fails (preferably by putting it in  
one of the tests or adding a new test to the test/ directory) that  
would be very helpful.

> P.S. Docs say luabind::object::state() to have no arguments, but it
> accepts lua_State * in current code.

With the 0.7 release of luabind, the state() function was replaced by  
interpreter() which doesn't take any arguments.

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind and coroutines

Alexander Gladysh
> > I'm getting the same assertion on luabind::resume() call at line 264
> > of call_function.hpp in my actual code. (I can make a minimal
> > compiling example if needed.)
>
> If you could make a testcase that fails (preferably by putting it in
> one of the tests or adding a new test to the test/ directory) that
> would be very helpful.

Apparently I've found some workaround to this problem -- it no longer
appears in my code, and I can't remember what I've fixed (since it was
about a month ago)...

> > P.S. Docs say luabind::object::state() to have no arguments, but it
> > accepts lua_State * in current code.
>
> With the 0.7 release of luabind, the state() function was replaced by
> interpreter() which doesn't take any arguments.

Very strange -- looks like I meant something else than I wrote :)
Can't remember what it was though...

Sorry for extra noise,
Alexander.


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user