Introduction to the JavaScript shell

  • 리비전 슬러그: Introduction_to_the_JavaScript_shell
  • 리비전 제목: Introduction to the JavaScript shell
  • 리비전 아이디: 195923
  • 제작일시:
  • 만든이: Heyjin
  • 현재 리비전인가요? 아니오
  • 댓글 /* Building a trunk version of JavaScript */

리비전 내용

들어가며

이 문서는 모질라 CVS 서버에서 자바스크립트 셸을 가져와 빌드하는 방법을 소개하고 있습니다. 또한 시험삼아 개발 버전을 빌드하는 방법에도 관심을 기울이고 있습니다.

게다가 이 문서는 쉘에서 자바스크립트 코드를 테스트하고 자바스크립트로 작성한 프로그램을 실행하는 방법에 대한 간단한 소개도 포함하고 있습니다.

자바스크립트 셸 설치하기

CVS 서버에 접속합시다

CVS에서 다른 모질라와 관련된 프로젝트의 산물을 얻는 것과 마찬가지로 자바스크립트 셸을 사용하려면 먼저 CVS 서버에 로그인해야 합니다. <tt>cd</tt> 명령을 사용하여 코드를 체크아웃할 기본 디렉토리로 들어가, 다음과 같은 명령어를 명령행에 입력합니다.

cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot login

프롬프트가 뜨면 패스워드를 입력합니다. <tt>anonymous</tt> 라고 입력하면 됩니다.

자바스크립트 셸 안정버전 설치하기

한번 CVS 서버에 로그인하였으면 필요한 파일을 받아오고, 패치된 내용도 바로 반영하여 최신 버전의 파일로 작업이나 설치를 할 수 있어야 합니다. CVS 트리의 루트 디렉토리로 들어가 다음과 같이 명령을 입력합니다.

cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -l mozilla/js/src mozilla/js/src/config mozilla/js/src/editline mozilla/js/src/fdlibm

자바스크립트 셸을 설치하는 데 필요한 모든 파일을 체크아웃하였습니다. 이제 다음과 같은 명령 두개만 입력하면 바로 설치할 수 있습니다:

cd mozilla/js/src
make -f Makefile.ref

컴파일이 끝나면 우리가 설치한 디렉토리에 실행할 수 있는 파일인 <tt>js</tt>가 들어 있어야 합니다. 예를 들어 Mac OS X에서라면, 실행 파일은 <tt>Darwin_DBG.OBJ/js</tt>의 경로에 놓일 것입니다.

이 상태에서 바로 자바스크립트 셸 사용하기를 실습해 볼 수도 있습니다.

Building a branch version of JavaScript

If you want to experiment with a specific branch's version of JavaScript, you can easily do so by adding -r branchname to the command line when checking out the files.

Follow all the same steps as before, but when checking out the files, change the <tt>cvs co...</tt> line to:

cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -l -r branch_name mozilla/js/src mozilla/js/src/config mozilla/js/src/editline mozilla/js/src/fdlibm

Change branch_name to the name of the branch you want to check out. For example, to check out the JavaScript 1.7 alpha branch, you should specify <tt>JS_1_7_ALPHA_BRANCH</tt>.

Then you can compile and run the shell as before.

자바스크립트 셸 사용하기

The shell offers two modes of operation. You can use it as an interactive shell, in which you type JavaScript code at a prompt and get instant gratification, which is handy for experimenting or testing new features. You can also pass in, on the command line, a JavaScript program file to run, in which case the program is run automatically.

Note: Because the JavaScript shell is used as a test environment for the JavaScript engine, the available options and built-in functions can change over time.

Command line options

There are a number of command line options you can specify to control the shell. These are summarized below.

  • -b branchlimit
    • Sets the branch limit.
  • -c stackchunksize
    • Sets the stack chunk size.
  • -C
    • Tells the shell to compile the program but not run it. This is a convenient way to quickly check for syntax errors in your program without actually running it.
  • -e script
    • Runs the specified script, which is a literal string containing the code to execute.
  • -f filename
    • Runs the JavaScript program specified by filename.
  • -i
    • Enables interactive mode.
  • -P
    • I'm not sure yet what this does.
  • -s
    • Enables strict warning mode.
  • -S stacksize
    • Sets the maximum stack size.
  • -v
    • Displays usage information.
  • -w
    • Enables warning messages.
  • -W
    • Disables warning messages.
  • -x
    • Enables E4X XML mode.

