title: Rhino: Ability to pause, save state and resume javascript
body: <p>I'm using Rhino to interpret javascripts by Java. My use case requires these javascripts making (multiple) service calls (RESTful/Webservices/HTTP GET/POST). Some of these service calls are async in nature (with 24 Hours of SLA).</p>
  
  <p>I want the ability to pause the execution of my script in such cases, serialize the state (saving it on, say S3 with key passed to the payload of async service which the service returns back on callback) and resume the execution when I get the result back from service.</p>
  
  <p>The challenge I'm facing is that ContinuationPending (extending RuntimeException) isn't serializable (because Context is not).</p>
  
  <p>Question: Is there any other way to store the state of script and get it back from its serialized form?</p>
  
  <p>Javascript:</p>
  
  <pre><code>function invokeFooService(arg) {
      return foo.bar.Helper.invokeFooServiceAsync(arg);
  }
  
  function main() {
      // Main JS function
      ..
      var response = invokeFooService(arg);
      if (..) {
          ..
      }
  }
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>package foo.bar;
  
  public class Helper {
  
      public static final void invokeFooServiceAsync(String arg) {
          Context cx = getContext();
          ContinuationPending pending = cx.captureContinuation();
          // At this point the script is paused
          // Serialize the state of script
          invokeFooService(arg, key);
      }
  
      public static final void returnResponse(FooResponse response, String key) {
          // De serialize the state of script
          ContinuationPending pending = ..
          Context cx = getContext();
          cx.resumeContinuation(pending.getContinuation(), getScope(), response);
          // Script is resumed
      }
  }
  </code></pre>