~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to docs/shell.html

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2
 
<html>
3
 
<head>
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>
8
 
</head>
9
 
<body bgcolor="#FFFFFF">
10
 
 
11
 
<center>
12
 
<h1>
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.
16
 
<h2>
17
 
Invoking the Shell</h2>
18
 
<tt>java org.mozilla.javascript.tools.shell.Main</tt> [<i>options</i>]
19
 
<i>file</i><tt>.js
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>
36
 
 
37
 
<h2>
38
 
Predefined Properties</h2>
39
 
Scripts executing in the shell have access to some additional properties
40
 
of the top-level object.
41
 
<br>&nbsp;
42
 
<h4>
43
 
arguments</h4>
44
 
 
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>
47
 
 
48
 
<h4>
49
 
help()</h4>
50
 
 
51
 
<blockquote>Executing the help function will print usage and help messages.</blockquote>
52
 
 
53
 
<h4>
54
 
defineClass(<i>className</i>)</h4>
55
 
 
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>
59
 
 
60
 
<h4>
61
 
load([<i>filename</i>, ...])</h4>
62
 
 
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>
65
 
 
66
 
<h4>
67
 
loadClass(<i>className</i>)</h4>
68
 
 
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>
72
 
 
73
 
<h4>
74
 
print([<i>expr</i> ...])</h4>
75
 
 
76
 
<blockquote>Evaluate and print expressions. Evaluates each expression,
77
 
converts the result to a string, and prints it.</blockquote>
78
 
 
79
 
<h4>
80
 
quit()</h4>
81
 
 
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>
84
 
 
85
 
<h4>
86
 
version([<i>number</i>])</h4>
87
 
 
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>
93
 
 
94
 
<h2>
95
 
Example</h2>
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>
103
 
<p><tt>hi</tt>
104
 
<p><tt>$ java org.mozilla.javascript.tools.shell.Main</tt>
105
 
<p><tt>js> print('hi')</tt>
106
 
<br><tt>hi</tt>
107
 
<br><tt>js> 6*7</tt>
108
 
<br><tt>42</tt>
109
 
<br><tt>js> function f() {</tt>
110
 
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a;</tt>
111
 
<br><tt>}</tt>
112
 
<br><tt>js> var a = 34;</tt>
113
 
<br><tt>js> f()</tt>
114
 
<br><tt>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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(arguments[i])</tt>
119
 
<br><tt>}</tt>
120
 
<br><tt>$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar</tt>
121
 
<p><tt>foo</tt>
122
 
<br><tt>bar</tt>
123
 
<p><tt>$</tt>
124
 
<p>
125
 
<hr WIDTH="100%">
126
 
<br><a href="index.html">back to top</a>
127
 
</body>
128
 
</html>
 
1
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
 
2
<html>
 
3
<head>
 
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>
 
8
</head>
 
9
<body bgcolor="#FFFFFF">
 
10
 
 
11
<center>
 
12
<h1>
 
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.
 
16
<h2>
 
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:
 
21
<p>
 
22
 
 
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>
 
27
 
 
28
<tt>-opt <i>optLevel</i></tt>
 
29
<br><tt>-O <i>optLevel</i></tt>
 
30
<blockquote>
 
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.
 
33
</blockquote>
 
34
 
 
35
<tt>-version <i>versionNumber</i></tt>
 
36
<blockquote>
 
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.
 
41
</blockquote>
 
42
 
 
43
<tt>-strict</tt>
 
44
<blockquote>
 
45
Enable strict mode.
 
46
</blockquote>
 
47
 
 
48
<tt>-continuations</tt>
 
49
<blockquote>
 
50
Enable experiments support for continuations and set the optimization level to -1 to force interpretation mode.
 
51
</blockquote>
 
52
 
 
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.
 
54
 
 
55
<h2>
 
56
Predefined Properties</h2>
 
57
Scripts executing in the shell have access to some additional properties
 
58
of the top-level object.
 
59
<br>&nbsp;
 
60
<h4>
 
61
arguments</h4>
 
62
 
 
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>
 
65
 
 
66
<h4>
 
67
help()</h4>
 
68
 
 
69
<blockquote>Executing the help function will print usage and help messages.</blockquote>
 
70
 
 
71
<h4>
 
72
defineClass(<i>className</i>)</h4>
 
73
 
 
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>
 
77
 
 
78
<h4>
 
79
deserialize(<i>filename</i>)</h4>
 
