RubyContinuation.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Copyright (C) 2007 Ola Bini <ola@ologix.com>
  15.  *
  16.  * Alternatively, the contents of this file may be used under the terms of
  17.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  18.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  19.  * in which case the provisions of the GPL or the LGPL are applicable instead
  20.  * of those above. If you wish to allow use of your version of this file only
  21.  * under the terms of either the GPL or the LGPL, and not to allow others to
  22.  * use your version of this file under the terms of the EPL, indicate your
  23.  * decision by deleting the provisions above and replace them with the notice
  24.  * and other provisions required by the GPL or the LGPL. If you do not delete
  25.  * the provisions above, a recipient may use your version of this file under
  26.  * the terms of any one of the EPL, the GPL or the LGPL.
  27.  ***** END LICENSE BLOCK *****/
  28. package org.jruby;

  29. import org.jruby.anno.JRubyMethod;
  30. import org.jruby.anno.JRubyClass;
  31. import org.jruby.exceptions.Unrescuable;
  32. import org.jruby.runtime.Block;
  33. import org.jruby.runtime.ClassIndex;
  34. import org.jruby.runtime.ThreadContext;
  35. import org.jruby.runtime.builtin.IRubyObject;

  36. /**
  37.  * Placeholder until/if we can support this
  38.  *
  39.  * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
  40.  */
  41. @JRubyClass(name="Continuation")
  42. public class RubyContinuation extends RubyObject {
  43.     public static class Continuation extends Error implements Unrescuable {
  44.         public Continuation() {tag = null;}
  45.         public Continuation(IRubyObject tag) {
  46.             this.tag = tag;
  47.         }
  48.         public IRubyObject[] args = IRubyObject.NULL_ARRAY;
  49.         public final IRubyObject tag;
  50.        
  51.         @Override
  52.         public synchronized Throwable fillInStackTrace() {
  53.             return this;
  54.         }
  55.     }

  56.     private final Continuation continuation;
  57.     private boolean disabled;
  58.    
  59.     public static void createContinuation(Ruby runtime) {
  60.         RubyClass cContinuation = runtime.defineClass("Continuation",runtime.getObject(),runtime.getObject().getAllocator());

  61.         cContinuation.setClassIndex(ClassIndex.CONTINUATION);
  62.         cContinuation.setReifiedClass(RubyContinuation.class);
  63.        
  64.         cContinuation.defineAnnotatedMethods(RubyContinuation.class);
  65.         cContinuation.getSingletonClass().undefineMethod("new");
  66.        
  67.         runtime.setContinuation(cContinuation);
  68.     }

  69.     public RubyContinuation(Ruby runtime) {
  70.         super(runtime, runtime.getContinuation());
  71.         this.continuation = new Continuation();
  72.     }

  73.     /**
  74.      * A RubyContinuation used for catch/throw, which have a tag associated
  75.      *
  76.      * @param runtime Current JRuby runtime
  77.      * @param tag The tag to use
  78.      */
  79.     public RubyContinuation(Ruby runtime, IRubyObject tag) {
  80.         super(runtime, runtime.getContinuation());
  81.         this.continuation = new Continuation(tag);
  82.     }

  83.     public Continuation getContinuation() {
  84.         return continuation;
  85.     }

  86.     @JRubyMethod(name = {"call", "[]"}, rest = true)
  87.     public IRubyObject call(ThreadContext context, IRubyObject[] args) {
  88.         if (disabled) {
  89.             RubyKernel.raise(context, context.runtime.getThreadError(),
  90.                     new IRubyObject[]{context.runtime.newString("continuations can not be called from outside their scope")},
  91.                     Block.NULL_BLOCK);
  92.         }
  93.         continuation.args = args;
  94.         throw continuation;
  95.     }

  96.     public IRubyObject enter(ThreadContext context, IRubyObject yielded, Block block) {
  97.         try {
  98.             return block.yield(context, yielded);
  99.         } catch (Continuation c) {
  100.             if (c == continuation) {
  101.                 if (continuation.args.length == 0) {
  102.                     return context.runtime.getNil();
  103.                 } else if (continuation.args.length == 1) {
  104.                     return continuation.args[0];
  105.                 } else {
  106.                     return context.runtime.newArrayNoCopy(continuation.args);
  107.                 }
  108.             } else {
  109.                 throw c;
  110.             }
  111.         } finally {
  112.             disabled = true;
  113.         }
  114.     }
  115. }// RubyContinuation