这篇翻译不完整。请帮忙从英语翻译这篇文章

JavaScript shell 是一个命令行程序,它被包含在SpiderMonkey源代码中。它在JavaScript中类似于Python的交互式提示符(即Python的命令行工具),Lisp中的读取执行打印循环工具,或者想Ruby中的irb。这篇文章解释如何使用shell去测试JavaScript代码,并且执行JavaScript程序。

得到 SpiderMonkey JavaScript shell, 参考SpiderMonkey Build Documentation or 根据你的平台下载二进制文件 Nightly Builds.

For a list of other JavaScript shells, see JavaScript shells.

Note: Starting with SpiderMonkey 44 (Firefox 44 / Thunderbird 44 / SeaMonkey 2.41), the standard, Web-compatible JavaScript version is used by default (and not JS1.7+ anymore). The version() shell builtin is still there for testing legacy features.

执行JavaScript shell

shell提供部分两种模式的操作 . 你既可以交互式操作shell, 在此你迅速输入JavaScript 代码并获得即刻的喜悦, 便于实验和测试新特性. 你也可以登陆, 在 command line, 运行一个JavaScript program file, 在这种情况下这个program自动运行.

After following the build documentation and installing the built shell using make install, you can run the shell in interactive mode using the command:

js

[ If you get " symbol lookup error: ./js: undefined symbol: PR_SetCurrentThreadName" e.g. from a Bash console when using a pre-compiled binary, try   /run-mozilla.sh ./js   -- that worked for me]

如果运行 foo.js file中的JavaScript代码,you can use this command、、、、、:

js foo.js

j运行 foo.js 然后进入interactive shell, do this:

js -f foo.js -i

说明

Note: 因为JavaScript shell 用来给 JavaScript 引擎做测试环境, 可获得的选择 和 内置函数可以随时改变.

Command line options

有许多command line 选择你可以指定去控制shell. 以下为总结. 确保用 -h 用你的 jsshell 去检查是否有未记录的.

-c, --compileonly
Tells the shell to  编译却不运行program. 这是一种方便的方式检查语法错误在你的program而不用真正running.
-e script
运行指定脚本, which is 一个字符串包含代码去执行.
-f filename
Runs the JavaScript program 指定文件名.
-i
使 交互模式有效. (Default if no filename is provided.)
--no-ion
    优化 JIT compiler无效.
--no-baseline
使底线 JIT compiler无效.
-P
如果文件的第一行是 "/usr/bin/env js -P",然后文件内容将被解释通过 JavaScript 引擎.
这使你能够 使 JavaScript file 可执行在 unix and OS X machines.
-s
Enables 严格的警告模式.
-w, --warnings
Enables 警告消息.
-W, --nowarnings
Disables warning messages.

环境选项

有一些环境变量能够被设置去修改 js shell行为.

JS_STDOUT=file
将标准输出重定向到文件.
JS_STDERR=file
将标准输入重定向到文件.

内置方法

To make the JavaScript shell more useful, there are a number of built-in functions provided that you can use either from JavaScript programs or in interactive mode.

Note: 这个列表是不完整的且与 Shell global objects交叉. See js/src/shell/js.cpp (around shell_functions) for more.

build()

JavaScript shell建立的日期和时间.

Note: clear() 不带参数是清楚全部.包括所有这些内置方法.

clone(function, [scope])

克隆指定函数对象. 如果未指定具体范围, 新对象的父本和原始对象的相同. 否则,新对象被放置于指定范围.

countHeap([start[, kind]])

Added in SpiderMonkey 1.8  计数堆中的动态GC, or 从开始时可到达的 当它被给出且不是空的. kind is either 'all' (default) to count all things or one of 'object', 'double', 'string', 'function', 'qname', 'namespace', 'xml' to count only things of that kind.

dumpHeap([fileName[, start[, toFind[, maxDepth[, toIgnore]]]]])

Added in SpiderMonkey 1.8 转储所有现存对象的图形 (or 一个具体的有趣的子图) 到文件. For more information, see the C/C++ version of this function, JS_DumpHeap.