Running the shell

If you want to run the shell in interactive mode, you can simply use the command:

js

If you'd like to run the JavaScript code in the file <tt>foo.js</tt>, you can use this command:

js -f foo.js

To run <tt>foo.js</tt> then drop into the interactive shell, do this:

js -f foo.js -f -

Using the shell in interactive mode

In interactive mode, you can enter JavaScript code by hand to create objects and functions, as well as individual statements. This is a convenient way to test out ideas and, more importantly to developers working on the JavaScript engine, test new features of the language.

Built-in functions

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.

build()

Returns the date and time at which the JavaScript shell was built.

clear([object])

Clears properties of the specified object. Calling clear() with no parameter clears everything so you can start with a clean slate.

Note: clear() with no parameters really clears everything. This includes all these built-in functions.

clone(function, [scope])

Clones the specified function object. If scope isn't specified, the new object's parent is the same as the original object's. Otherwise, the new object is placed in the scope of the object specified by scope.

dis([function])

Disassembles the JavaScript bytecode for the entire program, or for the specified function.

For example, if you enter the JavaScript function below:

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

Then run the command <tt>dis(test);</tt>, 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] var     
  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

evalcx(string[, object])

Evaluates the JavaScript code in string. If object is specified, the code is executed in that object, treating it as a sandbox.

If string is empty and object is not specified, evalcx() returns a new object with eager standard classes.

If string is "lazy" and object is not specified, evalcx() returns a new object 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()

가비지 콜렉터를 실행하여 메모리를 깨끗이 비웁니다.

getpda(object)

Returns the property descriptors for the specified object.

getslx(object)

Returns the script line extent, which is the number of lines of code comprising the specified object.

help([command ...])

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

intern(string)

Internalizes the specified string into the atom table. Every string has a unique identifier, called an atom. This system makes it easier to do comparisons between strings.

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

line2pc([function, ] line)

Returns the program counter value corresponding to the specified line of code. If function is specified, line is an offset into the specified function.

load(filename1 [filename])

Loads the files with the specified names.

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.

options([option ...])

Lets you set or get options. If you specified options on the command line, the results of calling options will indicate which options you requested. You can also pass in new options to set.

If you start the shell with the command <tt>js -x</tt>, then the options() function will return <tt>xml</tt>. If you start the shell with no options, and decide you want to enable XML mode, you can do so by issuing the command:

options('xml');

The available options are:

Option Name Description
strict Strict mode is enabled.
werror Warnings should be treated as errors.
atline When atline is enabled, comments of the form //@line num set the number of the following line to num.
xml XML mode is enabled.

pc2line(function, [pc])

Returns the line number of the JavaScript code that corresponds to the first line of the specified function. 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 and displays the result on <tt>stdout</tt>.

quit()

Exits the shell.

readline()

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

seal(object[, deep])

Seals the specified object, or an object graph if deep is true. By sealing an object or object graph, you disable modification of those objects.

stats([string ...])

Dumps statistics. Valid options are arena, atom, and global.

Option Description
arena Outputs the arena table.
atom Outputs the atom table.
global Outputs the global name table.

An arena is a large block of memory from which the JavaScript engine allocates sub-blocks in order to optimize performance; issuing a large number of malloc() calls for each individual block is inefficient, so the engine creates arenas, then uses an internal memory management system to allocate memory from within those. The arena table is a list of all the arenas that have been allocated by the shell. stats("arena") lets you inspect the arena table so that you can see how a test case uses arenas.

An atom is a unique identifier for a string. In order to optimize comparisons between strings, each string is given an atom. These atoms are stored in a hash table, which you can display using the command stats("atom").

stats("global") displays the global name table, which contains the names and information about each object in memory.

