@Test public void evalTest() { final Options options = new Options(""); final ErrorManager errors = new ErrorManager(); final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); final Global oldGlobal = Context.getGlobal(); Context.setGlobal(cx.createGlobal()); try { String code = "22 + 10"; assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); code = "obj = { js: 'nashorn' }; obj.js"; assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); } finally { Context.setGlobal(oldGlobal); } }
@Test public void compileErrorTest() { final Options options = new Options(""); final ErrorManager errors = new ErrorManager(); final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); final Global oldGlobal = Context.getGlobal(); Context.setGlobal(cx.createGlobal()); try { final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal()); if (script != null) { fail("Invalid script compiled without errors"); } if (errors.getNumberOfErrors() != 1) { fail("Wrong number of errors: " + errors.getNumberOfErrors()); } } finally { Context.setGlobal(oldGlobal); } }
@BeforeClass public void setupTest() { final Options options = new Options("nashorn"); options.set("compile.only", true); options.set("print.ast", true); options.set("print.parse", true); options.set("scripting", true); options.set("const.as.var", true); options.set("verify.code", true); final ErrorManager errors = new ErrorManager() { @Override public void error(final String msg) { log(msg); } }; final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw); this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); this.global = context.createGlobal(); }
private static Context makeContext(InputStream in, OutputStream out, OutputStream err, String[] args) { PrintStream pout = out instanceof PrintStream?(PrintStream)out:new PrintStream(out); PrintStream perr = err instanceof PrintStream?(PrintStream)err:new PrintStream(err); PrintWriter wout = new PrintWriter(pout, true); PrintWriter werr = new PrintWriter(perr, true); ErrorManager errors = new ErrorManager(werr); Options options = new Options("nashorn", werr); if(args != null) { try { options.process(args); } catch (IllegalArgumentException var27) { werr.println(bundle.getString("shell.usage")); options.displayHelp(var27); return null; } } return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); }
@BeforeClass public void setupTest() { final Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("compile.only", true); options.set("print.ast", true); options.set("print.parse", true); options.set("scripting", true); options.set("const.as.var", true); options.set("verify.code", true); final ErrorManager errors = new ErrorManager() { @Override public void error(final String msg) { log(msg); } }; final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw); this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); this.global = context.createGlobal(); }
/** * Creates the AST serializer executor service used for in-memory serialization of split functions' ASTs. * It is created with an unbounded queue (so it can queue any number of pending tasks). Its core and max * threads is the same, but they are all allowed to time out so when there's no work, they can all go * away. The threads will be daemons, and they will time out if idle for a minute. Their priority is also * slightly lower than normal priority as we'd prefer the CPU to keep running the program; serializing * split function is a memory conservation measure (it allows us to release the AST), it can wait a bit. * @return an executor service with above described characteristics. */ private static ExecutorService createAstSerializerExecutorService() { final int threads = Math.max(1, Options.getIntProperty("nashorn.serialize.threads", Runtime.getRuntime().availableProcessors() / 2)); final ThreadPoolExecutor service = new ThreadPoolExecutor(threads, threads, 1L, TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(), new ThreadFactory() { @Override public Thread newThread(final Runnable r) { final Thread t = new Thread(r, "Nashorn AST Serializer"); t.setDaemon(true); t.setPriority(Thread.NORM_PRIORITY - 1); return t; } }); service.allowCoreThreadTimeOut(true); return service; }
/** * 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 evalTest() { final Options options = new Options(""); final ErrorManager errors = new ErrorManager(); final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); final ScriptObject oldGlobal = Context.getGlobal(); Context.setGlobal(cx.createGlobal()); try { String code = "22 + 10"; assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); code = "obj = { js: 'nashorn' }; obj.js"; assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); } finally { Context.setGlobal(oldGlobal); } }
@BeforeClass public void setupTest() { final Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("compile.only", true); options.set("print.ast", true); options.set("print.parse", true); options.set("scripting", true); final ErrorManager errors = new ErrorManager() { @Override public void error(final String msg) { log(msg); } }; final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw); this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); this.global = context.createGlobal(); }
private static File createBaseCacheDir() { if(MAX_FILES == 0 || Options.getBooleanProperty("nashorn.typeInfo.disabled")) { return null; } try { return createBaseCacheDirPrivileged(); } catch(final Exception e) { reportError("Failed to create cache dir", e); return null; } }
private static int getMaxFiles() { final String str = Options.getStringProperty("nashorn.typeInfo.maxFiles", null); if (str == null) { return DEFAULT_MAX_FILES; } else if ("unlimited".equals(str)) { return UNLIMITED_FILES; } return Math.max(0, Integer.parseInt(str)); }
ParserImpl(final String... args) throws IllegalArgumentException { Objects.requireNonNull(args); // handle the parser specific "--es6-module" option boolean seenModuleOption = false; for (int idx = 0; idx < args.length; idx++) { final String opt = args[idx]; if (opt.equals("--es6-module")) { seenModuleOption = true; /* * Nashorn parser does not understand parser API specific * option. This option implies --language=es6. So, we change * the option to --language=es6. Note that if user specified * --language=es6 explicitly, that is okay. Nashorn tolerates * repeated options! */ args[idx] = "--language=es6"; break; } } this.moduleMode = seenModuleOption; // append "--parse-only to signal to the Nashorn that it // is being used in "parse only" mode. final String[] newArgs = Arrays.copyOf(args, args.length + 1, String[].class); newArgs[args.length] = "--parse-only"; final Options options = new Options("nashorn"); options.process(newArgs); this.env = new ScriptEnvironment(options, new PrintWriter(System.out), new PrintWriter(System.err)); }
/** * Creates the AST serializer executor service used for in-memory serialization of split functions' ASTs. * It is created with an unbounded queue (so it can queue any number of pending tasks). Its core and max * threads is the same, but they are all allowed to time out so when there's no work, they can all go * away. The threads will be daemons, and they will time out if idle for a minute. Their priority is also * slightly lower than normal priority as we'd prefer the CPU to keep running the program; serializing * split function is a memory conservation measure (it allows us to release the AST), it can wait a bit. * @return an executor service with above described characteristics. */ private static ExecutorService createAstSerializerExecutorService() { final int threads = Math.max(1, Options.getIntProperty("nashorn.serialize.threads", Runtime.getRuntime().availableProcessors() / 2)); final ThreadPoolExecutor service = new ThreadPoolExecutor(threads, threads, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(), (r) -> { final Thread t = new Thread(r, "Nashorn AST Serializer"); t.setDaemon(true); t.setPriority(Thread.NORM_PRIORITY - 1); return t; }); service.allowCoreThreadTimeOut(true); return service; }
@BeforeClass public void setupTest() { final Options options = new Options("nashorn"); options.set("parse.only", true); options.set("scripting", true); options.set("const.as.var", true); final ErrorManager errors = new ErrorManager(); this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader()); }
@BeforeClass public static void beforeClass() { // We must have a Context for the DynamicLinker that Bootstrap.getLinkerServices() will use oldGlobal = Context.getGlobal(); cx = new Context(new Options(""), new ErrorManager(), null); Context.setGlobal(cx.createGlobal()); }
/** * SharedContextEvaluator constructor * @param args initial script arguments to create shared context */ public SharedContextEvaluator(final String[] args) { this.ctxOut = new DelegatingOutputStream(System.out); this.ctxErr = new DelegatingOutputStream(System.err); final PrintWriter wout = new PrintWriter(ctxOut, true); final PrintWriter werr = new PrintWriter(ctxErr, true); final Options options = new Options("nashorn", werr); options.process(args); final ErrorManager errors = new ErrorManager(werr); this.context = new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); }
ParserImpl(final String... args) throws IllegalArgumentException { Objects.requireNonNull(args); Options options = new Options("nashorn"); options.process(args); this.env = new ScriptEnvironment(options, new PrintWriter(System.out), new PrintWriter(System.err)); }