1
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
4
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5
<meta name="Author" content="Norris Boyd">
6
<meta name="GENERATOR" content="Mozilla/4.7 [en]C-NSCP (WinNT; U) [Netscape]">
7
<title>JavaScript Shell</title>
9
<body bgcolor="#FFFFFF">
13
JavaScript Shell</h1></center>
14
The JavaScript shell provides a simple way to run scripts in batch mode
15
or an interactive environment for exploratory programming.
17
Invoking the Shell</h2>
18
<tt>java org.mozilla.javascript.tools.shell.Main</tt> [<i>options</i>]
20
[<i>script-arguments</i>]</tt>
21
<p>where <i>options</i> are:
22
<p><tt>-e </tt><i>script-source</i>
23
<blockquote>Executes <i>script-source</i> as a JavaScript script.</blockquote>
24
<tt>-f </tt><i>script-filename</i>
25
<blockquote>Reads filename specified by <i>script-filename</i> and executes
26
it as a JavaScript script.</blockquote>
27
<tt>-opt </tt><i>optLevel</i>
28
<br><tt>-O</tt> <i>optLevel</i>
29
<ul>Optimizes at level <i>optLevel</i>, which must be an integer between
30
0 and 9. See <a href="opt.html">Optimization</a> for more details.</ul>
31
<tt>-version </tt><i>versionNumber</i>
32
<ul>Specifies the language version to compile with. The string <i>versionNumber</i>
33
must be one of <tt>100</tt>, <tt>110</tt>, <tt>120</tt>, <tt>130</tt>,
34
or <tt>140</tt>. See <a href="overview.html#versions">JavaScript Language
35
Versions</a> for more information on language versions.</ul>
38
Predefined Properties</h2>
39
Scripts executing in the shell have access to some additional properties
40
of the top-level object.
45
<blockquote>The <tt>arguments</tt> object is an array containing the strings
46
of all the arguments given at the command line when the shell was invoked.</blockquote>
51
<blockquote>Executing the help function will print usage and help messages.</blockquote>
54
defineClass(<i>className</i>)</h4>
56
<blockquote>Define an extension using the Java class named with the string
57
argument <i>className</i>. Uses ScriptableObject.defineClass() to define
58
the extension.</blockquote>
61
load([<i>filename</i>, ...])</h4>
63
<blockquote>Load JavaScript source files named by string arguments. If
64
multiple arguments are given, each file is read in and executed in turn.</blockquote>
67
loadClass(<i>className</i>)</h4>
69
<blockquote>Load and execute the class named by the string argument <i>className</i>.
70
The class must be a class that implements the Script interface, as will
71
any script compiled by <a href="jsc.html">jsc</a>.</blockquote>
74
print([<i>expr</i> ...])</h4>
76
<blockquote>Evaluate and print expressions. Evaluates each expression,
77
converts the result to a string, and prints it.</blockquote>
82
<blockquote>Quit shell. The shell will also quit in interactive mode if
83
an end-of-file character is typed at the prompt.</blockquote>
86
version([<i>number</i>])</h4>
88
<blockquote>Get or set JavaScript version number. If no argument is supplied,
89
the current version number is returned. If an argument is supplied, it
90
is expected to be one of <tt>100</tt>, <tt>110</tt>, <tt>120</tt>, <tt>130,</tt>
91
or <tt>140</tt> to indicate JavaScript version 1.0, 1.1, 1.2, 1.3, or 1.4
92
respectively.</blockquote>
96
Here the shell is invoked three times from the command line. (The system
97
command prompt is shown as <tt>$</tt>.) The first invocation executes a
98
script specified on the command line itself. The next invocation has no
99
arguments, so the shell goes into interactive mode, reading and evaluating
100
each line as it is typed in. Finally, the last invocation executes a script
101
from a file and accesses arguments to the script itself.
102
<p><tt>$ java org.mozilla.javascript.tools.shell.Main -e print('hi')</tt>
104
<p><tt>$ java org.mozilla.javascript.tools.shell.Main</tt>
105
<p><tt>js> print('hi')</tt>
109
<br><tt>js> function f() {</tt>
110
<br><tt> return a;</tt>
112
<br><tt>js> var a = 34;</tt>
115
<br><tt>js> quit()</tt>
116
<p><tt>$ cat echo.js</tt>
117
<br><tt>for (i in arguments) {</tt>
118
<br><tt> print(arguments[i])</tt>
120
<br><tt>$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar</tt>
126
<br><a href="index.html">back to top</a>
1
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
4
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5
<meta name="Author" content="Norris Boyd">
6
<meta name="GENERATOR" content="Mozilla/4.7 [en]C-NSCP (WinNT; U) [Netscape]">
7
<title>JavaScript Shell</title>
9
<body bgcolor="#FFFFFF">
13
JavaScript Shell</h1></center>
14
The JavaScript shell provides a simple way to run scripts in batch mode
15
or an interactive environment for exploratory programming.
17
Invoking the Shell</h2>
18
<tt>java org.mozilla.javascript.tools.shell.Main [<i>options</i>]
19
<i>script-filename-or-url</i> [<i>script-arguments</i>]</tt>
20
<p>where <tt><i>options</i></tt> are:
23
<tt>-e <i>script-source</i></tt>
24
<blockquote>Executes <i>script-source</i> as a JavaScript script.</blockquote>
25
<tt>-f <i>script-filename-or-url</i></tt>
26
<blockquote>Reads <i>script-filename-or-url</i> content and execute it as a JavaScript script.</blockquote>
28
<tt>-opt <i>optLevel</i></tt>
29
<br><tt>-O <i>optLevel</i></tt>
31
Optimizes at level <i>optLevel</i>, which must be an integer between
32
0 and 9. See <a href="opt.html">Optimization</a> for more details.
35
<tt>-version <i>versionNumber</i></tt>
37
Specifies the language version to compile with. The string <i>versionNumber</i>
38
must be one of <tt>100</tt>, <tt>110</tt>, <tt>120</tt>, <tt>130</tt>,
39
or <tt>140</tt>. See <a href="overview.html#versions">JavaScript Language
40
Versions</a> for more information on language versions.
48
<tt>-continuations</tt>
50
Enable experiments support for continuations and set the optimization level to -1 to force interpretation mode.
53
If the shell is invoked with the system property rhino.use_java_policy_security set to true and with a security manager installed, the shell restricts scripts permissions based on their URLs according to Java policy settings. This is available only if JVM implements Java2 security model.
56
Predefined Properties</h2>
57
Scripts executing in the shell have access to some additional properties
58
of the top-level object.
63
<blockquote>The <tt>arguments</tt> object is an array containing the strings
64
of all the arguments given at the command line when the shell was invoked.</blockquote>
69
<blockquote>Executing the help function will print usage and help messages.</blockquote>
72
defineClass(<i>className</i>)</h4>
74
<blockquote>Define an extension using the Java class named with the string
75
argument <i>className</i>. Uses ScriptableObject.defineClass() to define
76
the extension.</blockquote>
79
deserialize(<i>filename</i>)</h4>
81
<blockquote>Restore from the specified file an object previously written by a call to <tt>serialize</tt>.</blockquote>
84
load([<i>filename</i>, ...])</h4>
86
<blockquote>Load JavaScript source files named by string arguments. If
87
multiple arguments are given, each file is read in and executed in turn.</blockquote>
90
loadClass(<i>className</i>)</h4>
92
<blockquote>Load and execute the class named by the string argument <i>className</i>.
93
The class must be a class that implements the Script interface, as will
94
any script compiled by <a href="jsc.html">jsc</a>.</blockquote>
97
print([<i>expr</i> ...])</h4>
99
<blockquote>Evaluate and print expressions. Evaluates each expression,
100
converts the result to a string, and prints it.</blockquote>
103
readFile(<i>path</i> [, <i>characterCoding</i>)</h4>
105
<blockquote>Read given file and convert its bytes to a string using the
106
specified character coding or default character coding if explicit coding
107
argument is not given.</blockquote>
110
readUrl(<i>url</i> [, <i>characterCoding</i>)</h4>
112
<blockquote>Open an input connection to the given string url, read all its
113
bytes and convert them to a string using the specified character coding or
114
default character coding if explicit coding argument is not given.</blockquote>
117
runCommand(<i>commandName</i>, [<i>arg</i>, ...] [<i>options</i>])</h4>
119
<blockquote>Execute the specified command with the given argument and options
120
as a separate process and return the exit status of the process. For details, see JavaDoc for <a href="http://lxr.mozilla.org/mozilla/source/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Global.java">org.mozilla.javascript.tools.shell.Global#runCommand</a>.</blockquote>
123
serialize(<i>object</i>, <i>filename</i>)</h4>
124
<blockquote>Serialize the given object to the specified file.</blockquote>
127
spawn(<i>functionOrScript</i>)</h4>
129
<blockquote>Run the given function or script in a different thread.</blockquote>
132
sync(<i>function</i>)</h4>
134
<blockquote>creates a synchronized function (in the sense of a Java synchronized method) from an existing function. The new function synchronizes on the <code>this</code> object of its invocation.</blockquote>
139
<blockquote>Quit shell. The shell will also quit in interactive mode if
140
an end-of-file character is typed at the prompt.</blockquote>
143
version([<i>number</i>])</h4>
145
<blockquote>Get or set JavaScript version number. If no argument is supplied,
146
the current version number is returned. If an argument is supplied, it
147
is expected to be one of <tt>100</tt>, <tt>110</tt>, <tt>120</tt>, <tt>130,</tt>
148
or <tt>140</tt> to indicate JavaScript version 1.0, 1.1, 1.2, 1.3, or 1.4
149
respectively.</blockquote>
155
Here the shell is invoked three times from the command line. (The system
156
command prompt is shown as <tt>$</tt>.) The first invocation executes a
157
script specified on the command line itself. The next invocation has no
158
arguments, so the shell goes into interactive mode, reading and evaluating
159
each line as it is typed in. Finally, the last invocation executes a script
160
from a file and accesses arguments to the script itself.
162
$ java org.mozilla.javascript.tools.shell.Main -e print('hi')
164
$ java org.mozilla.javascript.tools.shell.Main
177
for (i in arguments) {
180
$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar
186
<h4>spawn and sync</h4>
187
The following example creates 2 threads via <tt>spawn</tt> and uses <tt>sync</tt> to create a synchronized version of the function <tt>test</tt>.
190
js> function test(x) {
192
java.lang.Thread.sleep(x*1000);
195
js> var o = { f : sync(test) };
196
js> spawn(function() {o.f(5);});
197
Thread[Thread-0,5,main]
199
js> spawn(function() {o.f(5);});
200
Thread[Thread-1,5,main]
208
Here is few examples of invoking <tt>runCommand</tt> under Linux.
210
js> runCommand('date')
211
Thu Jan 23 16:49:36 CET 2003
213
// Using input option to provide process input
214
js> runCommand("sort", {input: "c\na\nb"})
219
js> // Demo of output and err options
220
js> var opt={input: "c\na\nb", output: 'Sort Output:\n'}
221
js> runCommand("sort", opt)
223
js> print(opt.output)
228
js> var opt={input: "c\na\nb", output: 'Sort Output:\n', err: ''}
229
js> runCommand("sort", "--bad-arg", opt)
232
/bin/sort: unrecognized option `--bad-arg'
233
Try `/bin/sort --help' for more information.
235
js> runCommand("bad_command", "--bad-arg", opt)
236
js: "<stdin>", line 18: uncaught JavaScript exception: java.io.IOException: bad_command: not found
237
js> // Passing explicit environment to the system shell
238
js> runCommand("sh", "-c", "echo $env1 $env2", { env: {env1: 100, env2: 200}})
241
js> // Use args option to provide additional command arguments
242
js> var arg_array = [1, 2, 3, 4];
243
js> runCommand("echo", { args: arg_array})
251
<br><a href="index.html">back to top</a>