yield with lua5.1 and luabind0.7

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

yield with lua5.1 and luabind0.7

yu jiang
Hi!
        I notice the test suits of luabind (on vc.net 2003)
        most work fine but test_yield will crash at call_function.hpp line 138
                -- stack_pop pop(L, lua_gettop(L) - top + m_params);
        according to prev mails about lua5.1, it seems some change with the lua's
cooroutine
        Will luabind have the plan to support 5.1, and I hope so!

        and I have a example , may be some one can know what indeed happen,(I post
it at lua newsgroup, but now I think it is better to post here again
        it is a bit long that...


        the source code of c++

extern "C" {
        #include "lua.h"
        #include "lualib.h"
        #include "lauxlib.h"
}

#include <luabind/luabind.hpp>
#include <luabind/error.hpp>
#include <string>
#include <stdio.h>
#include <iostream>
#include <sstream>

using namespace luabind;
using namespace std;

lua_State *L2;
string swait;
void MyYield(const char*szWait)
{
        cout << "thread is waiting for " << szWait << endl;
        swait = szWait;
        lua_yield(L2,0);
}

int luaMyYield(lua_State* LL)
{
        MyYield(lua_tostring(LL,1));
        return 0;
}

void RegisterLua()
{
        lua_register(L2,"MyYield",luaMyYield);
}

void RegisterLuabind()
{
        module(L2)
        [
                def("MyYield", MyYield)
        ];
}

int main()
{
        lua_State* L = lua_open();
        luabind::open(L);
        luaL_openlibs(L);

        L2 = lua_newthread (L);

        RegisterLuabind(); // ******************************
        //RegisterLua(); // ******************************

        int error = luaL_dofile(L2,"hello.lua");
        if (error != 0)
        {
                if(lua_isstring(L2, -1))
                {
                        const char* err = lua_tostring(L2, -1);
                        cout << err << endl;
                }
                return -1;
        }
        lua_getglobal(L2, "mythread");
        lua_resume(L2,0);

        while(1)
        {
                cout << "please input ... " << endl;
                string s ;
                cin >> s;
                if (s == swait)
                {
                        cout << "in main input " << s << endl;
                        lua_resume(L2,0);
                        if (s == "quit")
                                break;
                }
                else
                {
                        cout << "not match input" << endl;
                }
        }
}

        and the hello.lua file

function mythread()
        print "need yield"
        MyYield("yield")
        print "need quit"
        MyYield("quit")
end


        it works well as:

        need yield
        thread is waiting for yield
        please input ...
        yield  // keyboard input
        in main input yield
        need quit
        thread is waiting for quit
        please input...
        quit   // keyboard input

        all are ok!
        but if  comment RegisterLuabind uncomment RegisterLua

        //RegisterLuabind(); // ******************************
        RegisterLua(); // ******************************
       
        need yield
        thread is waiting for yield
        //skip the pre input...
        need quit
        thread is waiting for quit
        please input...
        quit   // keyboard input
        it crashed

        the interesting thing is the lua manual metion:

lua_yield
          int lua_yield  (lua_State *L, int nresults);

Yields a coroutine.

This function should only be called as the return expression of a C
function, as follows:

       return lua_yield (L, nresults);

        so modify the code like

//void MyYield(const char*szWait)
//{
// cout << "thread is waiting for " << szWait << endl;
// swait = szWait;
// lua_yield(L2,0);
//}

//int luaMyYield(lua_State* LL)
//{
// MyYield(lua_tostring(LL,1));
// return 0;
//}

int MyYield(const char*szWait)
{
        cout << "thread is waiting for " << szWait << endl;
        swait = szWait;
        return lua_yield(L2,0);
}

int luaMyYield(lua_State* LL)
{
        return MyYield(lua_tostring(LL,1));
}

        but after do it , the lua works ok but luabind crashed !

        but I thinks there is just a little different between lua_yield from
lua5.02 to 5.1, so I hope the author can give a new version of luabind, It
will not cost your too much time but solve too much trouble around us !
       
mos

_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger:  http://messenger.msn.com/cn 



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: yield with lua5.1 and luabind0.7

Evan Wies
thanks for the groundwork yu jiang.

to fix it, apply this change to src/pcall.cpp:

        int resume_impl(lua_State *L, int nargs, int)
        {
#if LUA_VERSION_NUM >= 501
        // Lua 5.1 added  LUA_YIELD as a possible return value,
        // this was causing crashes, because the caller expects 0 on success.
        int res = lua_resume(L, nargs);
        return (res == LUA_YIELD) ? 0 : res;
#else
                return lua_resume(L, nargs);
#endif // LUA_VERSION_NUM >= 501
        }

this will be in CVS soon.

-Evan



yu jiang wrote:

> Hi!
>     I notice the test suits of luabind (on vc.net 2003)
>     most work fine but test_yield will crash at call_function.hpp line
> 138         -- stack_pop pop(L, lua_gettop(L) - top + m_params);
>     according to prev mails about lua5.1, it seems some change with the
> lua's cooroutine
>     Will luabind have the plan to support 5.1, and I hope so!
>
>     and I have a example , may be some one can know what indeed
> happen,(I post it at lua newsgroup, but now I think it is better to post
> here again     it is a bit long that...
>
>
>     the source code of c++
>
> extern "C" {
>     #include "lua.h"
>     #include "lualib.h"
>     #include "lauxlib.h"
> }
>
> #include <luabind/luabind.hpp>
> #include <luabind/error.hpp>
> #include <string>
> #include <stdio.h>
> #include <iostream>
> #include <sstream>
>
> using namespace luabind;
> using namespace std;
>
> lua_State *L2;
> string swait;
> void MyYield(const char*szWait)
> {
>     cout << "thread is waiting for " << szWait << endl;
>     swait = szWait;
>     lua_yield(L2,0);
> }
>
> int luaMyYield(lua_State* LL)
> {
>     MyYield(lua_tostring(LL,1));
>     return 0;
> }
>
> void RegisterLua()
> {
>     lua_register(L2,"MyYield",luaMyYield);
> }
>
> void RegisterLuabind()
> {
>     module(L2)
>     [
>         def("MyYield", MyYield)
>     ];
> }
>
> int main()
> {
>     lua_State* L = lua_open();
>     luabind::open(L);
>     luaL_openlibs(L);
>
>     L2 = lua_newthread (L);
>
>     RegisterLuabind();    // ******************************
>     //RegisterLua();        // ******************************
>
>     int error = luaL_dofile(L2,"hello.lua");
>     if (error != 0)
>     {
>         if(lua_isstring(L2, -1))
>         {
>             const char* err = lua_tostring(L2, -1);
>             cout << err << endl;
>         }
>         return -1;
>     }
>     lua_getglobal(L2, "mythread");
>     lua_resume(L2,0);
>
>     while(1)
>     {
>         cout << "please input ... " << endl;
>         string s ;
>         cin >> s;
>         if (s == swait)
>         {
>             cout << "in main input " << s << endl;
>             lua_resume(L2,0);
>             if (s == "quit")
>                 break;
>         }
>         else
>         {
>             cout << "not match input" << endl;
>         }
>     }
> }
>
>     and the hello.lua file
>
> function mythread()
>     print "need yield"
>     MyYield("yield")
>     print "need quit"
>     MyYield("quit")
> end
>
>
>     it works well as:
>     need yield
>     thread is waiting for yield
>     please input ...     yield  // keyboard input
>     in main input yield
>     need quit
>     thread is waiting for quit
>     please input...
>     quit   // keyboard input
>
>     all are ok!
>     but if  comment RegisterLuabind uncomment RegisterLua
>
>     //RegisterLuabind();    // ******************************
>     RegisterLua();        // ******************************
>    
>     need yield
>     thread is waiting for yield  
>     //skip the pre input...
>     need quit
>     thread is waiting for quit
>     please input...
>     quit   // keyboard input
>     it crashed    
>
>     the interesting thing is the lua manual metion:
>
> lua_yield
>          int lua_yield  (lua_State *L, int nresults);
>
> Yields a coroutine.
> This function should only be called as the return expression of a C
> function, as follows:
>       return lua_yield (L, nresults);
>
>     so modify the code like
> //void MyYield(const char*szWait)
> //{
> //    cout << "thread is waiting for " << szWait << endl;
> //    swait = szWait;
> //    lua_yield(L2,0);
> //}
>
> //int luaMyYield(lua_State* LL)
> //{
> //    MyYield(lua_tostring(LL,1));
> //    return 0;
> //}
>
> int MyYield(const char*szWait)
> {
>     cout << "thread is waiting for " << szWait << endl;
>     swait = szWait;
>     return lua_yield(L2,0);
> }
>
> int luaMyYield(lua_State* LL)
> {
>     return MyYield(lua_tostring(LL,1));
> }
>
>     but after do it , the lua works ok but luabind crashed !
>     but I thinks there is just a little different between lua_yield from
> lua5.02 to 5.1, so I hope the author can give a new version of luabind,
> It will not cost your too much time but solve too much trouble around us !
>    
> mos
>
> _________________________________________________________________
> 与联机的朋友进行交流,请使用 MSN Messenger:  http://messenger.msn.com/cn 
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user