The JavaScript compiler package. The Parser class in
com.caucho.es.parser.Parser is the primary public entry.
The public classes are Parser, Script, and ScriptClosure. Parser is
the main entry to the compiler. Script contains a compiled script.
ScriptClosure lets programs execute the global script and then call
back into the JavaScript. The other classes are public only because
JavaScript gets compiled to Java. The generated Java classes need to
access the JavaScript libraries.
The script must first be compiled to a Script object, then
executed. The following example gives the bare-bones script invocation.
Parser parser = new Parser();
Script script = parser.parse("foo.js");
script.execute(null, null);
You can make Java objects available to the Script by adding them to
a HashMap. For example, the File object. The running script will
see these objects as properties of the Global object. If you set the
'out' object, the script can use 'writeln("foo")' to write to 'out'.
HashMap map = new HashMap();
map.put("File", Pwd.lookup());
map.put("out", System.out);
map.put("myObject", myObject);
script.execute(map, null);
You can also make any Java object be the global prototype.
Essentially, the effect is similar to the HashMap technique, but
it's a little simpler.
The Script object is threadsafe. It's safe to execute the same script
in multiple threads.
The ScriptClosure object, of course, is not threadsafe.
|