stringsAreUtf8()

Returns true if strings are encoded in UTF8 format; otherwise, returns false.

testUtf8(mode)

Performs UTF-8 tests. The mode parameter may be an integer from 1 to 4.

This is a debugging command that simply runs some checks on UTF8 string handling, and isn't of general use, unless you're working on the JavaScript engine itself.

throwError()

Throws an error from the JS_ReportError() function.

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

tracing([toggle])

Enables or disables tracing mode. Pass true to enable tracing or false to turn it off. If you don't specify a parameter, tracing() returns the current setting.

Tip: This only works if you build JavaScript with JS_THREADED_INTERP turned off in the file <tt>jsinterp.c</tt>.

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

Sets a trap at the specific point in the JavaScript code. When the bytecode at the offset specified by pc in the function function is about to be executed, the expression is evaluated.

This is a powerful debugging mechanism when used in concert with line2pc(). For example, if you want to display a message when line 6 of a function, doSomething() is executed, you can enter the following:

trap(doSomething, line2pc(test, 6), "print('line 6!\n')");
Note: When a trap is set, the corresponding bytecode in the program is replaced with a trap bytecode until you use untrap() to remove the trap.

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])

version() 함수는 자바스크립트 버전을 반환합니다. 그다지 쓸 일이 있는 함수는 아니지요.

리비전 소스

