case INIT: {
return init();
}
case CREATE: {
final LuaValue func = args.checkfunction(1);
return new LuaThread(func, LuaThread.getGlobals() );
}
case RESUME: {
final LuaThread t = args.checkthread(1);
return t.resume( args.subargs(2) );
}
case RUNNING: {
final LuaThread r = LuaThread.getRunning();
return LuaThread.isMainThread(r)? NIL: r;
}
case STATUS: {
return valueOf( args.checkthread(1).getStatus() );
}
case YIELD: {
final LuaThread r = LuaThread.getRunning();
if ( LuaThread.isMainThread( r ) )
error("main thread can't yield");
return r.yield( args );
}
case WRAP: {
final LuaValue func = args.checkfunction(1);
final LuaThread thread = new LuaThread(func, func.getfenv());
CoroutineLib cl = new CoroutineLib();
cl.setfenv(thread);
cl.name = "wrapped";
cl.opcode = WRAPPED;
return cl;
}
case WRAPPED: {
final LuaThread t = (LuaThread) env;
final Varargs result = t.resume( args );
if ( result.arg1().toboolean() ) {
return result.subargs(2);
} else {
error( result.arg(2).tojstring() );
}