Runtime Compilation

The easiest way to compile JavaScript Strands is to load and compile code dynamically on the client-side. To do this, include the strands runtime library (strands.js) and the compiler (compiler.js) script in your web page:

    <script src="../js/strands.js"></script>
Next, load and compile your scripts:
	
    <script>strands.loadScript("my_script_file.jss");</script>
That's it! You're ready to go. However because of the overhead of compiling on every page load, and the inability to debug dynamically evaled code in some debuggers, this is not the recommended way to use Strands, but it is an easy way to get started.

Build Time Compilation

Because of performance and debugging issues with dynamic compliation, the JavaScript Strands distribution includes a java command-line compiler based on Rhino. You can use the following command to compile your .jss or .js17 files:

    java -classpath js.jar;strands.jar \
         com.xucia.strands.Compiler \
         file1.jss
			
This will create a compiled script file named file1.js in the same directory as the .jss file. (The js.jar and strands.jar files can be found in the lib/ directory of the JavaScript Strands distribution.)

When compiling scripts from the command-line, you no longer need to load the entire JavaScript Strands library into your web page. Instead, you need only include much smaller strands.js:

    <script src="../js/strands.js"></script>

Servlet Based Compilation

If you are using a servlet based server, you can use a servlet to handle the compilation of your JavaScript Strand files automatically. You can add the servlet found in the conf/web.xml file to your web.xml configuration and include the js.jar and strands.jar (found in the lib directory) in your classpath, (usually easiest to put these in your WEB-INF/lib directory). You can then create .jss files, and refer to them with .js extension from your scripts, and the servlet will automatically compile them when changes are made to the jss file.

Compile Here

You can also compile your JavaScript Strands code right here on this page.

Compiler Options

debug=[true|false] source=[js17|jss]

Debug turns on or off adding the original source code into the output as comments. Line numbering is always preserved, but adding the original source code can be very helpful for debugging, however it does signficantly increase the size of the compiled code (by about 30-40%).

The source specifies whether you want to compile JavaScript with continuation support through out (jss), or if you want to compile JavaScript 1.7 code (using native generator support for continuations).

Try it Out

Enter some JavaScript Strands code here:

debug 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 compiler.js into your JavaScript environment. Then simply create and invoke a new compiler:

    var compiler = new StrandsCompiler(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
    }