<h2 name=".EB.93.A4.EC.96.B4.EA.B0.80.EB.A9.B0">들어가며</h2>
<p>이 문서는 모질라 CVS 서버에서 자바스크립트 셸을 가져와 빌드하는 방법을 소개하고 있습니다. 또한 시험삼아 개발 버전을 빌드하는 방법에도 관심을 기울이고 있습니다.
</p><p>게다가 이 문서는 쉘에서 자바스크립트 코드를 테스트하고 자바스크립트로 작성한 프로그램을 실행하는 방법에 대한 간단한 소개도 포함하고 있습니다.
</p>
<h2 name=".EC.9E.90.EB.B0.94.EC.8A.A4.ED.81.AC.EB.A6.BD.ED.8A.B8_.EC.85.B8_.EC.84.A4.EC.B9.98.ED.95.98.EA.B8.B0">자바스크립트 셸 설치하기</h2>
<h3 name="CVS_.EC.84.9C.EB.B2.84.EC.97.90_.EC.A0.91.EC.86.8D.ED.95.A9.EC.8B.9C.EB.8B.A4">CVS 서버에 접속합시다</h3>
<p>CVS에서 다른 모질라와 관련된 프로젝트의 산물을 얻는 것과 마찬가지로 자바스크립트 셸을 사용하려면 먼저 CVS 서버에 로그인해야 합니다. <tt>cd</tt> 명령을 사용하여 코드를 체크아웃할 기본 디렉토리로 들어가, 다음과 같은 명령어를 명령행에 입력합니다.
</p>
<pre class="eval">cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot login
</pre>
<p>프롬프트가 뜨면 패스워드를 입력합니다. <tt>anonymous</tt> 라고 입력하면 됩니다.
</p>
<h3 name=".EC.9E.90.EB.B0.94.EC.8A.A4.ED.81.AC.EB.A6.BD.ED.8A.B8_.EC.85.B8_.EC.95.88.EC.A0.95.EB.B2.84.EC.A0.84_.EC.84.A4.EC.B9.98.ED.95.98.EA.B8.B0">자바스크립트 셸 안정버전 설치하기</h3>
<p>한번 CVS 서버에 로그인하였으면 필요한 파일을 받아오고, 패치된 내용도 바로 반영하여 최신 버전의 파일로 작업이나 설치를 할 수 있어야 합니다. CVS 트리의 루트 디렉토리로 들어가 다음과 같이 명령을 입력합니다.
</p>
<pre>cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -l mozilla/js/src mozilla/js/src/config mozilla/js/src/editline mozilla/js/src/fdlibm
</pre>
<p>자바스크립트 셸을 설치하는 데 필요한 모든 파일을 체크아웃하였습니다. 이제 다음과 같은 명령 두개만 입력하면 바로 설치할 수 있습니다:
</p>
<pre>cd mozilla/js/src
make -f Makefile.ref
</pre>
<p>컴파일이 끝나면 우리가 설치한 디렉토리에 실행할 수 있는 파일인 <tt>js</tt>가 들어 있어야 합니다. 예를 들어 Mac OS X에서라면, 실행 파일은 <tt>Darwin_DBG.OBJ/js</tt>의 경로에 놓일 것입니다.
</p><p>이 상태에서 바로 <a href="#Using_the_JavaScript_shell">자바스크립트 셸 사용하기</a>를 실습해 볼 수도 있습니다.
</p>
<h3 name="Building_a_branch_version_of_JavaScript">Building a branch version of JavaScript</h3>
<p>If you want to experiment with a specific branch's version of JavaScript,  you can easily do so by adding <code>-r <i>branchname</i></code> to the command line when checking out the files.
</p><p>Follow all the same steps as before, but when checking out the files, change the <tt>cvs co...</tt> line to:
</p>
<pre class="eval">cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -l -r <i>branch_name</i> mozilla/js/src mozilla/js/src/config mozilla/js/src/editline mozilla/js/src/fdlibm
</pre>
<p>Change <i>branch_name</i> to the name of the branch you want to check out.  For example, to check out the JavaScript 1.7 alpha branch, you should specify <tt>JS_1_7_ALPHA_BRANCH</tt>.
</p><p>Then you can compile and run the shell as before.
</p>
<h2 name=".EC.9E.90.EB.B0.94.EC.8A.A4.ED.81.AC.EB.A6.BD.ED.8A.B8_.EC.85.B8_.EC.82.AC.EC.9A.A9.ED.95.98.EA.B8.B0">자바스크립트 셸 사용하기</h2>
<p>The shell offers two modes of operation.  You can use it as an interactive shell, in which you type JavaScript code at a prompt and get instant gratification, which is handy for experimenting or testing new features.  You can also pass in, on the command line, a JavaScript program file to run, in which case the program is run automatically.
</p>
<div class="note"><b>Note:</b> Because the JavaScript shell is used as a test environment for the JavaScript engine, the available options and built-in functions can change over time.</div>
<h3 name="Command_line_options">Command line options</h3>
<p>There are a number of command line options you can specify to control the shell.  These are summarized below.
</p>
<ul><li> <code>-b <i>branchlimit</i></code>
<ul><li> Sets the branch limit.
</li></ul>
</li><li> <code>-c <i>stackchunksize</i></code>
<ul><li> Sets the stack chunk size.
</li></ul>
</li><li> <code>-C</code>
<ul><li> Tells the shell to compile the program but not run it.  This is a convenient way to quickly check for syntax errors in your program without actually running it.
</li></ul>
</li><li> <code>-e <i>script</i></code>
<ul><li> Runs the specified <i>script</i>, which is a literal string containing the code to execute.
</li></ul>
</li><li> <code>-f <i>filename</i></code>
<ul><li>Runs the JavaScript program specified by <i>filename</i>.
</li></ul>
</li><li> <code>-i</code>
<ul><li> Enables interactive mode.
</li></ul>
</li><li> <code>-P</code>
<ul><li> <i>I'm not sure yet what this does.</i>
</li></ul>
</li><li> <code>-s</code>
<ul><li> Enables strict warning mode.
</li></ul>
</li><li> <code>-S <i>stacksize</i></code>
<ul><li> Sets the maximum stack size.
</li></ul>
</li><li> <code>-v</code>
<ul><li> Displays usage information.
</li></ul>
</li><li><code> -w</code>
<ul><li> Enables warning messages.
</li></ul>
</li><li> <code>-W</code>
<ul><li> Disables warning messages.
</li></ul>
</li><li> <code>-x</code>
<ul><li> Enables E4X XML mode.
</li></ul>
</li></ul>
<h3 name="Running_the_shell">Running the shell</h3>
<p>If you want to run the shell in interactive mode, you can simply use the command:
</p>
<pre class="eval">js
</pre>
<p>If you'd like to run the JavaScript code in the file <tt>foo.js</tt>, you can use this command:
</p>
<pre class="eval">js -f foo.js
</pre>
<p>To run <tt>foo.js</tt> then drop into the interactive shell, do this:
</p>
<pre class="eval">js -f foo.js -f -
</pre>
<h4 name="Using_the_shell_in_interactive_mode">Using the shell in interactive mode</h4>
<p>In interactive mode, you can enter JavaScript code by hand to create objects and functions, as well as individual statements.  This is a convenient way to test out ideas and, more importantly to developers working on the JavaScript engine, test new features of the language.
</p>
<h3 name="Built-in_functions">Built-in functions</h3>
<p>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.
</p>
<h4 name="build.28.29"><code>build()</code></h4>
<p>Returns the date and time at which the JavaScript shell was built.
</p>
<h4 name="clear.28.5Bobject.5D.29"><code>clear(<i>[object]</i>)</code></h4>
<p>Clears properties of the specified <var>object</var>.  Calling <code>clear()</code> with no parameter clears everything so you can start with a clean slate.
</p>
<div class="note"><b>Note:</b> <code>clear()</code> with no parameters really clears everything.  This includes all these built-in functions.</div>
<h4 name="clone.28function.2C_.5Bscope.5D.29"><code>clone(<i>function, [scope]</i>)</code></h4>
<p>Clones the specified <var>function</var> object.  If <var>scope</var> isn't specified, the new object's parent is the same as the original object's.  Otherwise, the new object is placed in the scope of the object specified by <var>scope</var>.
</p>
<h4 name="dis.28.5Bfunction.5D.29"><code>dis(<i>[function]</i>)</code></h4>
<p>Disassembles the JavaScript bytecode for the entire program, or for the specified <i>function</i>.
</p><p>For example, if you enter the JavaScript function below:
</p>
<pre>function test() {
  var i = 3;
  print(i+2);
}
</pre>
<p>Then run the command <tt>dis(test);</tt>, you get this output:
</p>
<pre>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] var     
  2:     7 [   4] newline 
  3:    18 [  11] xdelta  
  4:    18 [   0] pcbase   offset 11
