js - evaluate JavaScript
$ js [options...] [script.js]
js
evaluates JavaScript programs using the LibJS engine. If you pass it a path
to a script file, it will execute that script. Otherwise, it enters the
Read-Eval-Print-Loop (REPL) mode, where it interactively reads pieces (usually,
single lines) of code from standard input, evaluates them in one shared
interpreter context, and prints back their results. This mode is useful for
quickly experimenting with LibJS.
Run help()
in REPL mode to see its available built-in functions.
-A
, --dump-ast
: Dump the Abstract Syntax Tree after parsing the program.-l
, --print-last-result
: Print the result of the last statement executed.-g
, --gc-on-every-allocation
: Run garbage collection on every allocation.-s
, --syntax-highlight
: Enable live syntax highlighting in the REPL-t
, --test-mode
: Run the interpreter with added functionality for the test harnessHere's how you execute a script:
$ js ~/js/type-play.js
And here's an example of an interactive REPL session:
$ js
> function log_sum(a, b) {
> console.log(a + b)
> }
undefined
> log_sum(35, 42)
77
undefined
In test mode, the load()
function is added to the global object and can be used
to load further test utility functions defined in LibJS/Tests/test-common.js
.
Typically a test will look like this:
load("test-common.js");
try {
// test feature
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
Available functions in test-common.js
:
assert(expression)
: Throws an AssertionError
if condition does not evaluate to a truthy valueassertNotReached()
: Throws an AssertionError
, use to ensure certain code paths are never reached