List of usage examples for jdk.nashorn.internal.runtime.options Options set
public void set(final String key, final String option)
From source file:com.mongodb.jvm.test.TestNashorn.java
License:Apache License
private static void run(String script) { PrintWriter out = new PrintWriter(System.out, true); PrintWriter err = new PrintWriter(System.err, true); Options options = new Options("nashorn", err); options.set("print.no.newline", true); ErrorManager errors = new ErrorManager(err); Context context = new Context(options, errors, out, err, Thread.currentThread().getContextClassLoader()); ScriptObject globalScope = context.createGlobal(); Context.setGlobal(globalScope); ScriptFunction fn = context.compileScript(Source.sourceFor(TestNashorn.class.getCanonicalName(), script), globalScope);//from w ww . j a va 2s. com ScriptRuntime.apply(fn, globalScope); }
From source file:com.siemens.ct.exi.javascript.JStoAST.java
License:Open Source License
public static String getAST(String jsCode) throws IOException, EXIException { // http://sites.psu.edu/robertbcolton/2015/07/31/java-8-nashorn-script-engine/ /*/* ww w . ja v a2 s . c o m*/ * This JSON encoding provided by Nashorn is compliant with the * community standard JavaScript JSON AST model popularized by Mozilla. * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/ * SpiderMonkey/Parser_API */ // Additionally the OpenJDK project is developing a public interface for // Java 9 that allows AST traversal in a more standard and user friendly // way. // String code = "function a() { var b = 5; } function c() { }"; // String sin = "./data//jquery.min.js"; // String sin = "./data/animals.js"; // String code = new String(Files.readAllBytes(Paths.get(sin))); // ScriptEngine engine = new // ScriptEngineManager().getEngineByName("nashorn"); // engine.eval("print('Hello World!');"); // load jquery source file // engine.eval("load('" + // "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" + // "');"); // engine.eval("load('" + sin + "');"); Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("parse.only", true); options.set("scripting", true); ErrorManager errors = new ErrorManager(); Context contextm = new Context(options, errors, Thread.currentThread().getContextClassLoader()); Context.setGlobal(contextm.createGlobal()); String jsonAST = ScriptUtils.parse(jsCode, "<unknown>", false); return jsonAST; }
From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java
License:LGPL
/** * Constructor./*w w w . j a v a 2s. com*/ * * @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()); }
From source file:IDE.SyntaxTree.JWebParser.java
public JWebParser(String source) { strSource = source;/*from w w w . j a v a2 s .com*/ Options opt = new Options("nashorn"); opt.set("anon.functions", true); // opt.set("parse.only", true); opt.set("scripting", true); Context context = new Context(opt, em, Thread.currentThread().getContextClassLoader()); Context.setGlobal(new Global(context)); Source textSource = Source.sourceFor("mySource", source); env = context.getEnv();//new ScriptEnvironment(opt, new PrintWriter(OutWriter), new PrintWriter(ErrorWriter) ); nashornParser = new jdk.nashorn.internal.parser.Parser(env, textSource, em); // jdk.nashorn.internal.ir.FunctionCall }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * Make a new Nashorn Context to compile and/or run JavaScript files. * * @param i input stream for Shell//from w w w.j av a 2 s . c om * @param o output stream for Shell * @param e error stream for Shell * * @return null if there are problems with option parsing. */ @SuppressWarnings("resource") private static Context makeContext(final InputStream i, final OutputStream o, final OutputStream e) { final PrintWriter out = new PrintWriter(o instanceof PrintStream ? (PrintStream) o : new PrintStream(o), true); final PrintWriter err = new PrintWriter(e instanceof PrintStream ? (PrintStream) e : new PrintStream(e), true); final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final ErrorManager errors = new ErrorManager(err); final Options options = new Options("nashorn", err); options.set("scripting", true); options.set("language", "es6"); options.set("optimistic.types", true); options.set("lazy.compilation", true); final ClassFilter filter = (name) -> { if (Arrays.stream(prohibitedClasses).parallel().filter(name::equals).findAny().isPresent()) { System.err.println("Access Manager: Denied item " + name + "."); return false; } else return true; }; return new Context(options, errors, out, err, loader, filter); }
From source file:z.zee.Z.java
License:Open Source License
/** * Make a new Nashorn Context to compile and/or run JavaScript files. * * @param in input stream for Z/*from w w w. jav a 2 s . co m*/ * @param out output stream for Z * @param err error stream for Z * @param args arguments to Z * * @return null if there are problems with option parsing. */ @SuppressWarnings("resource") private static Context makeContext(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) { final PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out); final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : new PrintStream(err); final PrintWriter wout = new PrintWriter(pout, true); final PrintWriter werr = new PrintWriter(perr, true); // Set up error handler. final ErrorManager errors = new ErrorManager(werr); // Set up options. final Options options = new Options("nashorn", werr); // parse options if (args != null) { try { options.process(args); } catch (final IllegalArgumentException e) { werr.println(bundle.getString("shell.usage")); options.displayHelp(e); return null; } } // detect scripting mode by any source's first character being '#' scripting = options.getBoolean("scripting"); if (!scripting) { for (final String fileName : options.getFiles()) { final File firstFile = new File(fileName); if (firstFile.isFile()) { try (final FileReader fr = new FileReader(firstFile)) { final int firstChar = fr.read(); // starts with '# if (firstChar == '#') { options.set("scripting", true); break; } } catch (final IOException e) { // ignore this. File IO errors will be reported later anyway } } } } return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); }