</pre>
<h4 name="dissrc.28.5Bfunction.5D.29"><code>dissrc(<i>[function]</i>)</code></h4>
<p>Disassembles the JavaScript bytecode for the entire program, or for the specified <i>function</i>, showing the source lines.  This function only works with programs loaded from files, either using the <code>-f</code> flag on launching the shell, or by using the <code>load()</code> function.
</p><p>If your program includes a function, <code>doStuff()</code>, like this:
</p>
<pre>function doStuff(input) {
	print("Enter a number: ");
	var n1 = readline();
	print("Enter another one: ");
	var n2 = readline();
	
	print("You entered " + n1 + " and " + n2 + "\n");
}
</pre>
<p>Calling <code>dissrc(doStuff)</code> function would give this output:
</p>
<pre>;-------------------------  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
</pre>
<h4 name="evalcx.28string.5B.2C_object.5D.29"><code>evalcx(<i>string[, object]</i>)</code></h4>
<p>Evaluates the JavaScript code in <i>string</i>.  If <i>object</i> is specified, the code is executed in that object, treating it as a sandbox.
</p><p>If <i>string</i> is empty and <i>object</i> is not specified, <code>evalcx()</code> returns a new object with eager standard classes.
</p><p>If <i>string</i> is "lazy" and <i>object</i> is not specified, <code>evalcx()</code> returns a new object with lazy standard classes.
</p>
<div class="note"><b>Note:</b> <code>evalcx()</code> is only useful for people doing deep internal work on the JavaScript engine, for testing <code>evalInSandbox</code>-like environments in the shell.</div>
<h4 name="gc.28.29"><code>gc()</code></h4>
<p>가비지 콜렉터를 실행하여 메모리를 깨끗이 비웁니다.
</p>
<h4 name="getpda.28object.29"><code>getpda(<i>object</i>)</code></h4>
<p>Returns the property descriptors for the specified <i>object</i>.
</p>
<h4 name="getslx.28object.29"><code>getslx(<i>object</i>)</code></h4>
<p>Returns the script line extent, which is the number of lines of code comprising the specified object.
</p>
<h4 name="help.28.5Bcommand_....5D.29"><code>help(<i>[command ...]</i>)</code></h4>
<p>Displays brief help information about the specified commands, or about all available functions if none are specified.
</p>
<h4 name="intern.28string.29"><code>intern(<i>string</i>)</code></h4>
<p>Internalizes the specified <var>string</var> into the atom table.  Every string has a unique identifier, called an atom.  This system makes it easier to do comparisons between strings.
</p>
<div class="note"><b>Note:</b> This function is intended only for use when testing the JavaScript engine.</div>
<h4 name="line2pc.28.5Bfunction.2C_.5D_line.29"><code>line2pc(<i>[function, ] line</i>)</code></h4>
<p>Returns the program counter value corresponding to the specified <i>line</i> of code.  If <i>function</i> is specified, <i>line</i> is an offset into the specified function.
</p>
<h4 name="load.28filename1_.5Bfilename.5D.29"><code>load(<i>filename1</i> <i>[filename]</i>)</code></h4>
<p>Loads the files with the specified names.
</p>
<h4 name="notes.28.5Bfunction.5D.29"><code>notes(<i>[function]</i>)</code></h4>
<p>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 <code>dissrc()</code> function.
</p>
<h4 name="options.28.5Boption_....5D.29"><code>options(<i>[option ...]</i>)</code></h4>
<p>Lets you set or get options.  If you specified options on the command line, the results of calling <code>options</code> will indicate which options you requested.  You can also pass in new options to set.
</p><p>If you start the shell with the command <tt>js -x</tt>, then the <code>options()</code> function will return <tt>xml</tt>.  If you start the shell with no options, and decide you want to enable XML mode, you can do so by issuing the command:
</p>
<pre class="eval">options('xml');
</pre>
<p>The available options are:
</p>
<table class="standard-table">