80
 
 
81
<blockquote>Restore from the specified file an object previously written by a call to <tt>serialize</tt>.</blockquote>
 
82
 
 
83
<h4>
 
84
load([<i>filename</i>, ...])</h4>
 
85
 
 
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>
 
88
 
 
89
<h4>
 
90
loadClass(<i>className</i>)</h4>
 
91
 
 
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>
 
95
 
 
96
<h4>
 
97
print([<i>expr</i> ...])</h4>
 
98
 
 
99
<blockquote>Evaluate and print expressions. Evaluates each expression,
 
100
converts the result to a string, and prints it.</blockquote>
 
101
 
 
102
<h4>
 
103
readFile(<i>path</i> [, <i>characterCoding</i>)</h4>
 
104
 
 
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>
 
108
 
 
109
<h4>
 
110
readUrl(<i>url</i> [, <i>characterCoding</i>)</h4>
 
111
 
 
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>
 
115
 
 
116
<h4>
 
117
runCommand(<i>commandName</i>, [<i>arg</i>, ...] [<i>options</i>])</h4>
 
118
 
 
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>
 
121
 
 
122
<h4>
 
123
serialize(<i>object</i>, <i>filename</i>)</h4>
 
124
<blockquote>Serialize the given object to the specified file.</blockquote>
 
125
 
 
126
<h4>
 
127
spawn(<i>functionOrScript</i>)</h4>
 
128
 
 
129
<blockquote>Run the given function or script in a different thread.</blockquote>
 
130
 
 
131
<h4>
 
132
sync(<i>function</i>)</h4>
 
133
 
 
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>
 
135
 
 
136
<h4>
 
137
quit()</h4>
 
138
 
 
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>
 
141
 
 
142
<h4>
 
143
version([<i>number</i>])</h4>
 
144
 
 
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>
 
150
 
 
151
<h2>
 
152
Example</h2>
 
153
 
 
154
<h4>Invocation</h4>
 
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.
 
161
<pre>
 
162
$ java org.mozilla.javascript.tools.shell.Main -e print('hi')
 
163
hi
 
164
$ java org.mozilla.javascript.tools.shell.Main
 
165
js> print('hi')
 
166
hi
 
167
js> 6*7
 
168
42
 
169
js> function f() {
 
170
        return a;
 
171
}
 
172
js> var a = 34;
 
173
js> f()
 
174
34
 
175
js> quit()
 
176
$ cat echo.js
 
177
for (i in arguments) {
 
178
        print(arguments[i])
 
179
}
 
180
$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar
 
181
foo
 
182
bar
 
183
$
 
184
</pre>
 
185
 
 
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>.
 
188
 
 
189
<pre>
 
190
js> function test(x) {
 
191
        print("entry");
 
192
        java.lang.Thread.sleep(x*1000);
 
193
        print("exit");
 
194
}
 
195
js> var o = { f : sync(test) };
 
196
js> spawn(function() {o.f(5);});
 
197
Thread[Thread-0,5,main]
 
198
entry
 
199
js> spawn(function() {o.f(5);});
 
200
Thread[Thread-1,5,main]
 
201
js>
 
202
exit
 
203
entry
 
204
exit
 
205
</pre>
 
206
 
 
207
<h4>runCommand</h4>
 
208
Here is few examples of invoking <tt>runCommand</tt> under Linux.
 
209
<pre>
 
210
js> runCommand('date')
 
211
Thu Jan 23 16:49:36 CET 2003
 
212
0
 
213
// Using input option to provide process input
 
214
js> runCommand("sort", {input: "c\na\nb"})
 
215
a
 
216
b
 
217
c
 
218
0
 
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)
 
222
0
 
223
js> print(opt.output)
 
224
Sort Output:
 
225
a
 
226
b
 
227
c
 
228
js> var opt={input: "c\na\nb", output: 'Sort Output:\n', err: ''}
 
229
js> runCommand("sort", "--bad-arg", opt)
 
230
2
 
231
js> print(opt.err)
 
232
/bin/sort: unrecognized option `--bad-arg'
 
233
Try `/bin/sort --help' for more information.
 
234
 
 
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}})
 
239
100 200
 
240
0
 
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})
 
244
1 2 3 4
 
245
0
 
246
</pre>
 
247
 
 
248
 
 
249
 
 
250
<hr WIDTH="100%">
 
251
<br><a href="index.html">back to top</a>
 
252
</body>
 
253
</html>