From 1cf0bd8a05db10a61c4ca888b7fa57e79064df1f Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Tue, 9 Jul 2002 17:49:16 +0000 Subject: [PATCH] Examples now uses try {} finally { Context.exit(); } to release Context even in case of errors --- js/rhino/docs/tutorial.html | 109 +++++++++++++++++------------ js/rhino/examples/CounterTest.java | 40 ++++++----- js/rhino/examples/RunScript.java | 53 +++++++------- js/rhino/examples/RunScript2.java | 36 +++++----- js/rhino/examples/RunScript3.java | 68 ++++++++++-------- js/rhino/examples/RunScript4.java | 48 +++++++------ 6 files changed, 198 insertions(+), 156 deletions(-) diff --git a/js/rhino/docs/tutorial.html b/js/rhino/docs/tutorial.html index 26d79199df28..89fc08c847df 100644 --- a/js/rhino/docs/tutorial.html +++ b/js/rhino/docs/tutorial.html @@ -139,10 +139,14 @@ that we use in later calls.

Collecting the arguments

This code is standard Java and not specific to Rhino. It just collects all the arguments and concatenates them together. -

String s = ""; -
for (int i=0; i < args.length; i++) -
    s += args[i];
- +
+
+String s = "";
+for (int i=0; i < args.length; i++) {
+    s += args[i];
+}
+
+


Evaluating a script

The code

@@ -166,11 +170,15 @@ method converts any JavaScript value to a string.

Exit the Context

The code

-
Context.exit();
+
+} finally {
+    Context.exit();
+}
+
exits the Context. This removes the association between the Context and the current thread and is an essential cleanup action. There should be -a call to exit for every call to enter. +a call to exit for every call to enter. To make sure that it is called even if an exception is thrown, it is put into the finally block corresponding to the try block starting after Context.enter().
    Expose Java APIs @@ -219,40 +227,45 @@ is illustrated in the $ java RunScript3 'x = 7' -
x = 7 -
f is undefined or not a function. -
$ java RunScript3 'function f(a) { return -a; }' -
x is not defined. -
f('my args') = my arg -
 
+
+
+$ java RunScript3 'x = 7'
+x = 7
+f is undefined or not a function.
+$ java RunScript3 'function f(a) { return a; }'
+x is not defined.
+f('my args') = my arg
+
+
Using JavaScript variables

To print out the value of x, we add the following code. -

Object x = scope.get("x", scope); -
if (x == Scriptable.NOT_FOUND) -
    System.out.println("x -is not defined."); -
else -
    System.out.println("x -= " + Context.toString(x)); -
 
+
+
+Object x = scope.get("x", scope);
+if (x == Scriptable.NOT_FOUND) {
+    System.out.println("x is not defined.");
+} else {
+    System.out.println("x = " + Context.toString(x));
+}
+
+
Calling JavaScript functions

To get the function f, call it, and print the result, we add this code: -

Object f = scope.get("f", scope); -
if (!(f instanceof Function)) -
    System.out.println("f -is undefined or not a function."); -
else { -
    Object functionArgs[] -= { "my arg" }; -
    Object result = ((Function) -f).call(cx, scope, scope, functionArgs); -
    System.out.println("f('my -args') = " + Context.toString(result)); -
}
- +
+
+Object fObj = scope.get("f", scope);
+if (!(fObj instanceof Function)) {
+    System.out.println("f is undefined or not a function.");
+} else {
+    Object functionArgs[] = { "my arg" };
+    Function f = (Function)fObj;
+    Object result = f.call(cx, scope, scope, functionArgs);
+    String report = "f('my args') = " + Context.toString(result);
+    System.out.println(report);
+}
+
+


JavaScript host objects

Defining Host Objects @@ -267,7 +280,8 @@ built-in defineClass function. We'll see how to add it to RunScript later. (Note that because the java -jar option preempts the rest of the classpath, we can't use that and access the Counter class.)

