Return-Path: Delivered-To: apmail-cocoon-docs-archive@www.apache.org Received: (qmail 47337 invoked from network); 9 Jul 2004 14:14:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 9 Jul 2004 14:14:36 -0000 Received: (qmail 62263 invoked by uid 500); 9 Jul 2004 14:13:48 -0000 Delivered-To: apmail-cocoon-docs-archive@cocoon.apache.org Received: (qmail 62142 invoked by uid 500); 9 Jul 2004 14:13:47 -0000 Mailing-List: contact docs-help@cocoon.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: docs@cocoon.apache.org Delivered-To: mailing list docs@cocoon.apache.org Received: (qmail 62034 invoked by uid 99); 9 Jul 2004 14:13:46 -0000 X-ASF-Spam-Status: No, hits=0.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Fri, 09 Jul 2004 07:13:42 -0700 Received: (qmail 46787 invoked from network); 9 Jul 2004 14:13:41 -0000 Received: from localhost.hyperreal.org (HELO minotaur.apache.org) (127.0.0.1) by localhost.hyperreal.org with SMTP; 9 Jul 2004 14:13:41 -0000 Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: docs@cocoon.apache.org To: docs@cocoon.apache.org Subject: =?iso-8859-1?q?=5BCocoon_Wiki=5D_Updated=3A__RhinoWithContinuations?= Date: Fri, 09 Jul 2004 14:13:41 -0000 Message-ID: <20040709141341.46754.51690@minotaur.apache.org> X-Spam-Rating: localhost.hyperreal.org 1.6.2 0/1000/N X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Date: 2004-07-09T07:13:41 Editor: DerekLastname 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=3Dxml-cocoon-dev&m=3D1027= 81075226697&w=3D2 an explanation] of ChristopherOliver on cocoon-dev in Jul= y 2002.'' +''Transcript of [http://marc.theaimsgroup.com/?l=3Dxml-cocoon-dev&m=3D1027= 81075226697&w=3D2 an explanation] of Christopher Oliver on cocoon-dev in Ju= ly 2002.'' = Flow scripts in Cocoon are written in !JavaScript. Why !JavaScript ? Becau= se it's a high-level scripting language that looks a lot like Java, and als= o because we have a special version of the Rhino !JavaScript interpreter th= at 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 =3D=3D limit) { - return "done"; - } - return g(count + 1, limit); -} +{{{ +function g(count, limit) { + if (count =3D=3D 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 =3D new Continuation(); -} +{{{ +function someFunction(a, b) { + var kont =3D new Continuation(); +} }}} = The variable {{{kont}}} now represents the execution state of the current caller of {{{someFunction}}}. (For those of you who are familiar with Sche= me's {{{call-with-current-continuation}}}, here is the Rhino equivalent: = -{{{ -function call_with_current_continuation(fun) { - var kont =3D new Continuation(); - return fun(kont); -} +{{{ +function call_with_current_continuation(fun) { + var kont =3D 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 functi= on in which the Continuation was captured. For example: = -{{{ -01 function someFunction() { -02 var kont =3D new Continuation(); -03 print("captured: " + kont); -04 return kont; -05 } -06 -07 var k =3D 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 =3D new Continuation(); +03 print("captured: " + kont); +04 return kont; +05 } +06 +07 var k =3D 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" b= ack 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 =3D new Continuation(); - -function foo(suicide) { - print("commiting suicide"); - suicide(); - print("never reached"); -} - -foo(suicide); +{{{ +var suicide =3D new Continuation(); + +function foo(suicide) { + print("commiting suicide"); + suicide(); + print("never reached"); +} + +foo(suicide); }}} = =3D=3D ContinuationException =3D=3D @@ -167,31 +167,31 @@ is thrown in the context of the Continuation after the Continuation is restored. For example: = -{{{ -function someFunction() { - var k =3D new Continuation(); - return k; -} - -try { - var k =3D 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 =3D new Continuation(); + return k; +} + +try { + var k =3D 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 }}} = =3D=3D=3D Controlling what gets captured in a Continuation =3D=3D=3D @@ -199,18 +199,18 @@ In continuations mode, Rhino provides a special extended syntax to allow y= ou 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 =3D ...; - -function someFunction() { - - var conn =3D pool.getConnection(); - ... - - catch (break) { - conn.close(); - conn =3D null; - } - - catch (continue) { - conn =3D pool.getConnection(); - } -} +{{{ +var pool =3D ...; + +function someFunction() { + + var conn =3D pool.getConnection(); + ... + + catch (break) { + conn.close(); + conn =3D null; + } + + catch (continue) { + conn =3D pool.getConnection(); + } +} }}} = If you want to execute Flowscript code after calling the view layer but be= fore 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=3Dxml-cocoon-dev&m=3D10582541072= 1036&w=3D2] @@ -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 =3D new Continuation(); - serialize(k, filename); - java.lang.System.exit(0); -} - -function foo(level) { - var now =3D 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 =3D new Continuation(); + serialize(k, filename); + java.lang.System.exit(0); +} + +function foo(level) { + var now =3D 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 =3D deserialize("foo.ser"); -k(); +{{{ +var k =3D 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 }}}