cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@cocoon.apache.org
Subject [Cocoon Wiki] Updated: RhinoWithContinuations
Date Fri, 09 Jul 2004 14:13:41 GMT
   Date: 2004-07-09T07:13:41
   Editor: DerekLastname <dhohls@csir.co.za>
   Wiki: Cocoon Wiki
   Page: RhinoWithContinuations
   URL: http://wiki.apache.org/cocoon/RhinoWithContinuations

   removed link to ChristopherOliver

Change Log:

------------------------------------------------------------------------------
@@ -1,4 +1,4 @@
-''Transcript of [http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=102781075226697&w=2
an explanation] of ChristopherOliver on cocoon-dev in July 2002.''
+''Transcript of [http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=102781075226697&w=2
an explanation] of Christopher Oliver on cocoon-dev in July 2002.''
 
 Flow scripts in Cocoon are written in !JavaScript. Why !JavaScript ? Because it's a high-level
scripting language that looks a lot like Java, and also because we have a special version
of the Rhino !JavaScript interpreter that has the ability to "capture" the current execution
state of a program.
 
@@ -26,13 +26,13 @@
 You might think the following code is faulty since it apparently would
 overflow the call-stack given a large enough value for 'limit':
 
-{{{
-function g(count, limit) {
-   if (count == limit) {
-      return "done";
-   }
-   return g(count + 1, limit);
-}
+{{{
+function g(count, limit) {
+   if (count == limit) {
+      return "done";
+   }
+   return g(count + 1, limit);
+}
 }}}
 
 In fact, with the "continuations" mode of Rhino this is not the case. The
@@ -70,27 +70,27 @@
 
 You can capture the continuation of a script by simply calling
 
-{{{
-new Continuation()
+{{{
+new Continuation()
 }}}
 
 for example:
 
-{{{
-function someFunction(a, b) {
-    var kont = new Continuation();
-}
+{{{
+function someFunction(a, b) {
+    var kont = new Continuation();
+}
 }}}
 
 The variable {{{kont}}} now represents the execution state of the current
 caller of {{{someFunction}}}. (For those of you who are familiar with Scheme's
 {{{call-with-current-continuation}}}, here is the Rhino equivalent:
 
-{{{
-function call_with_current_continuation(fun) {
-    var kont = new Continuation();
-    return fun(kont);
-}
+{{{
+function call_with_current_continuation(fun) {
+    var kont = new Continuation();
+    return fun(kont);
+}
 }}}
 
 Since {{{kont}}} is a first-class !JavaScript object you can return it,
@@ -104,30 +104,30 @@
 argument to the Continuation object becomes the return value of the function
 in which the Continuation was captured. For example:
 
-{{{
-01  function someFunction()  {
-02     var kont  = new  Continuation();
-03     print("captured: " + kont);
-04     return kont;
-05  }
-06
-07  var k = someFunction();
-08  if (k instanceof Continuation) {
-09     print("k is a continuation");
-10     k(200);
-11  } else {
-12     print("k is now a " + typeof(k));
-13  }
-14  print(k);
+{{{
+01  function someFunction()  {
+02     var kont  = new  Continuation();
+03     print("captured: " + kont);
+04     return kont;
+05  }
+06
+07  var k = someFunction();
+08  if (k instanceof Continuation) {
+09     print("k is a continuation");
+10     k(200);
+11  } else {
+12     print("k is now a " + typeof(k));
+13  }
+14  print(k);
 }}}
 
 Evaluating the above script yields the following output:
 
-{{{
-captured: [object Continuation]
-k is a continuation
-k is now a number
-200
+{{{
+captured: [object Continuation]
+k is a continuation
+k is now a number
+200
 }}}
 
 When the continuation {{{k}}} is invoked on line 10, the program "jumps" back to
@@ -143,16 +143,16 @@
 means to terminate any script immediately. Whenever such a Continuation is
 invoked it simply terminates the interpreter, for example:
 
-{{{
-var suicide = new Continuation();
-
-function foo(suicide) {
-    print("commiting suicide");
-    suicide();
-    print("never reached");
-}
-
-foo(suicide);
+{{{
+var suicide = new Continuation();
+
+function foo(suicide) {
+    print("commiting suicide");
+    suicide();
+    print("never reached");
+}
+
+foo(suicide);
 }}}
 
 == ContinuationException ==
@@ -167,31 +167,31 @@
 is thrown in the context of the Continuation after the Continuation is
 restored. For example:
 
-{{{
-function someFunction() {
-    var k = new Continuation();
-    return k;
-}
-
-try {
-    var k = someFunction();
-    print("k: " + k);
-    if (k instanceof Continuation) {
-        print("k is a continuation");
-        k(new ContinuationException("this is thrown from someFunction"));
-    }
-    print("never reached");
-} catch (e) {
-    print("caught exception: " + e);
-}
+{{{
+function someFunction() {
+    var k = new Continuation();
+    return k;
+}
+
+try {
+    var k = someFunction();
+    print("k: " + k);
+    if (k instanceof Continuation) {
+        print("k is a continuation");
+        k(new ContinuationException("this is thrown from someFunction"));
+    }
+    print("never reached");
+} catch (e) {
+    print("caught exception: " + e);
+}
 }}}
 
 Evaluating the above script yields the following output:
 
-{{{
-k: [object Continuation]
-k is a continuation
-caught exception: this is thrown from someFunction
+{{{
+k: [object Continuation]
+k is a continuation
+caught exception: this is thrown from someFunction
 }}}
 
 === Controlling what gets captured in a Continuation ===
@@ -199,18 +199,18 @@
 In continuations mode, Rhino provides a special extended syntax to allow you
 to control what gets captured in a continuation as follows:
 
-{{{
-catch (break) {
-    // a continuation has been captured - code to handle that
-    // goes here
-}
-
-catch (continue) {
-    // a continuation has been resumed - code to handle that
-    // goes here
-}
-
-
+{{{
+catch (break) {
+    // a continuation has been captured - code to handle that
+    // goes here
+}
+
+catch (continue) {
+    // a continuation has been resumed - code to handle that
+    // goes here
+}
+
+
 }}}
 
 Multiple such "catch" clauses may be present at any scope. All such clauses
@@ -220,33 +220,33 @@
 its connection pool while a Continuation is suspended and recover the
 connection when the Continuation is resumed:
 
-{{{
-var pool = ...;
-
-function someFunction() {
-
-    var conn = pool.getConnection();
-    ...
-
-    catch (break) {
-        conn.close();
-        conn = null;
-    }
-
-    catch (continue) {
-        conn = pool.getConnection();
-    }
-}
+{{{
+var pool = ...;
+
+function someFunction() {
+
+    var conn = pool.getConnection();
+    ...
+
+    catch (break) {
+        conn.close();
+        conn = null;
+    }
+
+    catch (continue) {
+        conn = pool.getConnection();
+    }
+}
 }}}
 
 If you want to execute Flowscript code after calling the view layer but before control leaves
the 
 interpreter, catch(return) will help:
 
-{{{
-catch (return) {
-    // after calling the view layer but before control 
-    // leaves the interpreter
-}
+{{{
+catch (return) {
+    // after calling the view layer but before control 
+    // leaves the interpreter
+}
 }}}
 
 See also [http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=105825410721036&w=2]
@@ -257,54 +257,54 @@
 Continuations. Thus you may save the state of an executing script and
 restore it later. Here is an example:
 
-{{{
-function capture(filename) {
-    var k = new Continuation();
-    serialize(k, filename);
-    java.lang.System.exit(0);
-}
-
-function foo(level) {
-    var now = new java.util.Date();
-    if(level > 5) {
-        print("run the file foo.ser");
-        capture("foo.ser");
-    } else {
-        print("next level");
-        foo(level + 1);
-    }
-    print("restarted("+level+"): " + now)
-}
-
-foo(1);
+{{{
+function capture(filename) {
+    var k = new Continuation();
+    serialize(k, filename);
+    java.lang.System.exit(0);
+}
+
+function foo(level) {
+    var now = new java.util.Date();
+    if(level > 5) {
+        print("run the file foo.ser");
+        capture("foo.ser");
+    } else {
+        print("next level");
+        foo(level + 1);
+    }
+    print("restarted("+level+"): " + now)
+}
+
+foo(1);
 }}}
 
 Evaluating the above script saves a Continuation to the file "foo.ser" and
 prints the following output:
 
-{{{
-next level
-next level
-next level
-next level
-next level
-run the file foo.ser
+{{{
+next level
+next level
+next level
+next level
+next level
+run the file foo.ser
 }}}
 
 The following script deserializes the Continuation and executes it:
 
-{{{
-var k = deserialize("foo.ser");
-k();
+{{{
+var k = deserialize("foo.ser");
+k();
 }}}
 
 Evaluating the second script produces the following output:
 
-{{{
-restarted(6): Mon May 20 19:03:12 PDT 2002
-restarted(5): Mon May 20 19:03:12 PDT 2002
-restarted(4): Mon May 20 19:03:12 PDT 2002
-restarted(3): Mon May 20 19:03:12 PDT 2002
-restarted(2): Mon May 20 19:03:12 PDT 2002
-restarted(1): Mon May 20 19:03:12 PDT 2002
+{{{
+restarted(6): Mon May 20 19:03:12 PDT 2002
+restarted(5): Mon May 20 19:03:12 PDT 2002
+restarted(4): Mon May 20 19:03:12 PDT 2002
+restarted(3): Mon May 20 19:03:12 PDT 2002
+restarted(2): Mon May 20 19:03:12 PDT 2002
+restarted(1): Mon May 20 19:03:12 PDT 2002
 }}}

Mime
View raw message