NashornScriptEngine (Java SE 9 & JDK 9 )
Keywords: jdk.nashorn.api.scripting.NashornScriptEngine class

Module jdk.scripting.nashorn
Package jdk.nashorn.api.scripting

## Class NashornScriptEngine

- java.lang.Object
- - javax.script.AbstractScriptEngine
  - - jdk.nashorn.api.scripting.NashornScriptEngine

- All Implemented Interfaces:
    `Compilable`, `Invocable`, `ScriptEngine`

      public final class NashornScriptEngine
      extends AbstractScriptEngine
      implements Compilable, Invocable

  JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through `NashornScriptEngineFactory.getScriptEngine()`. Note that this engine implements the `Compilable` and `Invocable` interfaces, allowing for efficient precompilation and repeated execution of scripts.
  Since:
    1.8u40
  See Also:
    `NashornScriptEngineFactory`

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static String` | `NASHORN_GLOBAL` | Key used to associate Nashorn global object mirror with arbitrary Bindings instance.

    - ### Fields inherited from class javax.script.AbstractScriptEngine

      `context`

    - ### Fields inherited from interface javax.script.ScriptEngine

      `ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `CompiledScript` | `compile​(Reader reader)` | Compiles the script (source read from `Reader`) for later execution.
    `CompiledScript` | `compile​(String str)` | Compiles the script (source represented as a `String`) for later execution.
    `Bindings` | `createBindings​()` | Returns an uninitialized `Bindings`.
    `Object` | `eval​(Reader reader, ScriptContext ctxt)` | Same as `eval(String, ScriptContext)` where the source of the script is read from a `Reader`.
    `Object` | `eval​(String script, ScriptContext ctxt)` | Causes the immediate execution of the script whose source is the String passed as the first argument.
    `ScriptEngineFactory` | `getFactory​()` | Returns a `ScriptEngineFactory` for the class to which this `ScriptEngine` belongs.
    `<T> T` | `getInterface​(Class<T> clazz)` | Returns an implementation of an interface using functions compiled in the interpreter.
    `<T> T` | `getInterface​(Object thiz, Class<T> clazz)` | Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter.
    `Object` | `invokeFunction​(String name, Object... args)` | Used to call top-level procedures and functions defined in scripts.
    `Object` | `invokeMethod​(Object thiz, String name, Object... args)` | Calls a method on a script object compiled during a previous script execution, which is retained in the state of the `ScriptEngine`.

    - ### Methods inherited from class javax.script.AbstractScriptEngine

      `eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### NASHORN_GLOBAL

          public static final String NASHORN_GLOBAL

      Key used to associate Nashorn global object mirror with arbitrary Bindings instance.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### eval

          public Object eval​(Reader reader,
                             ScriptContext ctxt)
                      throws ScriptException

      Description copied from interface: `ScriptEngine`
      Same as `eval(String, ScriptContext)` where the source of the script is read from a `Reader`.
      Specified by:
        `eval` in interface `ScriptEngine`
      Parameters:
        `reader` - The source of the script to be executed by the script engine.
        `ctxt` - The `ScriptContext` passed to the script engine.
      Returns:
        The value returned from the execution of the script.
      Throws:
        `ScriptException` - if an error occurs in script. ScriptEngines should create and throw `ScriptException` wrappers for checked Exceptions thrown by underlying scripting implementations.

    - #### eval

          public Object eval​(String script,
                             ScriptContext ctxt)
                      throws ScriptException

      Description copied from interface: `ScriptEngine`
      Causes the immediate execution of the script whose source is the String passed as the first argument. The script may be reparsed or recompiled before execution. State left in the engine from previous executions, including variable values and compiled procedures may be visible during this execution.
      Specified by:
        `eval` in interface `ScriptEngine`
      Parameters:
        `script` - The script to be executed by the script engine.
        `ctxt` - A `ScriptContext` exposing sets of attributes in different scopes. The meanings of the scopes `ScriptContext.GLOBAL_SCOPE`, and `ScriptContext.ENGINE_SCOPE` are defined in the specification.
        The `ENGINE_SCOPE` `Bindings` of the `ScriptContext` contains the bindings of scripting variables to application objects to be used during this script execution.
      Returns:
        The value returned from the execution of the script.
      Throws:
        `ScriptException` - if an error occurs in script. ScriptEngines should create and throw `ScriptException` wrappers for checked Exceptions thrown by underlying scripting implementations.

    - #### getFactory

          public ScriptEngineFactory getFactory​()

      Description copied from interface: `ScriptEngine`
      Returns a `ScriptEngineFactory` for the class to which this `ScriptEngine` belongs.
      Specified by:
        `getFactory` in interface `ScriptEngine`
      Returns:
        The `ScriptEngineFactory`

    - #### createBindings

          public Bindings createBindings​()

      Description copied from interface: `ScriptEngine`
      Returns an uninitialized `Bindings`.
      Specified by:
        `createBindings` in interface `ScriptEngine`
      Returns:
        A `Bindings` that can be used to replace the state of this `ScriptEngine`.

    - #### compile

          public CompiledScript compile​(Reader reader)
                                 throws ScriptException

      Description copied from interface: `Compilable`
      Compiles the script (source read from `Reader`) for later execution. Functionality is identical to `compile(String)` other than the way in which the source is passed.
      Specified by:
        `compile` in interface `Compilable`
      Parameters:
        `reader` - The reader from which the script source is obtained.
      Returns:
        An instance of a subclass of `CompiledScript` to be executed later using one of its `eval` methods of `CompiledScript`.
      Throws:
        `ScriptException` - if compilation fails.

    - #### compile

          public CompiledScript compile​(String str)
                                 throws ScriptException

      Description copied from interface: `Compilable`
      Compiles the script (source represented as a `String`) for later execution.
      Specified by:
        `compile` in interface `Compilable`
      Parameters:
        `str` - The source of the script, represented as a `String`.
      Returns:
        An instance of a subclass of `CompiledScript` to be executed later using one of the `eval` methods of `CompiledScript`.
      Throws:
        `ScriptException` - if compilation fails.

    - #### invokeFunction

          public Object invokeFunction​(String name,
                                       Object... args)
                                throws ScriptException,
                                       NoSuchMethodException

      Description copied from interface: `Invocable`
      Used to call top-level procedures and functions defined in scripts.
      Specified by:
        `invokeFunction` in interface `Invocable`
      Parameters:
        `name` - of the procedure or function to call
        `args` - Arguments to pass to the procedure or function
      Returns:
        The value returned by the procedure or function
      Throws:
        `ScriptException` - if an error occurs during invocation of the method.
        `NoSuchMethodException` - if method with given name or matching argument types cannot be found.

    - #### invokeMethod

          public Object invokeMethod​(Object thiz,
                                     String name,
                                     Object... args)
                              throws ScriptException,
                                     NoSuchMethodException

      Description copied from interface: `Invocable`
      Calls a method on a script object compiled during a previous script execution, which is retained in the state of the `ScriptEngine`.
      Specified by:
        `invokeMethod` in interface `Invocable`
      Parameters:
        `thiz` - If the procedure is a member of a class defined in the script and thiz is an instance of that class returned by a previous execution or invocation, the named method is called through that instance.
        `name` - The name of the procedure to be called.
        `args` - Arguments to pass to the procedure. The rules for converting the arguments to scripting variables are implementation-specific.
      Returns:
        The value returned by the procedure. The rules for converting the scripting variable returned by the script method to a Java Object are implementation-specific.
      Throws:
        `ScriptException` - if an error occurs during invocation of the method.
        `NoSuchMethodException` - if method with given name or matching argument types cannot be found.

    - #### getInterface

          public <T> T getInterface​(Class<T> clazz)

      Description copied from interface: `Invocable`
      Returns an implementation of an interface using functions compiled in the interpreter. The methods of the interface may be implemented using the `invokeFunction` method.
      Specified by:
        `getInterface` in interface `Invocable`
      Type Parameters:
        `T` - the type of the interface to return
      Parameters:
        `clazz` - The `Class` object of the interface to return.
      Returns:
        An instance of requested interface - null if the requested interface is unavailable, i. e. if compiled functions in the `ScriptEngine` cannot be found matching the ones in the requested interface.

    - #### getInterface

          public <T> T getInterface​(Object thiz,
                                    Class<T> clazz)

      Description copied from interface: `Invocable`
      Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter. The methods of the interface may be implemented using the `invokeMethod` method.
      Specified by:
        `getInterface` in interface `Invocable`
      Type Parameters:
        `T` - the type of the interface to return
      Parameters:
        `thiz` - The scripting object whose member functions are used to implement the methods of the interface.
        `clazz` - The `Class` object of the interface to return.
      Returns:
        An instance of requested interface - null if the requested interface is unavailable, i. e. if compiled methods in the `ScriptEngine` cannot be found matching the ones in the requested interface.