[luabind] yield with lua5.1 and luabind0.7
Brought to you by:
arvidn,
daniel_wallin
From: yu j. <mos...@ho...> - 2006-04-30 06:12:56
|
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 |