LoginSignup
80
67

More than 5 years have passed since last update.

OSXのターミナルでJavascriptを動かす

Last updated at Posted at 2015-05-02

はじめに

ターミナルからJavascriptを動かしたかった。
先生に訊いてみると、MacにはJSCというインタプリタが最初からついている、とのこと。
それ使おう。
ということで、JSCのヘルプを見てみる。

mac1:~ user$ jsc --help
-bash: jsc: command not found

初期状態ではパスが通ってないので使えないようだ。

1. MacについてるJSCのパスを設定する

まずはパスを通す。
先生によると、JSCの場所はここらしい。

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

なるほど。
覗いてみると、たしかにあった。
ということで、ターミナルで以下を実行。

mac1:~ user$ cd /usr/local/bin
mac1:bin user$ ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc ./

jsc --helpをもう一回やって、通ったかどうか確認

mac1:~ user$ jsc --help
Usage: jsc [options] [files] [-- arguments]
  -d         Dumps bytecode (debug builds only)
  -e         Evaluate argument as script code
  -f         Specifies a source file (deprecated)
  -h|--help  Prints this help message
  -i         Enables interactive mode (default if no files are specified)
  -s         Installs signal handlers that exit on a crash (Unix platforms only)
  -p <file>  Outputs profiling data to a file
  -x         Output exit code before terminating

  --options                  Dumps all JSC VM options and exits
  --dumpOptions              Dumps all JSC VM options before continuing
  --<jsc VM option>=<value>  Sets the specified JSC VM option

うまくいった。

2. 対話型インタフェースで実行

ターミナルでjscをコマンドしてみる。

mac1:~ user$ jsc

すると、こうなる。


mac1:~ user$ jsc
>>> function double(a) {
... var b = a * 2;
... return b;
... }
undefined
>>> double (1024)
2048

おお、できた。

3. jsファイルを実行

helloworld.js
(function(){
  print('hello, world!');
}());

こんな感じのファイルを作って保存。
ターミナルからjscで走らせてみると...

mac1:~ user$ jsc helloworld.js
hello, world!

動いた。
よかったよかった。
おわり。

参考URL

macで、ターミナルからJavaScriptスクリプトファイルを実行する - ytyng.com
MacのターミナルでJSを走らせる。- modifiedの日記

80
67
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
80
67