wiki:JSC
Last modified 5 years ago Last modified on 07/01/09 16:12:06

JSC

jsc is a command-line utility that allows you to run JavaScript programs outside of the context of a web browser. It is primarily used as part of the test harness for validating the JavaScript portions of WebKit, but can also be used as a scripting tool.

jsc can be run in an interactive mode to test out JavaScript expressions, or it can be passed one or more files to run similar to invoking a Perl or Python script.

Usage

Command Line Flags

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)

Built-In Functions

checkSyntax('[FileName]')
Check the syntax of the specified external JavaScript file [FileName].
debug(term)
Prints a human-readable version of the passed argument.
gc()
Perform a garbage collection. It will return undefined.
load('[FileName]')
Load and execute the specified external JavaScript file [FileName]. If the run is successful, it will return the final value of the script.
quit()
Quit the interpreter
readline()
Read data from stdin. Mainly useful for writing test routines.
run('[FileName]')
Load and execute the specified external JavaScript file [FileName]. If the run is successful, it will return the elapsed milliseconds for the run.
version()
Currently does nothing. It will return undefined.

Examples

You can use jsc as a calculator:

$ ./jsc
> "hello"
hello
> 1 + 3
4
> 2 * Math.sin(32)
1.1028533624833812
> 2 * Math.floor(1.2)
2
> 2 * Math.ceil(1.2)
4
> 15.3 / 18 * 27.1 * (Math.ceil(1.3) * Math.exp(2.3) * Math.log (1.223) * Math.sin(32.22))
66.6192983328985
> quit()

You can also test more complicated expressions:

> var add3 = function(arg) { return arg + 3; }
undefined
> add3(3)
6
> var foo = readline();
abcdefg
undefined
> foo
abcdefg
> quit()

The return value of undefined from the variable assignment is from the JavaScript specification. While it might seem like an odd response to a successful operation, it is the expected behavior.