<tbody><tr>
<td class="header">Option Name
</td><td class="header">Description
</td></tr>

<tr>
<td><code>strict</code>
</td><td> Strict mode is enabled.
</td></tr>
<tr>
<td><code>werror</code>
</td><td> Warnings should be treated as errors.
</td></tr>
<tr>
<td><code>atline</code>
</td><td> When <code>atline</code> is enabled, comments of the form <code>//@line <i>num</i></code> set the number of the following line to <code><i>num</i></code>.
</td></tr>
<tr>
<td><code>xml</code>
</td><td> XML mode is enabled.
</td></tr>
</tbody></table>
<h4 name="pc2line.28function.2C_.5Bpc.5D.29"><code>pc2line(<i>function, [pc]</i>)</code></h4>
<p>Returns the line number of the JavaScript code that corresponds to the first line of the specified <i>function</i>.  If you specify a program counter offset into the function, the line number of the line of code containing that offset is returned.
</p>
<h4 name="print.28.5Bexpression_....5D.29"><code>print(<i>[expression ...]</i>)</code></h4>
<p>Evaluates the <i>expression</i> and displays the result on <tt>stdout</tt>.
</p>
<h4 name="quit.28.29"><code>quit()</code></h4>
<p>Exits the shell.
</p>
<h4 name="readline.28.29"><code>readline()</code></h4>
<p>Reads a single line of input from <tt>stdin</tt>, returning it to the caller.  You can use this to create interactive shell programs in JavaScript.
</p>
<h4 name="seal.28object.5B.2C_deep.5D.29"><code>seal(<i>object[, deep]</i>)</code></h4>
<p>Seals the specified <i>object</i>, or an object graph if <i>deep</i> is <code>true</code>.  By sealing an object or object graph, you disable modification of those objects.
</p>
<h4 name="stats.28.5Bstring_....5D.29"><code>stats(<i>[string ...]</i>)</code></h4>
<p>Dumps statistics.  Valid options are <i>arena</i>, <i>atom</i>, and <i>global</i>.
</p>
<table class="standard-table">

