[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lua_pcallk() in lua5.2, attempt to yield from outside a coroutine
- From: GaoXianchao <gxcmaillist@...>
- Date: Sun, 26 Aug 2012 07:50:58 +0800
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;
}