evalcx(string[, object])

用字符串评估JavaScript代码. 如果对象被指定,代码将在该对象中执行,将其视为sandbox.

If 字符串是空的且对象不具体, evalcx() 返回一个新对象 with eager standard classes.

If 字符串是"lazy" 且对象不具体, evalcx() 返回一个新对象 with lazy standard classes.

Note: evalcx() is only useful for people doing deep internal work on the JavaScript engine, for testing evalInSandbox-like environments in the shell.

gc()

运行垃圾收集器释放内存

gcparam(name[, value])

Added in SpiderMonkey 1.8 读取或配置垃圾收集器参数.

名称必须是参数key之一 (such as 'maxBytes', 'maxMallocBytes' or 'gcNumber').

如果没有指定值,gcparam()返回与GC参数名相关联的当前值

如果被指定值,则必须将其转换为正的uint32;gcparam()将GC参数名设置为。

For more information, see the C/C++ functions  JS_SetGCParameter and JS_SetGCParameter.

gczeal(level)

Added in SpiderMonkey 1.8 DEBUG only. 设置级别 of GC zeal, 这是一个调试特性.      0 对于正常的周期性垃圾收集, 1 对于非常频繁的GC, or 2 对于及其频繁的GC. 除了0以外的都将使JavaScript运行极其缓慢但是可能有助于揭露和再生 GC-相关的 bugs. For more information, see the C/C++ version of this function, JS_SetGCZeal.

getpda(object)

返回指定对象的属性描述

getslx(object)

返回脚本行范围,这是包含指定对象的代码行数

help([command ...])

Displays brief help information about the specified commands, or about all available functions if none are specified.

intern(string)

将指定的字符串 内化到atom表中。每个字符串都有一个唯一的标识符,称为atom。这个系统使得对字符串进行比较变得更加容易。

Note: This function is intended only for use when testing the JavaScript engine.

line2pc([function, ] line)

返回对应于指定行的代码行的程序计数器值。如果指定了函数,那么行就是指定函数的偏移量.

load(filename1 [filename])

 

加载带有指定名称的JavaScript文件.

Note: For loading non-JavaScript files, use read().

options([option ...])

让你设置或获取选项。如果您在命令行上指定选项,那么调用options的结果将指示您所请求的选项。您还可以传递新的选项来设置.

The available options are:

Option Name Description
strict 严格模式 is enabled.
werror Warnings should be treated as errors.
atline When atline is enabled, 表单的注释 //@line num set the number of the following line to num.

pc2line(function, [pc])

返回对应于指定函数的第一行的JavaScript代码的行号. If you specify a program counter offset into the function, the line number of the line of code containing that offset is returned.

print([expression ...])

Evaluates the expression(s) and displays the result(s) on stdout, 分隔 by spaces (" ") and 终止 by a newline ("\n").

putstr(expression)

Evaluates the expression and displays the result on stdout.

quit([status])

Exits the shell. 如果省略,状态默认为0。

read(filename[, type])

Reads and returns the contents of file. If type is "binary" returns an Uint8Array, otherwise returns an UTF-8 decoded string.

readline()

Reads a single line of input from stdin, returning it to the caller. You can use this to create interactive shell programs in JavaScript.

Reflect.parse()

See Parser API.

Note: This function is intended only for use when testing the JavaScript engine.

seal(object[, deep])

密封指定的对象, or an object graph if deep is true. 通过封住对象或对象图,可以禁用这些对象的修改.

sleep(dt)

Added in SpiderMonkey 1.8 Only in JS_THREADSAFE builds. Sleep for dt seconds. Fractions of a second are supported. Returns true on success, false if the sleep was interrupted.

stackQuota([number])

Added in SpiderMonkey 1.8 获取或设置脚本堆栈配额.

throwError()

Throws an error from the JS_ReportError() function.

Note: This function is intended only for use when testing the JavaScript engine.

trap([function, [pc,]] expression)

