@Override public Object getParameter(final String key) { switch (key) { case ScriptEngine.NAME: return "javascript"; case ScriptEngine.ENGINE: return "Oracle Nashorn"; case ScriptEngine.ENGINE_VERSION: return Version.version(); case ScriptEngine.LANGUAGE: return "ECMAScript"; case ScriptEngine.LANGUAGE_VERSION: return "ECMA - 262 Edition 5.1"; case "THREADING": // The engine implementation is not thread-safe. Can't be // used to execute scripts concurrently on multiple threads. return null; default: throw new IllegalArgumentException("Invalid key"); } }
@Override public Object getParameter(final String key) { switch (key) { case ScriptEngine.NAME: return "javascript"; case ScriptEngine.ENGINE: return "Oracle Nashorn"; case ScriptEngine.ENGINE_VERSION: return Version.version(); case ScriptEngine.LANGUAGE: return "ECMAScript"; case ScriptEngine.LANGUAGE_VERSION: return "ECMA - 262 Edition 5.1"; case "THREADING": // The engine implementation is not thread-safe. Can't be // used to execute scripts concurrently on multiple threads. return null; default: return null; } }
/** * Constructor. * * @throws LanguageAdapterException * In case of an initialization error */ public NashornAdapter() throws LanguageAdapterException { super( "Nashorn", Version.version(), "JavaScript", new NashornScriptEngineFactory().getLanguageVersion(), Arrays.asList( "js", "javascript", "nashorn" ), "js", Arrays.asList( "javascript", "js", "nashorn" ), "nashorn" ); try { System.setProperty( "nashorn.persistent.code.cache", getCacheDir().getCanonicalPath() ); } catch( IOException x ) { throw new LanguageAdapterException( NashornAdapter.class, "Could not access cache directory: " + getCacheDir(), x ); } PrintWriter out = new PrintWriter( new ExecutionContextWriter(), true ); PrintWriter err = new PrintWriter( new ExecutionContextErrorWriter(), true ); // See: jdk.nashorn.internal.runtime.ScriptEnvironment Options options = new Options( "nashorn", err ); options.set( "print.no.newline", true ); options.set( "persistent.code.cache", true ); ErrorManager errors = new ErrorManager( err ); context = new Context( options, errors, out, err, getClass().getClassLoader() ); }
@Test public void versionTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); assertEquals(e.getFactory().getEngineVersion(), Version.version()); }