★ wanayoo — archive 1999 http://developer.mozilla.org/ja/docs/Introduction_to_the_JavaScript_shellNouvelle recherche | Portail wanayoo
Visit Mozilla.org

Introduction to the JavaScript shell

出典: MDC

◆◆◆ 翻訳中 ◆◆◆

このページは翻訳中です。
翻訳作業に参加する場合は、履歴にある翻訳者と連絡・調整してください。

目次

導入

この記事は Mozilla の CVS サーバから JavaScript シェルの入手とビルドを紹介します。そしてテストと実験のためにプレリース版を入手しビルドする事に特にフォーカスを当てています。

さらにこの記事は JavaScript コードを実験し JavaScript プログラムを実行するためのシェルの使いかたに関する基本的な情報を提供します。

JavaScript シェルを入手しビルドする

CVS サーバへのログイン

他の Mozilla プロジェクト を CVS からとってくるのと同様に、最初に CVS サーバにログインする必要があります。そうするには、コードをチェックアウトしたい基本のディレクトリに cd し、以下のコマンドをコマンドラインに入力します。

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

入力を促されたら、パスワード anonymous を入力してください。

Trunk 版の JavaScript をビルドする

一度サーバにログインしたなら CVS からコードをとってきましょう。最初に CVS ツリーのルートに cd する必要があり、次に以下のコマンドを入力してください。

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

これは JavaScript シェルをビルドするために必要な全てのファイルをチェックアウトします。

これで以下の 2 つのコマンドを実行すると JavaScript をビルドできるようになりました。

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

コンパイルが完了したら、あるディレクトリ(名前はビルドしたシステムに依存します)の中に js という実行ファイルがあるはずです。js。例えば Mac OS X では実行ファイルは Darwin_DBG.OBJ/js にあります。

この時点で、シェルを実行し試す準備ができました。

ブランチ版の JavaScript をビルドする

JavaScript の特定のブランチ版のを実験したいなら、ファイルをチェックアウトするときに -r branchname をコマンドラインに追加することでできます。

以下はファイルのチェックアウトの時に cvs co... の行を変更した以外は全ては前と同じ作業です。

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

branch_name はチェックアウトしたいブランチの名前に変更してください。例えば JavaScript 1.7 アルファブランチをチェックアウトするには、JS_1_7_ALPHA_BRANCH を指定します。

その後は前と同様にコンパイルし実行できます。

JavaScript シェルを使う

シェルは 2 種類の操作モードを提供します。それを対話的なシェルとして使うことができます。それは JavaScript コードをプロンプトで書き、その瞬間に満足を得られます。これは新機能を実験するかテストするのに便利です。プログラムを自動的に実行したい場合にはコマンドラインで実行する JavaScript プログラムファイルを渡すこともができます。

注意: JavaScript シェルは JavaScript エンジンのテスト環境として使われているため、利用可能なオプションと組み込み関数はそのうちに変わる可能性があります。

コマンドラインオプション

シェルを管理するために設定できる多くのコマンドラインがあります。以下にそれは要約されています。

  • -b branchlimit
    • Sets the branch limit.
  • -c stackchunksize
    • スタックチャンクの大きさを設定する。
  • -C
    • シェルにプログラムをコンパイルするが実行しないように指示する。これはプログラムを実際に実行しないでシンタックスエラーを確認する便利な方法である。
  • -e script
    • 指定された script を実行する。script は実行するコードを含む文字列である。
  • -f filename
    • filename で指定されたファイルの JavaScript プログラムを実行する。
  • -i
    • 対話的モードを有効にする。
  • -P
    • 私もなんなのかまだわからない。
  • -s
    • 厳密に警告を出すモードを有効にする。
  • -S stacksize
    • スタックサイズの最大値を設定する。
  • -v version
    • version で指定された JavaScript のバージョンを設定する。(例えば、JavaScript 1.7 では 170)
  • -w
    • 警告メッセージを有効にする。
  • -W
    • 警告メッセージを無効にする。
  • -x
    • E4X XML モードを有効にする。

シェルを実行する

もし対話的モードでシェルを実行したいなら、単にコマンドを使うだけでできます。

js

もし foo.js ファイルの中の JavaScript コードを実行したいなら、このコマンドを使うことができます。

js -f foo.js

foo.js を実行した後に対話的シェルに入るにはこうします。

js -f foo.js -f -

シェルを対話的モードで使う

対話的モードでは、個別の文と同様にオブジェクトや関数を作るために手で JavaScript コードを入力できます。これはアイディアを試す、JavaScript エンジンで作業している開発者にとってより重要な、言語の新しい機能をテストするには簡単な方法です。

組み込み関数

JavaScript シェルをより便利にするために、JavaScript プログラムか対話的モードで使うことができる多くの組み込み関数が提供されています。

build()

JavaScript シェルがビルドされた日付と時間を返します。

clear([object])

指定された object のプロパティをクリアします。パラメータ無しで clear() を呼び出すと全てがクリアされので白紙の状態で始めることができます。

注意: パラメータ無しの clear() は本当に全てをクリアします。これはこれら全ての組み込み関数を含んでいます。

clone(function, [scope])

指定された function オブジェクトをクローンします。もし scope が指定されていないと、新しいオブジェクトの親は元のオブジェクトの親と同じになります。それ以外なら新しいオブジェクトは scope で指定されたオブジェクトのスコープの中に置かれます。

dis([function])

JavaScript バイトコードをプログラム全体、あるいは特定の function を逆アセンブルします。

例えば、以下の JavaScript 関数を入力したとします:

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

次に dis(test); を実行すると、この出力を受け取るでしょう:

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

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

Runs the garbage collector to clean up memory.

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 js -x, then the options() function will return xml. 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 stdout.

quit()

Exits the shell.

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.

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 jsinterp.c.

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

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