lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It seems that lua_pcallk() can not be used in main thread of a lua_State?

 lua_resume() works:
 lua_resume() -> dotest() -> lua_mytest() -> lua_yield()

 lua_pcallk() don't works:
 lua_pcallk() -> dotest() -> lua_mytest() -> lua_yield()
error message : attempt to yield from outside a coroutine

test code:


==lua script test.txt==
function dotest()
 local a = mytest()
 error("err from lua a="..a)
end


C++ code:

static int lua_mytest(lua_State* ls)
{
	return lua_yield(ls, 0);
}

CluatestDlg::OnInitDialog()
{
 ......
 l = luaL_newstate();
 luaL_openlibs(l);
 lua_register(l, "mytest", lua_mytest);
}


static int lua_mytest_con_func(lua_State* ls)
{
 return 0;
}


void CluatestDlg::OnBnClickedButton3()
{
 if(luaL_dofile(l, "test.txt")) return;
 lua_getglobal(l, "dotest");


 int ret = lua_pcallk(l, 0, 0, 0, 0, lua_mytest_con_func);
 if(ret != 0)
 {
 const char* err = luaL_checkstring(l, -1);
 //err : attempt to yield from outside a coroutine
 TRACE(err);
 }
}


//lua_resume works fine
void CluatestDlg::OnBnClickedButton1()
{
 if(luaL_dofile(l, "test.txt")) return;
    lua_getglobal(l, "dotest");

 int ret = lua_resume(l, NULL, 0);
 if((ret!=LUA_OK) && (ret!=LUA_YIELD))
 {
 const char* err = luaL_checkstring(l, -1);
 TRACE(err);
 return;
 }

 int b = ret + 1;
}