Compatibility of luajit coroutine
//
// This test shows that Lua and luajit have different processing capabilities in the coroutine,
// Luajit lacks compatibility in processing operations from Lua yield to Lua,
// However, the example included with Coco: cotet. Lua description, because luajit supports Coco,
// The support for the Lua pcall test is enhanced.
//
Bool test_c_lua_coroutine ()
{
//
// Not passed in luajit
//
Luastateincpp * State = new luastateincpp ();
State-> Init (null );
State-> do_string ("A = function () I = coroutine. Yield (1); print (I); coroutine. Yield (2); End ");
Luastateincpp * CO = state-> new_thread ("");
Printf ("start... n ");
Int yieldret = Co-> resume ();
Printf ("n first yield ret = % d", yieldret );
Yieldret = Co-> resume ();
Printf ("N 2nd yield ret = % d", yieldret );
Yieldret = Co-> resume ();
Printf ("n 3rd yield ret = % d", yieldret );
//
// Passed in luajit
//
{
Luastateincpp * State = new luastateincpp ();
State-> Init (null );
State-> do_string ("A = function () I = coroutine. Yield (1); print (I); coroutine. Yield (2); End ");
State-> do_string ("CO = coroutine. Wrap ()");
State-> do_string ("Print ('1: '. CO ())");
State-> do_string ("Print ('2: '. CO ())");
State-> do_string ("Print ('3: '.. (Co () or ''))");
}
Return true;
}
Lua 5.1.2 copyright (c) 1994-2007 lua.org, pUC-Rio
> Require "linker_mod"
Linker_mod Ver: 1.0.0.0 loaded!
> Test_c_lua_coroutine ()
Cannot open: Invalid argumentstart...
First yield ret = 1nil
2nd yield ret = 1
3rd yield ret = 0 cannot open: Invalid argument1: 1
Nil
2: 2
3:
Lua 5.1.3 copyright (c) 1994-2008 lua.org, pUC-Rio
Luajit 1.1.4 copyright (c) 2005-2008 Mike pall, http://luajit.org/
> Require "linker_mod"
Linker_mod Ver: 1.0.0.0 loaded!
> Test_c_lua_coroutine ()
Cannot open: Invalid argumentstart...
Attempt to yield processing SS metamethod/c-call boundary
First yield ret =-1 cannot resume non-susponded coroutine
2nd yield ret =-1 cannot resume non-susponded coroutine
3rd yield ret =-1 cannot open: Invalid argument1: 1
Nil
2: 2
3:
>
To solve this problem, someone wrote a patch on Lua wiki, but this patch has not been fully tested and verified.
Not maintained for a long time.
To put it simply, the root cause of the yield resume problem of luavm is the processing of the context correspondence between the context of the C function and the context of the Lua coroutine.
In fact, the complete solution is to avoid directly switching the coroutine between C and Lua, but to implement all coroutine switching on the Lua side.
Limited to simple function calls.
This avoids many problems.