diff options
Diffstat (limited to 'files/zh-cn/mozilla/projects/rhino/shell/index.html')
-rw-r--r-- | files/zh-cn/mozilla/projects/rhino/shell/index.html | 179 |
1 files changed, 0 insertions, 179 deletions
diff --git a/files/zh-cn/mozilla/projects/rhino/shell/index.html b/files/zh-cn/mozilla/projects/rhino/shell/index.html deleted file mode 100644 index d5a4b779a9..0000000000 --- a/files/zh-cn/mozilla/projects/rhino/shell/index.html +++ /dev/null @@ -1,179 +0,0 @@ ---- -title: Shell -slug: Mozilla/Projects/Rhino/Shell -translation_of: Mozilla/Projects/Rhino/Shell ---- -<p>JavaScript的外壳提供了一个简单的方法来在批处理模式下或编程的一个探索性的互动环境运行脚本.</p> -<h3 id="Invoking_the_Shell" name="Invoking_the_Shell">调用Shell</h3> -<p><big><code>java org.mozilla.javascript.tools.shell.Main {{ mediawiki.external('<var>options</var>') }} <var>script-filename-or-url</var> {{ mediawiki.external('<var>script-arguments</var>') }} </code></big></p> -<p>这里是一些选项 <code><var>options</var></code></p> -<h4 id="-e_script-source" name="-e_script-source"><code>-e <var>script-source</var></code></h4> -<p>把 <var>script-source</var> 作为 JavaScript 执行.</p> -<h4 id="-f_script-filename-or-url" name="-f_script-filename-or-url"><code>-f <var>script-filename-or-url</var></code></h4> -<p>读取<var>script-filename-or-url</var> 的内容并作为JavaScript执行.</p> -<h4 id="-opt_optLevel_.2F_-O_optLevel" name="-opt_optLevel_.2F_-O_optLevel"><code>-opt <var>optLevel</var></code> / <code>-O <var>optLevel</var></code></h4> -<p>优化等级 <var>optLevel</var>, 必须是 <code>-1</code> 或 [0-9]之间的整数. 参考<a href="/en/Rhino/Optimization" title="en/Rhino_Optimization">Rhino Optimization</a> 或许详情.</p> -<h4 id="-version_versionNumber" name="-version_versionNumber"><code>-version <var>versionNumber</var></code></h4> -<p>制定语言编译版本. 字符串 <var>versionNumber</var> 必须是 <code>100</code>, <code>110</code>, <code>120</code>, <code>130</code>, <code>140</code>, <a class="external" href="/en/JavaScript/New_in_JavaScript/1.5" title="https://developer.mozilla.org/editor/fckeditor/core/editor/en/New_in_JavaScript_1.5"><code>150</code></a>, <code><a class="external" href="/en/JavaScript/New_in_JavaScript/1.6" title="https://developer.mozilla.org/editor/fckeditor/core/editor/en/New_in_JavaScript_1.6">160</a> 或 <a class="external" href="/en/JavaScript/New_in_JavaScript/1.7" title="https://developer.mozilla.org/editor/fckeditor/core/editor/en/New_in_JavaScript_1.7">170</a></code>. 参考<a href="/en/Rhino/Overview#JavaScript_Language_Versions" title="en/Rhino_Overview#JavaScript_Language_Versions">JavaScript Language Versions</a>获取更多的语言版本信息.</p> -<h4 id="-strict" name="-strict"><code>-strict</code></h4> -<p>开启strict 模式.</p> -<h4 id="-continuations" name="-continuations"><code>-continuations</code></h4> -<p>启用continuation的实验支持,建立优化级别为-1强制解释模式. 从Rhino 1.7 开始这个选项不再可用.</p> -<h4 id="Note" name="Note">注意</h4> -<p>如果shell被使用设置为<code>true</code>的系统属性<code>rhino.use_java_policy_security调用,一个安全管理将被安装</code>, shell限制脚本通过基于根据URL访问Java安全选项的权限.如果JVM实现Java2的安全模型才可以用.</p> -<h3 id="Predefined_Properties" name="Predefined_Properties">预定义的属性</h3> -<p>在shell中呗执行的脚本访问顶层定义的一些属性.</p> -<h4 id="arguments" name="arguments"><code>arguments</code></h4> -<p><code>当shell被调用时,arguments</code> 对象是一个包含所有通过命令行输入的字符串的数组</p> -<h4 id="environment" name="environment"><code>environment</code></h4> -<p>返回当前环境对象.</p> -<h4 id="history" name="history"><code>history</code></h4> -<p>显示命令行中执行命令的历史.</p> -<h4 id="help.28.29" name="help.28.29"><code>help()</code></h4> -<p>获取帮助信息.</p> -<h4 id="defineClass.28className.29" name="defineClass.28className.29"><code>defineClass(<var>className</var>)</code></h4> -<p>定义一个使用Java类命名的扩展,参数 <var>className</var>是个类名的字符串. 使用 <code>ScriptableObject.defineClass()</code> 定义扩展.</p> -<h4 id="deserialize.28filename.29" name="deserialize.28filename.29"><code>deserialize(<var>filename</var>)</code></h4> -<p>从指定的文件恢复以前通过调用一个被<code>serialize</code>的对象.</p> -<h4 id="gc" name="gc"><code>gc()</code></h4> -<p>运行垃圾回收器.</p> -<h4 id="load.28.5Bfilename.2C_....5D.29" name="load.28.5Bfilename.2C_....5D.29"><code>load([<var>filename</var>, ...])</code></h4> -<p>载入字符串命名的参数JavaScript源码文件. 如果有多个参数被传递,那么每一个参数都会被读取并执行.</p> -<h4 id="loadClass.28className.29" name="loadClass.28className.29"><code>loadClass(<var>className</var>)</code></h4> -<p>载入并执行字符串<var>className</var>.表示的类.这个类必须是已实现的脚本接口, 就像任何被编译的脚本 <a href="/en/Rhino/JavaScript_Compiler" title="en/Rhino_JavaScript_Compiler">Rhino JavaScript Compiler</a>.</p> -<h4 id="print.28.5Bexpr_....5D.29" name="print.28.5Bexpr_....5D.29"><code>print([<var>expr</var> ...])</code></h4> -<p>评估并打印表达式. 评估每一个表达式, 把结果转化为字符串并打印.</p> -<h4 id="readFile.28path_.5B.2C_characterCoding.29" name="readFile.28path_.5B.2C_characterCoding.29"><code>readFile(<var>path</var> [, <var>characterCoding]</var>)</code></h4> -<p>读取给定的文件并使用指定的编码把字节转换为字符串,如果没有制定编码就用默认编码.</p> -<h4 id="readUrl.28url_.5B.2C_characterCoding.29" name="readUrl.28url_.5B.2C_characterCoding.29"><code>readUrl(<var>url</var> [, <var>characterCoding</var>])</code></h4> -<p>打开指定的url,读取所有的字节并用指定的编码转化为数组,如果没有指定编码就是用默认的.</p> -<h4 id="runCommand.28commandName.2C_.5Barg.2C_....5D_.5Boptions.5D.29" name="runCommand.28commandName.2C_.5Barg.2C_....5D_.5Boptions.5D.29"><code>runCommand(<var>commandName</var>, [<var>arg</var>, ...] [<var>options</var>])</code></h4> -<p>执行参数中给定的命令,作为一个独立的进程选项并返回该进程的退出状态.</p> -<p>Usage:</p> -<pre>runCommand(command) -runCommand(command, arg1, ..., argN) -runCommand(command, arg1, ..., argN, options) -</pre> -<p>All except the last arguments to <code>runCommand</code> are converted to strings and denote command name and its arguments. If the last argument is a JavaScript object, it is an option object. Otherwise it is converted to string denoting the last argument and options objects assumed to be empty.</p> -<p>The following properties of the option object are processed:</p> -<ul> - <li><code>args</code> - provides an array of additional command arguments</li> - <li><code>env</code> - explicit environment object. All its enumeratable properties define the corresponding environment variable names.</li> - <li><code>input</code> - the process input. If it is not <code>java.io.InputStream</code>, it is converted to string and sent to the process as its input. If not specified, no input is provided to the process.</li> - <li><code>output</code> - the process output instead of <code>java.lang.System.out</code>. If it is not instance of <code>java.io.OutputStream</code>, the process output is read, converted to a string, appended to the output property value converted to string and put as the new value of the output property.</li> - <li><code>err</code> - the process error output instead of <code>java.lang.System.err</code>. If it is not instance of <code>java.io.OutputStream</code>, the process error output is read, converted to a string, appended to the err property value converted to string and put as the new value of the err property.</li> -</ul> -<h4 id="seal.28object.29" name="seal.28object.29"><code>seal(<var>object</var>)</code></h4> -<p>Seal the specified object so any attempt to add, delete or modify its properties would throw an exception.</p> -<h4 id="serialize.28object.2C_filename.29" name="serialize.28object.2C_filename.29"><code>serialize(<var>object</var>, <var>filename</var>)</code></h4> -<p>Serialize the given object to the specified file.</p> -<h4 id="spawn.28functionOrScript.29" name="spawn.28functionOrScript.29"><code>spawn(<var>functionOrScript</var>)</code></h4> -<p>Run the given function or script in a different thread.</p> -<h4 id="sync.28function.29" name="sync.28function.29"><code>sync(<var>function</var>)</code></h4> -<p>creates a synchronized function (in the sense of a Java <code>synchronized</code> method) from an existing function. The new function synchronizes on the <code>this</code> object of its invocation.</p> -<h4 id="quit.28.29" name="quit.28.29"><code>quit()</code></h4> -<p>退出shell. 如果文件字符是在提示符下键入,shell将也退出在交互模式下.</p> -<h4 id="version.28.5Bnumber.5D.29" name="version.28.5Bnumber.5D.29"><code>version([<var>number</var>])</code></h4> -<p>设置或获取JavaScript的版本号. 如果没有参数,就返回当前的版本. 如果有参数,参数要在 <code>100</code>, <code>110</code>, <code>120</code>, <code>130</code>, <code>140</code>, <code>150</code>, <code>160 或 170范围内,它们分别对应</code> JavaScript1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6 or 1.7.</p> -<h3 id="Examples" name="Examples">例子</h3> -<h4 id="Invocation" name="Invocation">调用</h4> -<p>Here the shell is invoked three times from the command line. (The system command prompt is shown as <code>$</code>.) The first invocation executes a script specified on the command line itself. The next invocation has no arguments, so the shell goes into interactive mode, reading and evaluating each line as it is typed in. Finally, the last invocation executes a script from a file and accesses arguments to the script itself.</p> -<pre>$ java org.mozilla.javascript.tools.shell.Main -e print('hi') -hi -$ java org.mozilla.javascript.tools.shell.Main -js> print('hi') -hi -js> 6*7 -42 -js> function f() { - return a; -} -js> var a = 34; -js> f() -34 -js> quit() -$ cat echo.js -for (i in arguments) { - print(arguments[i]) -} -$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar -foo -bar -$ -</pre> -<h4 id="spawn_and_sync" name="spawn_and_sync"><code>spawn</code> 和 sync </h4> -<p>下面的例子创建了2个线程 via <code>spawn</code> 和 uses <code>sync</code> 去创建同步的函数test <code>test版本.</code></p> -<pre>js> function test(x) { - print("entry"); - java.lang.Thread.sleep(x*1000); - print("exit"); -} -js> var o = { f : sync(test) }; -js> spawn(function() {o.f(5);}); -Thread[Thread-0,5,main] -entry -js> spawn(function() {o.f(5);}); -Thread[Thread-1,5,main] -js> -exit -entry -exit -</pre> -<h4 id="runCommand" name="runCommand"><code>运行命令</code></h4> -<p>这是在Linux下调用 <code>runCommand</code> 的例子.</p> -<pre>js> runCommand('date') -Thu Jan 23 16:49:36 CET 2003 -0 -// Using input option to provide process input -js> runCommand("sort", {input: "c\na\nb"}) -a -b -c -0 -js> // Demo of output and err options -js> var opt={input: "c\na\nb", output: 'Sort Output:\n'} -js> runCommand("sort", opt) -0 -js> print(opt.output) -Sort Output: -a -b -c -js> var opt={input: "c\na\nb", output: 'Sort Output:\n', err: ''} -js> runCommand("sort", "--bad-arg", opt) -2 -js> print(opt.err) -/bin/sort: unrecognized option `--bad-arg' -Try `/bin/sort --help' for more information. - -js> runCommand("bad_command", "--bad-arg", opt) -js: "<stdin>", line 18: uncaught JavaScript exception: java.io.IOException: bad_command: not found -js> // Passing explicit environment to the system shell -js> runCommand("sh", "-c", "echo $env1 $env2", { env: {env1: 100, env2: 200}}) -100 200 -0 -js> // Use args option to provide additional command arguments -js> var arg_array = [1, 2, 3, 4]; -js> runCommand("echo", { args: arg_array}) -1 2 3 4 -0 -</pre> -<p>Windows下的例子也差不多:</p> -<pre>js> // Invoke shell command -js> runCommand("cmd", "/C", "date /T") -27.08.2005 -0 -js> // Run sort collectiong the output -js> var opt={input: "c\na\nb", output: 'Sort Output:\n'} -js> runCommand("sort", opt) -0 -js> print(opt.output) -Sort Output: -a -b -c -js> // Invoke notepad and wait until it exits -js> runCommand("notepad") -0 -</pre> -<p>{{ languages( { "zh-cn": "zh-cn/Shell" } ) }}</p> |