The Script object represents a compiled JavaScript. Executing it
is thread safe. To create a Script, use the Parser class to parse
a file.
Java programs set JavaScript Global properties by adding objects to
a HashMap. Typically you will at least assign the 'File' and
the 'out' objects. The running script will
see these objects as properties of the Global object. If you set the
'out' object, the script can use the bare bones 'writeln("foo")' to
write to 'out'.
HashMap map = new HashMap();
map.put("File", Vfs.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.
Scripts are thread-safe. Multiple script instances can
safely execute in separate threads. Script.execute creates the
entire JavaScript global object hierarchy fresh for each execution.
So one Script execution cannot interfere with another, even by doing
evil things like modifying the Object prototype.
Of course, wrapped Java objects shared by script invocations
must still be synchronized.
|