implement corouting.wrap(), fix bugs in coroutine.status(), fix StackState.insert()
This commit is contained in:
@@ -16,6 +16,12 @@ import lua.value.LValue;
|
||||
|
||||
public class LuaJTest extends TestCase {
|
||||
|
||||
/*
|
||||
public void testCoroutines() throws IOException, InterruptedException {
|
||||
runTest( "coroutines" );
|
||||
}
|
||||
|
||||
/*/
|
||||
public void testTest1() throws IOException, InterruptedException {
|
||||
runTest( "test1" );
|
||||
}
|
||||
@@ -103,7 +109,8 @@ public class LuaJTest extends TestCase {
|
||||
public void testUpvalues2() throws IOException, InterruptedException {
|
||||
runTest( "upvalues2" );
|
||||
}
|
||||
|
||||
//*/
|
||||
|
||||
private void runTest( String testName ) throws IOException, InterruptedException {
|
||||
|
||||
// Reset the _G table just in case some test mucks with it
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
function printrunning()
|
||||
if coroutine.running() == nil then
|
||||
print("running is nil");
|
||||
else
|
||||
print("running is not nil")
|
||||
end
|
||||
end
|
||||
|
||||
function foo (a)
|
||||
print("foo", a)
|
||||
return coroutine.yield(2*a)
|
||||
@@ -9,10 +17,56 @@ co = coroutine.create(function (a,b)
|
||||
print("co-body", r)
|
||||
local r, s = coroutine.yield(a+b, a-b)
|
||||
print("co-body", r, s)
|
||||
|
||||
printrunning()
|
||||
print("co.status.inside",coroutine.status(co));
|
||||
local co2 = coroutine.create(function()
|
||||
print("co.status.inside2",coroutine.status(co));
|
||||
end)
|
||||
print("co.status.inside",coroutine.status(co));
|
||||
coroutine.resume(co2);
|
||||
|
||||
return b, "end"
|
||||
end)
|
||||
|
||||
print("main", coroutine.resume(co, 1, 10))
|
||||
print("main", coroutine.resume(co, "r"))
|
||||
print("main", coroutine.resume(co, "x", "y"))
|
||||
print("main", coroutine.resume(co, "x", "y"))
|
||||
|
||||
function exercise()
|
||||
printrunning()
|
||||
print("co.status",coroutine.status(co));
|
||||
print("main", coroutine.resume(co, 1, 10))
|
||||
print("co.status",coroutine.status(co));
|
||||
print("main", coroutine.resume(co, "r"))
|
||||
print("co.status",coroutine.status(co));
|
||||
print("main", coroutine.resume(co, "x", "y"))
|
||||
print("co.status",coroutine.status(co));
|
||||
print("main", coroutine.resume(co, "x", "y"))
|
||||
print("co.status",coroutine.status(co));
|
||||
end
|
||||
|
||||
exercise();
|
||||
|
||||
co = coroutine.create(function (a,b)
|
||||
print("co-body", a, b)
|
||||
local statis,r = pcall( foo, a+1 )
|
||||
print("co-body", status,r)
|
||||
local r, s = coroutine.yield(a+b, a-b)
|
||||
print("co-body", r, s)
|
||||
return b, "end"
|
||||
end)
|
||||
|
||||
exercise();
|
||||
|
||||
-- wrap test
|
||||
local g = coroutine.wrap(function (a,b)
|
||||
print("co-body", a, b)
|
||||
local r = foo(a+1)
|
||||
print("co-body", r)
|
||||
local r, s = coroutine.yield(a+b, a-b)
|
||||
print("co-body", r, s)
|
||||
return b, "end"
|
||||
end )
|
||||
|
||||
print("g", g(1, 10))
|
||||
print("g", g("r"))
|
||||
print("g", g("x", "y"))
|
||||
print("g", pcall( g, "x", "y" ))
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user