Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.caucho.es
Class Script

    java.lang.Object
      |
      +--com.caucho.es.Script

public abstract class Script
extends java.lang.Object

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.

Field Summary
`protected Path` | `classDir`
`protected Path` | `scriptPath`

Constructor Summary
`Script()`

Method Summary
`java.lang.String` | `execute(java.util.HashMap properties, java.lang.Object proto)` Execute the script; the main useful entry.
`ScriptClosure` | `executeClosure(java.util.HashMap properties, java.lang.Object proto)` Execute the program, returning a closure of the global state.
`void` | `export(ESObject dest, ESObject src)` Internal method to export objects.
`com.caucho.java.LineMap` | `getLineMap()` Returns the map from source file line numbers to the generated java line numbers.
`abstract ESGlobal` | `initClass(Global resin)` Internal method implemented by the generated script for initialization.
`ESGlobal` | `initClass(Global resin, ESObject global)` Internal method to initialize the script after loading it.
`boolean` | `isModified()` Internal method to check if the source files have changed.
`void` | `setClassDir(Path classDir)` Internal method to set the work directory for generated *.java and *.class.
`void` | `setScriptPath(Path scriptPath)` Internal method to set the script search path for imported scripts.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### scriptPath

    protected Path scriptPath

### classDir

    protected Path classDir

Constructor Detail

### Script

    public Script()

Method Detail

### isModified

    public boolean isModified()

  Internal method to check if the source files have changed.

### setScriptPath

    public void setScriptPath(Path scriptPath)

  Internal method to set the script search path for imported scripts.

### setClassDir

    public void setClassDir(Path classDir)

  Internal method to set the work directory for generated *.java and *.class.

### getLineMap

    public com.caucho.java.LineMap getLineMap()

  Returns the map from source file line numbers to the generated java line numbers.

### execute

    public java.lang.String execute(java.util.HashMap properties,
                                    java.lang.Object proto)
                             throws java.lang.Exception

  Execute the script; the main useful entry.

  Calling programs can make Java objects available as properties of the global object by creating a property hash map or assigning a global prototype.

       HashMap map = new HashMap();
       map.put("File", Vfs.lookup());
       map.put("out", System.out);
       map.put("myObject", myObject);
       script.execute(map, null);

  Then the JavaScript can use the defined objects:

       out.println(myObject.myMethod("foo"));

  Parameters:
    `properties` - A hash map of global properties.
    `proto` - Global prototype. Gives the script direct access to the java methods of the object.
  Returns:
    String value of the last expression, like the JavaScript eval. This is useful only for testing.

### executeClosure

    public ScriptClosure executeClosure(java.util.HashMap properties,
                                        java.lang.Object proto)
                                 throws java.lang.Exception

  Execute the program, returning a closure of the global state. `executeClosure` will execute the global script.

  Later routines can then call into the closure. The closure is not thread-safe. So only a single thread may execute the closure.

  Parameters:
    `properties` - A hash map of global properties.
    `proto` - Global prototype. Gives the script direct access to the java methods of the object.
  Returns:
    the closure

### initClass

    public ESGlobal initClass(Global resin,
                              ESObject global)
                       throws java.lang.Exception

  Internal method to initialize the script after loading it.

### initClass

    public abstract ESGlobal initClass(Global resin)
                                throws java.lang.Exception

  Internal method implemented by the generated script for initialization.

### export

    public void export(ESObject dest,
                       ESObject src)
                throws java.lang.Exception

  Internal method to export objects.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD