Runtime Compilation
The easiest way to compile Narrative JavaScript is to load, parse, and compile code dynamically on the client-side. To do this, start by including the deploy/njs_compile.js script in your web page. This script contains the entire Narrative JavaScript library (including the compiler):
<script src="../deploy/njs_compile.js"></script>Next, load and compile your scripts:
<script>NjsCompiler.load("my_script_file.njs");</script>That's it! You're ready to go.
Build Time Compilation
Dynamically loading and compiling scripts at runtime may incur significant performance overhead depending on the size of the scripts you compile. In addition, it may be harder to debug such scripts on some platforms.
For these reasons the Narrative JavaScript distribution includes a java command-line compiler based on Rhino. You can use the following command to compile your .njs files:
java -classpath js.jar:narrativejs.jar \ com.neilmix.narrativejs.CompilerMain \ file1.njs file2.njsThis will create compiled script files named file1.js and file2.js in the same directory as the .njs file. (The js.jar and narrativejs.jar files can be found in the lib/ directory of the Narrative JavaScript distribution.)
When compiling scripts from the command-line, you no longer need to load the entire Narrative JavaScript library into your web page. Instead, you need only include much smaller njs_runtime.js:
<script src="../deploy/njs_runtime.js"></script>
Compiler Options
exceptions=[on|off|true|false|1|0|yes|no]
Turns on or off support for exception handling within yielding methods. The default is "on", which is appropriate for most environments. Some environments don't support or have poor support for exception handling; in such cases it is advisable to turn this flag off.
Try it Out
Enter some Narrative JavaScript code here:
exception support
Compiled code:
Compiler API
If the above options aren't flexible enough for you, you can access the compiler API directly. Start by loading njs_compile.js into your JavaScript environment. Then simply create and invoke a new compiler:
var compiler = new NjsCompiler(options); try { // options is a hash table of name/value pairs corresponding to // the command-line options for the compiler. var output_code = compiler.compile(input_code, filename) } catch(e) { // e is a JavaScript Error object // do error notification here }