1

I'm trying to use continuations with Rhino but I find mixed instructions as to how to do it. I want to create and use continuations in JS proper.

https://developer.mozilla.org/en-US/docs/New_in_Rhino_1.7R2#Java_API.C2.A0for_Continuations

shows how to use them from Java, in the form of handling exceptions (I think that's just the wrong way to look at it).

http://wiki.apache.org/cocoon/RhinoWithContinuations

shows a now deprecated way of using them - it's not allowed to explicitly create continuations anymore.

Can anyone clarify this? Can I now create JS continuations with Rhino only via Java side manipulation using the 4 methods in the 1.7R2 release notes?

moshewe
  • 127
  • 1
  • 9
  • 1
    Here's a question and answer I wrote a while ago. I'm not sure whether this uses features that fall under what you now reference as deprecated, but it worked very well for me. If that's the case, it might still be worth using the old version unless it was deprecated for a good reason. http://stackoverflow.com/questions/8616512/interpreting-javascript-in-java-with-rhino-pausing-resuming-scripts – Josh1billion Apr 06 '15 at 03:54
  • Thanks Josh! I've already stumbled upon your blog post a while back and it really helped me through! – moshewe Apr 07 '15 at 04:44

1 Answers1

2

It's been a while, but I worked it out so better write it down.

Continuation handling in Rhino is Java-side only - you go into JS by running a function with Continuations support, and get back to Java from Javascript code when you throw a ContinuationPending exception or call Java code that throws it. You return to the catch in the Java code.

This piece of code starts executing a function with Continuations support - goes into Javascript.

try {
    openGlobalContext();
    _globalContext.callFunctionWithContinuations(_func, _scope,
        new Object[0]);
} catch (ContinuationPending pending) {
    _cont = pending;
} finally {
    closeGlobalContext();
}

This piece of Java code, when called from the interpreted Javascript, "goes out of" the Javascript code to where the continuation exception was thrown. That is, SomeMoreCode() will run.

public void createContinuation(){
    try {
        openGlobalContext();
        ContinuationPending pending =
            _globalContext.captureContinuation();
        throw pending;
    } finally {
        closeGlobalContext();
    }
    SomeMoreCode();
}
moshewe
  • 127
  • 1
  • 9