-
$ java -cp 'js.jar;examples' org.mozilla.javascript.tools.shell.Main
+
+$ java -cp 'js.jar;examples' org.mozilla.javascript.tools.shell.Main
 js> defineClass("Counter")
 js> c = new Counter(7)
 [object Counter]
@@ -279,7 +293,8 @@ js> c.count
 9
 js> c.resetCount()
 js> c.count
-0
+0 +
Counter's constructors

The zero-argument constructor is used by Rhino runtime to create instances. @@ -323,14 +338,20 @@ c.count;' It also creates a new instance of the Counter object from within our Java code, constructing it with the value 7, and assigning it to the top-level variable myCounter: -

Object[] arg = { new Integer(7) }; -
Scriptable myCounter = cx.newObject(scope, -"Counter", arg); -
scope.put("myCounter", scope, myCounter);
+
+
+Object[] arg = { new Integer(7) };
+Scriptable myCounter = cx.newObject(scope, "Counter", arg);
+scope.put("myCounter", scope, myCounter);
+
+
Now we can reference the myCounter object from our script: -$ java RunScript3 'RunScript4 'myCounter.count; -myCounter.count' -
8
+
+
+$ java RunScript3 'RunScript4 'myCounter.count; myCounter.count'
+8
+
+
diff --git a/js/rhino/examples/CounterTest.java b/js/rhino/examples/CounterTest.java index b85c5c6ffb1f..c403fe08d9ab 100644 --- a/js/rhino/examples/CounterTest.java +++ b/js/rhino/examples/CounterTest.java @@ -18,7 +18,7 @@ * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * - * Contributor(s): + * Contributor(s): * Norris Boyd * * Alternatively, the contents of this file may be used under the @@ -49,27 +49,31 @@ import org.mozilla.javascript.*; */ public class CounterTest { - public static void main(String[] args) throws Exception - { - Context cx = Context.enter(); - Scriptable scope = cx.initStandardObjects(null); - ScriptableObject.defineClass(scope, Counter.class); + public static void main(String[] args) throws Exception + { + Context cx = Context.enter(); + try { + Scriptable scope = cx.initStandardObjects(null); + ScriptableObject.defineClass(scope, Counter.class); - Scriptable testCounter = cx.newObject(scope, "Counter"); - - Object count = ScriptableObject.getProperty(testCounter, "count"); - System.out.println("count = " + count); + Scriptable testCounter = cx.newObject(scope, "Counter"); - count = ScriptableObject.getProperty(testCounter, "count"); - System.out.println("count = " + count); + Object count = ScriptableObject.getProperty(testCounter, "count"); + System.out.println("count = " + count); - ScriptableObject.callMethod(testCounter, "resetCount", new Object[0]); - System.out.println("resetCount"); + count = ScriptableObject.getProperty(testCounter, "count"); + System.out.println("count = " + count); - count = ScriptableObject.getProperty(testCounter, "count"); - System.out.println("count = " + count); + ScriptableObject.callMethod(testCounter, + "resetCount", + new Object[0]); + System.out.println("resetCount"); - Context.exit(); - } + count = ScriptableObject.getProperty(testCounter, "count"); + System.out.println("count = " + count); + } finally { + Context.exit(); + } + } } diff --git a/js/rhino/examples/RunScript.java b/js/rhino/examples/RunScript.java index 02575c390795..a641505fd507 100644 --- a/js/rhino/examples/RunScript.java +++ b/js/rhino/examples/RunScript.java @@ -18,7 +18,7 @@ * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * - * Contributor(s): + * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the @@ -36,38 +36,41 @@ import org.mozilla.javascript.*; /** * RunScript: simplest example of controlling execution of Rhino. - * - * Collects its arguments from the command line, executes the + * + * Collects its arguments from the command line, executes the * script, and prints the result. - * + * * @author Norris Boyd */ public class RunScript { - public static void main(String args[]) - throws JavaScriptException + public static void main(String args[]) + throws JavaScriptException { // Creates and enters a Context. The Context stores information // about the execution environment of a script. Context cx = Context.enter(); - - // Initialize the standard objects (Object, Function, etc.) - // This must be done before scripts can be executed. Returns - // a scope object that we use in later calls. - Scriptable scope = cx.initStandardObjects(null); - - // Collect the arguments into a single string. - String s = ""; - for (int i=0; i < args.length; i++) - s += args[i]; - - // Now evaluate the string we've colected. - Object result = cx.evaluateString(scope, s, "", 1, null); - - // Convert the result to a string and print it. - System.err.println(cx.toString(result)); - - // Exit from the context. - Context.exit(); + try { + // Initialize the standard objects (Object, Function, etc.) + // This must be done before scripts can be executed. Returns + // a scope object that we use in later calls. + Scriptable scope = cx.initStandardObjects(null); + + // Collect the arguments into a single string. + String s = ""; + for (int i=0; i < args.length; i++) { + s += args[i]; + } + + // Now evaluate the string we've colected. + Object result = cx.evaluateString(scope, s, "", 1, null); + + // Convert the result to a string and print it. + System.err.println(cx.toString(result)); + + } finally { + // Exit from the context. + Context.exit(); + } } } diff --git a/js/rhino/examples/RunScript2.java b/js/rhino/examples/RunScript2.java index 89699973f8c4..76a8c37a76a2 100644 --- a/js/rhino/examples/RunScript2.java +++ b/js/rhino/examples/RunScript2.java @@ -18,7 +18,7 @@ * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * - * Contributor(s): + * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the @@ -36,27 +36,31 @@ import org.mozilla.javascript.*; /** * RunScript2: Like RunScript, but reflects the System.out into JavaScript. - * + * * @author Norris Boyd */ public class RunScript2 { - public static void main(String args[]) - throws JavaScriptException + public static void main(String args[]) + throws JavaScriptException { Context cx = Context.enter(); - Scriptable scope = cx.initStandardObjects(null); + try { + Scriptable scope = cx.initStandardObjects(null); - // Add a global variable "out" that is a JavaScript reflection - // of System.out - Scriptable jsArgs = Context.toObject(System.out, scope); - scope.put("out", scope, jsArgs); - - String s = ""; - for (int i=0; i < args.length; i++) - s += args[i]; - Object result = cx.evaluateString(scope, s, "", 1, null); - System.err.println(cx.toString(result)); - Context.exit(); + // Add a global variable "out" that is a JavaScript reflection + // of System.out + Scriptable jsArgs = Context.toObject(System.out, scope); + scope.put("out", scope, jsArgs); + + String s = ""; + for (int i=0; i < args.length; i++) { + s += args[i]; + } + Object result = cx.evaluateString(scope, s, "", 1, null); + System.err.println(cx.toString(result)); + } finally { + Context.exit(); + } } } diff --git a/js/rhino/examples/RunScript3.java b/js/rhino/examples/RunScript3.java index 71b822a228b7..0af564e78231 100644 --- a/js/rhino/examples/RunScript3.java +++ b/js/rhino/examples/RunScript3.java @@ -18,7 +18,7 @@ * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * - * Contributor(s): + * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the @@ -36,45 +36,51 @@ import org.mozilla.javascript.*; /** * RunScript3: Example of using JavaScript objects - * - * Collects its arguments from the command line, executes the + * + * Collects its arguments from the command line, executes the * script, and then ... - * + * * @author Norris Boyd */ public class RunScript3 { - public static void main(String args[]) - throws JavaScriptException + public static void main(String args[]) + throws JavaScriptException { Context cx = Context.enter(); - Scriptable scope = cx.initStandardObjects(null); - - // Collect the arguments into a single string. - String s = ""; - for (int i=0; i < args.length; i++) - s += args[i]; - - // Now evaluate the string we've colected. We'll ignore the result. - cx.evaluateString(scope, s, "", 1, null); + try { + Scriptable scope = cx.initStandardObjects(null); - // Print the value of variable "x" - Object x = scope.get("x", scope); - if (x == Scriptable.NOT_FOUND) - System.out.println("x is not defined."); - else - System.out.println("x = " + Context.toString(x)); + // Collect the arguments into a single string. + String s = ""; + for (int i=0; i < args.length; i++) { + s += args[i]; + } - // Call function "f('my arg')" and print its result. - Object f = scope.get("f", scope); - if (!(f instanceof Function)) - System.out.println("f is undefined or not a function."); - else { - Object functionArgs[] = { "my arg" }; - Object result = ((Function) f).call(cx, scope, scope, functionArgs); - System.out.println("f('my args') = " + Context.toString(result)); + // Now evaluate the string we've colected. We'll ignore the result. + cx.evaluateString(scope, s, "", 1, null); + + // Print the value of variable "x" + Object x = scope.get("x", scope); + if (x == Scriptable.NOT_FOUND) { + System.out.println("x is not defined."); + } else { + System.out.println("x = " + Context.toString(x)); + } + + // Call function "f('my arg')" and print its result. + Object fObj = scope.get("f", scope); + if (!(fObj instanceof Function)) { + System.out.println("f is undefined or not a function."); + } else { + Object functionArgs[] = { "my arg" }; + Function f = (Function)fObj; + Object result = f.call(cx, scope, scope, functionArgs); + String report = "f('my args') = " + Context.toString(result); + System.out.println(report); + } + } finally { + Context.exit(); } - - Context.exit(); } } diff --git a/js/rhino/examples/RunScript4.java b/js/rhino/examples/RunScript4.java index d6a1213f7700..180b329855e9 100644 --- a/js/rhino/examples/RunScript4.java +++ b/js/rhino/examples/RunScript4.java @@ -18,7 +18,7 @@ * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * - * Contributor(s): + * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the @@ -37,34 +37,38 @@ import org.mozilla.javascript.*; /** * RunScript4: Execute scripts in an environment that includes the * example Counter class. - * + * * @author Norris Boyd */ public class RunScript4 { - public static void main(String args[]) - throws Exception + public static void main(String args[]) + throws Exception { Context cx = Context.enter(); - Scriptable scope = cx.initStandardObjects(null); + try { + Scriptable scope = cx.initStandardObjects(null); - // Use the Counter class to define a Counter constructor - // and prototype in JavaScript. - ScriptableObject.defineClass(scope, Counter.class); + // Use the Counter class to define a Counter constructor + // and prototype in JavaScript. + ScriptableObject.defineClass(scope, Counter.class); - // Create an instance of Counter and assign it to - // the top-level variable "myCounter". This is - // equivalent to the JavaScript code - // myCounter = new Counter(7); - Object[] arg = { new Integer(7) }; - Scriptable myCounter = cx.newObject(scope, "Counter", arg); - scope.put("myCounter", scope, myCounter); - - String s = ""; - for (int i=0; i < args.length; i++) - s += args[i]; - Object result = cx.evaluateString(scope, s, "", 1, null); - System.err.println(cx.toString(result)); - Context.exit(); + // Create an instance of Counter and assign it to + // the top-level variable "myCounter". This is + // equivalent to the JavaScript code + // myCounter = new Counter(7); + Object[] arg = { new Integer(7) }; + Scriptable myCounter = cx.newObject(scope, "Counter", arg); + scope.put("myCounter", scope, myCounter); + + String s = ""; + for (int i=0; i < args.length; i++) { + s += args[i]; + } + Object result = cx.evaluateString(scope, s, "", 1, null); + System.err.println(cx.toString(result)); + } finally { + Context.exit(); + } } }