<tbody><tr>
<td class="header">Option
</td><td class="header">Description
</td></tr>

<tr>
<td><code>arena</code>
</td><td> Outputs the arena table.
</td></tr>
<tr>
<td><code>atom</code>
</td><td> Outputs the atom table.
</td></tr>
<tr>
<td><code>global</code>
</td><td> Outputs the global name table.
</td></tr>
</tbody></table>
<p>An <i>arena</i> is a large block of memory from which the  JavaScript engine allocates sub-blocks in order to optimize performance; issuing a large number of <code>malloc()</code> calls for each individual block is inefficient, so the engine creates arenas, then uses an internal memory management system to allocate memory from within those.  The arena table is a list of all the arenas that have been allocated by the shell.  <code>stats("arena")</code> lets you inspect the arena table so that you can see how a test case uses arenas.
</p><p>An <i>atom</i> is a unique identifier for a string. In order to optimize comparisons between strings, each string is given an atom.  These atoms are stored in a hash table, which you can display using the command <code>stats("atom")</code>.
</p><p><code>stats("global")</code> displays the global name table, which contains the names and information about each object in memory.
</p>
<h4 name="stringsAreUtf8.28.29"><code>stringsAreUtf8()</code></h4>
<p>Returns <code>true</code> if strings are encoded in UTF8 format; otherwise, returns <code>false</code>.
</p>
<h4 name="testUtf8.28mode.29"><code>testUtf8(<i>mode</i>)</code></h4>
<p>Performs UTF-8 tests.  The mode parameter may be an integer from 1 to 4.
</p><p>This is a debugging command that simply runs some checks on UTF8 string handling, and isn't of general use, unless you're working on the JavaScript engine itself.
</p>
<h4 name="throwError.28.29"><code>throwError()</code></h4>
<p>Throws an error from the <code>JS_ReportError()</code> function.
</p>
<div class="note"><b>Note:</b> This function is intended only for use when testing the JavaScript engine.</div>
<h4 name="tracing.28.5Btoggle.5D.29"><code>tracing(<i>[toggle]</i>)</code></h4>
<p>Enables or disables tracing mode.  Pass <code>true</code> to enable tracing or <code>false</code> to turn it off.  If you don't specify a parameter, <code>tracing()</code> returns the current setting.
</p>
<div class="tip"><b>Tip:</b> This only works if you build JavaScript with <code> JS_THREADED_INTERP</code> turned off in the file <tt>jsinterp.c</tt>.</div>
<h4 name="trap.28.5Bfunction.2C_.5Bpc.2C.5D.5D_expression.29"><code>trap(<i>[function, [pc,]] expression</i>)</code></h4>
<p>Sets a trap at the specific point in the JavaScript code.  When the bytecode at the offset specified by <i>pc</i> in the function <i>function</i> is about to be executed, the <i>expression</i> is evaluated.
</p><p>This is a powerful debugging mechanism when used in concert with <code>line2pc()</code>.  For example, if you want to display a message when line 6 of a function, <code>doSomething()</code> is executed, you can enter the following:
</p>
<pre>trap(doSomething, line2pc(test, 6), "print('line 6!\n')");
</pre>
<div class="note"><b>Note:</b> When a trap is set, the corresponding bytecode in the program is replaced with a <code>trap</code> bytecode until you use <code>untrap()</code> to remove the trap.</div>
<h4 name="untrap.28function_.5B.2C_pc.5D.29"><code>untrap(<i>function [, pc]</i>)</code></h4>
<p>Removes a trap from the specified <i>function</i> at the offset <i>pc</i>.  If <i>pc</i> isn't specified, the trap is removed from the function's entry point.
</p><p>This function has no effect if there is no trap at the specified location.
</p>
<h4 name="version.28.5Bnumber.5D.29"><code>version(<i>[number]</i>)</code></h4>
<p><code>version()</code> 함수는 자바스크립트 버전을 반환합니다. 그다지 쓸 일이 있는 함수는 아니지요.
</p>
Revert to this revision