public class Continuable extends Object implements Serializable
| Modifier and Type | Field and Description |
|---|---|
static List<Class> |
categories |
static StackTraceElement |
SEPARATOR_STACK_ELEMENT
The artificial
StackTraceElement that appears in the stack trace when the CPS library fixes up
the stack trace. |
| Constructor and Description |
|---|
Continuable(Block block)
Creates a
Continuable that executes the block of code in a fresh empty environment. |
Continuable(Block block,
Env e)
Creates a
Continuable that executes the block in the specified environment. |
Continuable(Continuable src) |
Continuable(Next n) |
Continuable(groovy.lang.Script cpsTransformedScript)
|
Continuable(groovy.lang.Script cpsTransformedScript,
Env env)
Takes a
Script compiled from CPS-transforming GroovyShell and
wraps that into a Continuable, in the context of the given Env. |
Continuable(groovy.lang.Script cpsTransformedScript,
Env env,
Continuation k)
|
| Modifier and Type | Method and Description |
|---|---|
Continuable |
fork()
Creates a shallow copy of
Continuable. |
List<StackTraceElement> |
getStackTrace()
Returns the stack trace in the CPS-transformed code that indicates where this
Continuable will resume from. |
boolean |
isResumable()
Checks if this
Continuable is pointing at the end of the program which cannot
be resumed. |
void |
jump(Continuable c)
Ignore whatever that we've been doing, and jumps the execution to the given continuation.
|
void |
prepend(Continuable c,
com.google.common.base.Function<Outcome,Outcome> mapper)
Set aside what we are executing, and instead resume the next execution from the point
the given 'Continuable' points to.
|
void |
printStackTrace(PrintWriter s)
Prints the stack trace into the given writer, much like
Throwable.printStackTrace(PrintWriter) |
Object |
run(Object arg)
Deprecated.
|
Outcome |
run0(Outcome cn)
Deprecated.
|
Outcome |
run0(Outcome cn,
List<Class> categories)
Resumes this program by either returning the value from
suspend(Object) or
throwing an exception |
Object |
runByThrow(Throwable arg)
Deprecated.
|
void |
superInterrupt(Throwable t)
Sets a super-interrupt.
|
static Object |
suspend(Object v)
Deprecated.
|
static Object |
suspend(String methodName,
Object v) |
public static final StackTraceElement SEPARATOR_STACK_ELEMENT
StackTraceElement that appears in the stack trace when the CPS library fixes up
the stack trace. This separator separates the regular call stack that tracks the actual call stack
JVM executes and the synthesized CPS call stack that CPS-transformed program is logically executing.public Continuable(Continuable src)
public Continuable(Next n)
public Continuable(Block block)
Continuable that executes the block of code in a fresh empty environment.public Continuable(Block block, Env e)
Continuable that executes the block in the specified environment.public Continuable(groovy.lang.Script cpsTransformedScript)
public Continuable(groovy.lang.Script cpsTransformedScript,
Env env)
Script compiled from CPS-transforming GroovyShell and
wraps that into a Continuable, in the context of the given Env.
The added 'env' parameter can be used to control the execution flow in case
of exceptions, and/or providing custom Invokerpublic Continuable(groovy.lang.Script cpsTransformedScript,
Env env,
Continuation k)
Script compiled from CPS-transforming GroovyShell and
wraps that into a Continuable.
The added 'k' parameter can be used to pass the control to somewhere else
when the script has finished executing.public Continuable fork()
Continuable. The copy shares
all the local variables of the original Continuable, and
point to the exact same point of the program.public void printStackTrace(PrintWriter s)
Throwable.printStackTrace(PrintWriter)@Deprecated public Object run(Object arg) throws InvocationTargetException
InvocationTargetException - if the program threw an exception that it didn't handle by itself.@Deprecated public Object runByThrow(Throwable arg) throws InvocationTargetException
InvocationTargetException@Deprecated public Outcome run0(Outcome cn)
public Outcome run0(Outcome cn, List<Class> categories)
suspend(Object) or
throwing an exceptionpublic void superInterrupt(Throwable t)
A super interrupt works like Thread.interrupt() that throws
InterruptedException. It lets other threads interrupt the execution
of Continuable by making it throw an exception.
Unlike InterruptedException, which only gets thrown in specific
known locations, such as Object.wait(), this "super interruption"
gets thrown at any point in the execution, even during while(true) ; kind of loop.
The
public boolean isResumable()
Continuable is pointing at the end of the program which cannot
be resumed.@Deprecated public static Object suspend(Object v)
When this method is called, the control goes back to
the caller of run(Object), which returns with the argument given to this method.
When the continuable is resumed via run(Object) later, the argument to the run method
will become the return value from this method to the CPS-transformed program.
public List<StackTraceElement> getStackTrace()
Continuable will resume from.
If this object represents a yet-started program, an empty list will be returned.public void jump(Continuable c)
Aside from the obvious use case of completely overwriting the state of Continuable,
more interesting case is
Sets aside the current continuation aside, schedule the evaluation of the given block in the given environment,
then when done pass the result to the given Continuation.
A common pattern is for that Continuation to then resume executing the current execution that was set
aside.
Continuable c = ...;
final Continuable pausePoint = new Continuable(c); // set aside what we were doing
c.jump(bodyOfNewThread,env,new Continuation() {
public Next receive(Object o) {
// o is the result of evaluating bodyOfNewThread (the failure will go to the handler specified by 'env')
doSomethingWith(c);
if (...) {// maybe you want to yield this value, then resume from the pause point?
return Next.yield0(new Outcome(o,null),pausePoint);
}
if (...) {// maybe you want to keep going by immediately resuming from the pause point with 'o'
return Next.go0(new Outcome(o,null),pausePoint);
}
// maybe you want to halt the execution by returning
return Next.terminate0(new Outcome(o,null));
}
});
c.run(...); // this will start executing from 'bodyOfNewThread'
public void prepend(Continuable c, com.google.common.base.Function<Outcome,Outcome> mapper)
Copyright © 2011–2019. All rights reserved.