在JavaScript代码的特定点设置trap. 当由pc在函数函数中指定的偏移量即将被执行时,表达式将被求值。

这是一种强大的调试机制,与line2pc()一起使用. 例如, 如果你想在函数的第6行中显示一条消息, doSomething() 被执行, you can enter the following:

trap(doSomething, line2pc(doSomething, 6), "print('line 6!\n')");
Note: 当设置陷阱时,程序中的相应字节码将被trap字节码替换,直到您使用untrap() 来移除陷阱。

untrap(function [, pc])

Removes a trap from the specified function at the offset pc. If pc isn't specified, the trap is removed from the function's entry point.

This function has no effect if there is no trap at the specified location.

version([number])

The version() function lets you get or set the JavaScript version number. This may be useful for gaining access to syntax only available in certain versions of JavaScript (for example, see Using JavaScript 1.7).

调试方法

These built-in functions are only available in DEBUG builds.

dis([function])

为整个程序或指定的函数分解JavaScript字节码

For example, if you enter the JavaScript function below:

function test() {
  var i = 3;
  print(i+2);
}

Then run the command dis(test);, you get this output:

main:
00000:  uint16 3
00003:  setvar 0
00006:  pop
00007:  name "print"
00010:  pushobj
00011:  getvar 0
00014:  uint16 2
00017:  add
00018:  call 1
00021:  pop
00022:  stop

Source notes:
  0:     0 [   0] newline 
  1:     3 [   3] decl     offset 0
  2:     7 [   4] newline 
  3:    18 [  11] xdelta  
  4:    18 [   0] pcbase   offset 11

dissrc([function])

Disassembles the JavaScript bytecode for the entire program, or for the specified function, showing the source lines. This function only works with programs loaded from files, either using the -f flag on launching the shell, or by using the load() function.

If your program includes a function, doStuff(), like this:

function doStuff(input) {
	print("Enter a number: ");
	var n1 = readline();
	print("Enter another one: ");
	var n2 = readline();
	
	print("You entered " + n1 + " and " + n2 + "\n");
}

Calling dissrc(doStuff) function would give this output:

;-------------------------  10:         print("Enter a number: ");
00000:  10  name "print"
00003:  10  pushobj
00004:  10  string "Enter a number: "
00007:  10  call 1
00010:  10  pop
;-------------------------  11:         var n1 = readline();
00011:  11  name "readline"
00014:  11  pushobj
00015:  11  call 0
00018:  11  setvar 0
00021:  11  pop
;-------------------------  12:         print("Enter another one: ");
00022:  12  name "print"
00025:  12  pushobj
00026:  12  string "Enter another one: "
00029:  12  call 1
00032:  12  pop
;-------------------------  13:         var n2 = readline();
00033:  13  name "readline"
00036:  13  pushobj
00037:  13  call 0
00040:  13  setvar 1
00043:  13  pop
;-------------------------  14: 
;-------------------------  15:         print("You entered " + n1 + " and " + n2 + "\n");
00044:  15  name "print"
00047:  15  pushobj
00048:  15  string "You entered "
00051:  15  getvar 0
00054:  15  add
00055:  15  string " and "
00058:  15  add
00059:  15  getvar 1
00062:  15  add
00063:  15  string "\\n"
00066:  15  add
00067:  15  call 1
00070:  15  pop
00071:  15  stop

dumpheap(([fileName[, start[, toFind[, maxDepth[, toIgnore]]]]])

Dump GC information. This is a thin wrapper for JS_DumpHeap.

gczeal(zeal)

Enable extra-frequent GC, to help find GC hazards. zeal is an integer. The meaning is the same as for the parameter to JS_SetGCZeal.

notes([function])

Shows the source notes for the specified function. Source notes contain information that map the bytecode to the source code, which is used when decompiling the code, such as when using the dissrc() function.

文档标签和贡献者

标签: 
此页面的贡献者: fscholz, ricoeo, lampui, Gaohaoyang, pokerqueen, ziyunfei, zmlovect
最后编辑者: fscholz,