< prev index next >

## src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/NashornScriptEngine.java

Print this page

      50 import javax.script.ScriptEngine;
      51 import javax.script.ScriptEngineFactory;
      52 import javax.script.ScriptException;
      53 import javax.script.SimpleBindings;
      54 import jdk.nashorn.internal.objects.Global;
      55 import jdk.nashorn.internal.runtime.Context;
      56 import jdk.nashorn.internal.runtime.ErrorManager;
      57 import jdk.nashorn.internal.runtime.ScriptFunction;
      58 import jdk.nashorn.internal.runtime.ScriptObject;
      59 import jdk.nashorn.internal.runtime.ScriptRuntime;
      60 import jdk.nashorn.internal.runtime.Source;
      61 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
      62 import jdk.nashorn.internal.runtime.options.Options;
      63
      64 /**
      65  * JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through
      66  * {@link NashornScriptEngineFactory#getScriptEngine()}. Note that this engine implements the {@link Compilable} and
      67  * {@link Invocable} interfaces, allowing for efficient precompilation and repeated execution of scripts.
      68  * @see NashornScriptEngineFactory
      69  *

      70  * @since 1.8u40
      71  */

      72 public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
      73     /**
      74      * Key used to associate Nashorn global object mirror with arbitrary Bindings instance.
      75      */
      76     public static final String NASHORN_GLOBAL = "nashorn.global";
      77
      78     // commonly used access control context objects
      79     private static AccessControlContext createPermAccCtxt(final String permName) {
      80         final Permissions perms = new Permissions();
      81         perms.add(new RuntimePermission(permName));
      82         return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
      83     }
      84
      85     private static final AccessControlContext CREATE_CONTEXT_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_CONTEXT);
      86     private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(Context.NASHORN_CREATE_GLOBAL);
      87
      88     // the factory that created this engine
      89     private final ScriptEngineFactory factory;
      90     // underlying nashorn Context - 1:1 with engine instance
      91     private final Context             nashornContext;
     118         final Options options = new Options("nashorn");
     119         options.process(args);
     120
     121         // throw ParseException on first error from script
     122         final ErrorManager errMgr = new Context.ThrowErrorManager();
     123         // create new Nashorn Context
     124         this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
     125             @Override
     126             public Context run() {
     127                 try {
     128                     return new Context(options, errMgr, appLoader, classFilter);
     129                 } catch (final RuntimeException e) {
     130                     if (Context.DEBUG) {
     131                         e.printStackTrace();
     132                     }
     133                     throw e;
     134                 }
     135             }
     136         }, CREATE_CONTEXT_ACC_CTXT);
     137

     138         // cache this option that is used often
     139         this._global_per_engine = nashornContext.getEnv()._global_per_engine;
     140
     141         // create new global object
     142         this.global = createNashornGlobal();
     143         // set the default ENGINE_SCOPE object for the default context
     144         context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
     145     }
     146
     147     @Override
     148     public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException {
     149         return evalImpl(makeSource(reader, ctxt), ctxt);
     150     }
     151
     152     @Override
     153     public Object eval(final String script, final ScriptContext ctxt) throws ScriptException {
     154         return evalImpl(makeSource(script, ctxt), ctxt);
     155     }
     156
     157     @Override
      50 import javax.script.ScriptEngine;
      51 import javax.script.ScriptEngineFactory;
      52 import javax.script.ScriptException;
      53 import javax.script.SimpleBindings;
      54 import jdk.nashorn.internal.objects.Global;
      55 import jdk.nashorn.internal.runtime.Context;
      56 import jdk.nashorn.internal.runtime.ErrorManager;
      57 import jdk.nashorn.internal.runtime.ScriptFunction;
      58 import jdk.nashorn.internal.runtime.ScriptObject;
      59 import jdk.nashorn.internal.runtime.ScriptRuntime;
      60 import jdk.nashorn.internal.runtime.Source;
      61 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
      62 import jdk.nashorn.internal.runtime.options.Options;
      63
      64 /**
      65  * JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through
      66  * {@link NashornScriptEngineFactory#getScriptEngine()}. Note that this engine implements the {@link Compilable} and
      67  * {@link Invocable} interfaces, allowing for efficient precompilation and repeated execution of scripts.
      68  * @see NashornScriptEngineFactory
      69  *
      70  * @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool
      71  * are deprecated with the intent to remove them in a future release.
      72  *
      73  * @since 1.8u40
      74  */
      75 @Deprecated(since="11", forRemoval=true)
      76 public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
      77     /**
      78      * Key used to associate Nashorn global object mirror with arbitrary Bindings instance.
      79      */
      80     public static final String NASHORN_GLOBAL = "nashorn.global";
      81
      82     // commonly used access control context objects
      83     private static AccessControlContext createPermAccCtxt(final String permName) {
      84         final Permissions perms = new Permissions();
      85         perms.add(new RuntimePermission(permName));
      86         return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
      87     }
      88
      89     private static final AccessControlContext CREATE_CONTEXT_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_CONTEXT);
      90     private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(Context.NASHORN_CREATE_GLOBAL);
      91
      92     // the factory that created this engine
      93     private final ScriptEngineFactory factory;
      94     // underlying nashorn Context - 1:1 with engine instance
      95     private final Context             nashornContext;
     122         final Options options = new Options("nashorn");
     123         options.process(args);
     124
     125         // throw ParseException on first error from script
     126         final ErrorManager errMgr = new Context.ThrowErrorManager();
     127         // create new Nashorn Context
     128         this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
     129             @Override
     130             public Context run() {
     131                 try {
     132                     return new Context(options, errMgr, appLoader, classFilter);
     133                 } catch (final RuntimeException e) {
     134                     if (Context.DEBUG) {
     135                         e.printStackTrace();
     136                     }
     137                     throw e;
     138                 }
     139             }
     140         }, CREATE_CONTEXT_ACC_CTXT);
     141
     142         if (!nashornContext.getEnv()._no_deprecation_warning) {
     143             System.err.println("Warning: Nashorn engine is planned to be removed from a future JDK release");
     144         }
     145
     146         // cache this option that is used often
     147         this._global_per_engine = nashornContext.getEnv()._global_per_engine;
     148
     149         // create new global object
     150         this.global = createNashornGlobal();
     151         // set the default ENGINE_SCOPE object for the default context
     152         context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
     153     }
     154
     155     @Override
     156     public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException {
     157         return evalImpl(makeSource(reader, ctxt), ctxt);
     158     }
     159
     160     @Override
     161     public Object eval(final String script, final ScriptContext ctxt) throws ScriptException {
     162         return evalImpl(makeSource(script, ctxt), ctxt);
     163     }
     164
     165     @Override

< prev index next >