зеркало из https://github.com/mozilla/gecko-dev.git
Examples now uses try {} finally { Context.exit(); } to release Context even in case of errors
This commit is contained in:
Родитель
2f03420b5d
Коммит
1cf0bd8a05
|
@ -139,10 +139,14 @@ that we use in later calls.
|
|||
<p><a NAME="Collecting"></a><font size=+2>Collecting the arguments</font>
|
||||
<p>This code is standard Java and not specific to Rhino. It just collects
|
||||
all the arguments and concatenates them together.
|
||||
<blockquote><tt><font color="#006600">String s = "";</font></tt>
|
||||
<br><tt><font color="#006600">for (int i=0; i < args.length; i++)</font></tt>
|
||||
<br><tt><font color="#006600"> s += args[i];</font></tt></blockquote>
|
||||
|
||||
<blockquote>
|
||||
<pre style="color: #006600">
|
||||
String s = "";
|
||||
for (int i=0; i < args.length; i++) {
|
||||
s += args[i];
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
<p><br><a NAME="Evaluating"></a><font size=+2>Evaluating a script</font>
|
||||
<p>The code
|
||||
<blockquote>
|
||||
|
@ -166,11 +170,15 @@ method converts any JavaScript value to a string.
|
|||
<p><a NAME="Exit"></a><font size=+2>Exit the Context</font>
|
||||
<p>The code
|
||||
<blockquote>
|
||||
<pre><font color="#006600">Context.exit();</font></pre>
|
||||
<pre style="color: #006600">
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
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 <tt>exit</tt> for every call to <tt>enter</tt>.
|
||||
a call to <tt>exit</tt> for every call to <tt>enter</tt>. 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 <tt>Context.enter()</tt>.
|
||||
<br>
|
||||
<dir> </dir>
|
||||
<a NAME="Expose"></a><font size=+3>Expose Java APIs</font>
|
||||
|
@ -219,40 +227,45 @@ is illustrated in the <a href="http://lxr.mozilla.org/mozilla/source/js/rhino/ex
|
|||
example. This example adds the ability to print the value of variable <tt>x</tt>
|
||||
and the result of calling function <tt>f</tt>. Both <tt>x</tt> and <tt>f</tt>
|
||||
are expected to be defined by the evaluated script. For example,
|
||||
<blockquote><tt><font color="#663366">$ java RunScript3 'x = 7'</font></tt>
|
||||
<br><tt><font color="#663366">x = 7</font></tt>
|
||||
<br><tt><font color="#663366">f is undefined or not a function.</font></tt>
|
||||
<br><tt><font color="#663366">$ java RunScript3 'function f(a) { return
|
||||
a; }'</font></tt>
|
||||
<br><tt><font color="#663366">x is not defined.</font></tt>
|
||||
<br><tt><font color="#663366">f('my args') = my arg</font></tt>
|
||||
<br><tt><font color="#663366"></font></tt> </blockquote>
|
||||
<blockquote>
|
||||
<pre style="color: #663366">
|
||||
$ 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
|
||||
</pre>
|
||||
</blockquote>
|
||||
<a NAME="UsingJSvars"></a><font size=+2>Using JavaScript variables</font>
|
||||
<p>To print out the value of <tt>x</tt>, we add the following code.
|
||||
<dir><tt><font color="#006600">Object x = scope.get("x", scope);</font></tt>
|
||||
<br><tt><font color="#006600">if (x == Scriptable.NOT_FOUND)</font></tt>
|
||||
<br><tt><font color="#006600"> System.out.println("x
|
||||
is not defined.");</font></tt>
|
||||
<br><tt><font color="#006600">else</font></tt>
|
||||
<br><tt><font color="#006600"> System.out.println("x
|
||||
= " + Context.toString(x));</font></tt>
|
||||
<br><tt><font color="#006600"></font></tt> </dir>
|
||||
<blockquote>
|
||||
<pre style="color: #006600">
|
||||
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));
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
<a NAME="CallingJSfuns"></a><font size=+2>Calling JavaScript functions</font>
|
||||
<p>To get the function <tt>f</tt>, call it, and print the result, we add
|
||||
this code:
|
||||
<dir><tt><font color="#006600">Object f = scope.get("f", scope);</font></tt>
|
||||
<br><tt><font color="#006600">if (!(f instanceof Function))</font></tt>
|
||||
<br><tt><font color="#006600"> System.out.println("f
|
||||
is undefined or not a function.");</font></tt>
|
||||
<br><tt><font color="#006600">else {</font></tt>
|
||||
<br><tt><font color="#006600"> Object functionArgs[]
|
||||
= { "my arg" };</font></tt>
|
||||
<br><tt><font color="#006600"> Object result = ((Function)
|
||||
f).call(cx, scope, scope, functionArgs);</font></tt>
|
||||
<br><tt><font color="#006600"> System.out.println("f('my
|
||||
args') = " + Context.toString(result));</font></tt>
|
||||
<br><tt><font color="#006600">}</font></tt></dir>
|
||||
|
||||
<blockquote>
|
||||
<pre style="color: #006600">
|
||||
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);
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
<p><br><a NAME="JavaScriptHostObjects"></a><font size=+3>JavaScript host
|
||||
objects</font>
|
||||
<p><a NAME="DefiningHostObjects"></a><font size=+2>Defining Host Objects</font>
|
||||
|
@ -267,7 +280,8 @@ built-in <tt>defineClass</tt> function. We'll see how to add it to RunScript
|
|||
later. (Note that because the <tt>java -jar</tt> option preempts the rest
|
||||
of the classpath, we can't use that and access the <tt>Counter</tt> class.)
|
||||
<blockquote>
|
||||
<pre><font color="#663366">$ java -cp 'js.jar;examples' org.mozilla.javascript.tools.shell.Main
|
||||
<pre style="color: #663366">
|
||||
$ 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</font></pre>
|
||||
0
|
||||
</pre>
|
||||
</blockquote>
|
||||
<a NAME="CounterCtors"></a><font size=+2>Counter's constructors</font>
|
||||
<p>The zero-argument constructor is used by Rhino runtime to create instances.
|
||||
|
@ -323,14 +338,20 @@ c.count;'</font></tt>
|
|||
It also creates a new instance of the <tt>Counter</tt> object from within
|
||||
our Java code, constructing it with the value 7, and assigning it to the
|
||||
top-level variable <tt>myCounter</tt>:
|
||||
<dir><tt><font color="#006600">Object[] arg = { new Integer(7) };</font></tt>
|
||||
<br><tt><font color="#006600">Scriptable myCounter = cx.newObject(scope,
|
||||
"Counter", arg);</font></tt>
|
||||
<br><tt><font color="#006600">scope.put("myCounter", scope, myCounter);</font></tt></dir>
|
||||
<blockquote>
|
||||
<pre style="color: #006600">
|
||||
Object[] arg = { new Integer(7) };
|
||||
Scriptable myCounter = cx.newObject(scope, "Counter", arg);
|
||||
scope.put("myCounter", scope, myCounter);
|
||||
</pre>
|
||||
</blockquote>
|
||||
Now we can reference the <tt>myCounter</tt> object from our script:
|
||||
<dir><tt><font color="#663366">$ java RunScript3 'RunScript4 'myCounter.count;
|
||||
myCounter.count'</font></tt>
|
||||
<br><tt><font color="#663366">8</font></tt></dir>
|
||||
<blockquote>
|
||||
<pre style="color: #663366">
|
||||
$ java RunScript3 'RunScript4 'myCounter.count; myCounter.count'
|
||||
8
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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, "<cmd>", 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, "<cmd>", 1, null);
|
||||
|
||||
// Convert the result to a string and print it.
|
||||
System.err.println(cx.toString(result));
|
||||
|
||||
} finally {
|
||||
// Exit from the context.
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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, "<cmd>", 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, "<cmd>", 1, null);
|
||||
System.err.println(cx.toString(result));
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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, "<cmd>", 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, "<cmd>", 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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, "<cmd>", 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, "<cmd>", 1, null);
|
||||
System.err.println(cx.toString(result));
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче