List of usage examples for jdk.nashorn.internal.runtime Context createGlobal
public Global createGlobal()
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 w w. ja v a 2 s.c o m*/ 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/ /*//from w w w .ja v a 2 s.co 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:io.stallion.plugins.javascript.JavascriptShell.java
License:Open Source License
public int runShell(String[] args) throws IOException, ScriptException { OutputStream out = System.out; OutputStream err = System.err; InputStream in = System.in; Context context = makeContext(in, out, err, args); Global global = context.createGlobal(); if (context == null) { return 100; } else {//from w w w . ja va2s . c o m return readEvalPrint(in, out, context, global); } }
From source file:me.finalchild.nashornbukkit.util.BukkitImporter.java
License:MIT License
public static Set<String> getUsedIdentifiers(Path file) throws IOException { Set<String> result = new HashSet<>(); String text = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); String json;/*from w w w. j a v a2 s. c o m*/ Options options = new Options(""); ErrorManager errorManager = new ErrorManager(); Context context = new Context(options, errorManager, NashornBukkit.class.getClassLoader()); Context.setGlobal(context.createGlobal()); json = ScriptUtils.parse(text, file.getFileName().toString(), false); JsonParser jsonParser = new JsonParser(); JsonObject rootObj = jsonParser.parse(json).getAsJsonObject(); addUsedIdentifiers(rootObj, result); return result; }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * Run method logic./*from w ww . java 2s. c om*/ * * @param in input stream for Shell * @param out output stream for Shell * @param err error stream for Shell * * @return exit code * * @throws IOException if there's a problem setting up the streams */ protected final int run(final InputStream in, final OutputStream out, final OutputStream err) throws IOException { final Context context = makeContext(in, out, err); if (context == null) { return COMMANDLINE_ERROR; } final ScriptObject global = context.createGlobal(); final ScriptEnvironment env = context.getEnv(); final List<String> files = env.getFiles(); // Prepare bindings here. SimpleScriptContext scriptContext = new SimpleScriptContext(); ScriptObjectMirror mirror = (ScriptObjectMirror) ScriptObjectMirror.wrap(global, global); scriptContext.setBindings(mirror, ScriptContext.ENGINE_SCOPE); bindings.accept(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)); // TODO: JDK 1.8u65 method only. Will invoke old call if fails. try { Global g = ((Global) global); ScriptEngine s = new ScriptEngineManager().getEngineByName("nashorn"); try { Method m = Global.class.getMethod("initBuiltinObjects", ScriptEngine.class, ScriptContext.class); m.invoke(g, s, scriptContext); } catch (NoSuchMethodException e) { try { Method m = Global.class.getMethod("initBuiltinObjects", ScriptEngine.class); m.invoke(g, s); g.setScriptContext(scriptContext); } catch (NoSuchMethodException ee) { System.err.println("COULD NOT INIT!"); throw new IOException("COULD NOT INIT!"); } } } catch (Exception ignored) { } if (files.isEmpty()) { return readEvalPrint(context, global); } if (env._compile_only) { return compileScripts(context, global, files); } if (env._fx) { return runFXScripts(context, global, files); } return runScripts(context, global, files); }
From source file:z.zee.Z.java
License:Open Source License
/** * Run method logic.// w w w . jav a 2 s. c om * * @param in input stream for Z * @param out output stream for Z * @param err error stream for Z * @param args arguments to Z * * @return exit code * * @throws IOException if there's a problem setting up the streams */ protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { final Context context = makeContext(in, out, err, args); if (context == null) { return COMMANDLINE_ERROR; } final ScriptObject global = context.createGlobal(); final ScriptEnvironment env = context.getEnv(); final List<String> files = env.getFiles(); if (env.getArguments().isEmpty() && !scripting) { context.getErr().println(bundle.getString("shell.usage")); return COMPILATION_ERROR; } if (files.isEmpty()) { return readEvalPrint(context, global); } if (env._compile_only) { return compileScripts(context, global, files); } return runScripts(context, global, files); }