private static void test5() throws ScriptException { NashornScriptEngine engine = createEngine(); engine.eval("var obj = { foo: 'foo' };"); engine.eval("function printFoo() { print(obj.foo) };"); ScriptContext defaultContext = engine.getContext(); Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); SimpleScriptContext context1 = new SimpleScriptContext(); context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); SimpleScriptContext context2 = new SimpleScriptContext(); context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); engine.eval("obj.foo = 'bar';", context1); engine.eval("printFoo();", context1); engine.eval("printFoo();", context2); }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('java8-nashorn/src/main/resources/nashorn8.js')"); engine.invokeFunction("evaluate1"); // [object global] engine.invokeFunction("evaluate2"); // [object Object] engine.invokeFunction("evaluate3", "Foobar"); // Foobar engine.invokeFunction("evaluate3", new Person("John", "Doe")); // [object global] <- ??????? }
public NashornRuntime(final Vertx vertx, NashornScriptEngine engine) { this.engine = engine; try { final Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); // remove the exit and quit functions engineBindings.remove("exit"); engineBindings.remove("quit"); final Bindings globalBindings = new SimpleBindings(); // add vertx as a global globalBindings.put("vertx", vertx); // add the global reference to the bindings globalBindings.put("global", engine.eval("this")); engine.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE); // add polyfills engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/object.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/json.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/global.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/date.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/console.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/promise.js"); engine.invokeFunction("load", "classpath:io/reactiverse/es4x/polyfill/worker.js"); // install the commonjs loader module = engine.invokeFunction("load", "classpath:io/reactiverse/es4x/jvm-npm.js"); // get a reference to the require function require = (JSObject) engine.get("require"); } catch (ScriptException | NoSuchMethodException e) { throw new RuntimeException(e); } }
private Module compileJavaScriptModule(Folder parent, String fullPath, String code) throws ScriptException { Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE); Bindings module = createSafeBindings(); module.putAll(engineScope); String dirname = fullPath.substring(0, Math.max(fullPath.length() - filename.length() - 1, 0)); String previousFilename = (String) engine.get(ScriptEngine.FILENAME); engine.put(ScriptEngine.FILENAME, fullPath); engine.eval( "(function (exports, require, module, __filename, __dirname) {" + code + "\n})"); function.call(created, created.exports, created, created.module, filename, dirname); } finally { engine.put(ScriptEngine.FILENAME, previousFilename);
public ScriptExecutor(Collection<Pair<String, String>> libraries, String scriptName, String scriptCode, Map<String,Object> globalObjects) { scriptEngine = (NashornScriptEngine)new NashornScriptEngineFactory().getScriptEngine(); bindings = new SimpleBindings(); bindings.putAll(globalObjects); scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); libraries.forEach(p->{ bindings.put(NASHORN_SCRIPT_FILE_NAME, p.getKey()); try { scriptEngine.eval(p.getValue()); } catch (ScriptException e) { throw new RuntimeException(e); } }); try { bindings.put(NASHORN_SCRIPT_FILE_NAME, scriptName); compiledScript = scriptEngine.compile(scriptCode); } catch (ScriptException e) { throw new RuntimeException(e); } }
/** * Public only for tests */ public void initSpaceGlobal() { Bindings bindings = SCRIPT_ENGINE.createBindings(); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); try { setSpace(Space.this); try { scriptContext.setAttribute(ScriptEngine.FILENAME, INTERNALS_MODULENAME, ScriptContext.ENGINE_SCOPE); SCRIPT_ENGINE.eval(new URLReader(internalsUrl), scriptContext); } finally { setSpace(null); } } catch (ScriptException ex) { Logger.getLogger(Scripts.class.getName()).log(Level.SEVERE, null, ex); } }
public CompiledScript compile(String s) throws ScriptException { return engine.compile(s); }
@Override public Object invokeFunction(String function, Object... args) { try { return engine.invokeFunction(function, args); } catch (ScriptException | NoSuchMethodException e) { throw new RuntimeException(e); } }
public static Module enable(NashornScriptEngine engine, Folder folder, Bindings bindings) throws ScriptException { Bindings module = engine.createBindings(); Bindings exports = engine.createBindings(); Module created = new Module(engine, folder, new ModuleCache(), "<main>", module, exports, null, null); created.setLoaded(); bindings.put("require", created); bindings.put("module", module); bindings.put("exports", exports); return created; } }
public Object execute(Map<String,Object> variables) { bindings.putAll(variables); try { scriptEngine.getContext().setBindings(bindings,ScriptContext.ENGINE_SCOPE); return compiledScript.eval(); } catch (ScriptException e) { throw new RuntimeException(e); } finally { variables.keySet().forEach(bindings::remove); } }
public static Module enable(NashornScriptEngine engine, Folder folder) throws ScriptException { Bindings global = engine.getBindings(ScriptContext.ENGINE_SCOPE); return enable(engine, folder, global); }
private Module compileJavaScriptModule(Folder parent, String fullPath, String code) throws ScriptException { Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE); Bindings module = createSafeBindings(); module.putAll(engineScope); String dirname = fullPath.substring(0, Math.max(fullPath.length() - filename.length() - 1, 0)); String previousFilename = (String) engine.get(ScriptEngine.FILENAME); engine.put(ScriptEngine.FILENAME, fullPath); engine.eval( "(function (exports, require, module, __filename, __dirname) {" + code + "\n})"); function.call(created, created.exports, created, created.module, filename, dirname); } finally { engine.put(ScriptEngine.FILENAME, previousFilename);
public CompiledScript compileUnchecked(String s) { try { return engine.compile(s); }catch (ScriptException e) { throw new IllegalStateException(e.getMessage(), e); } }
/** * Compile the given HTML template to JS functions using vue-template-compiler. * * @param htmlTemplate The HTML Component template to compile * @return An object containing the render functions * @throws VueTemplateCompilerException If the compilation fails */ public VueTemplateCompilerResult compile(String htmlTemplate) throws VueTemplateCompilerException { ScriptObjectMirror templateCompilerResult; try { templateCompilerResult = (ScriptObjectMirror) engine.invokeFunction("compile", htmlTemplate); } catch (ScriptException | NoSuchMethodException e) { e.printStackTrace(); throw new VueTemplateCompilerException( "An error occurred while compiling the template: " + htmlTemplate + " -> " + e.getMessage()); } String renderFunction = (String) templateCompilerResult.get("render"); String[] staticRenderFunctions = ((ScriptObjectMirror) templateCompilerResult.get("staticRenderFns")).to(String[].class); return new VueTemplateCompilerResult(renderFunction, staticRenderFunctions); } }
public static Module enable(NashornScriptEngine engine, Folder folder, Bindings bindings) throws ScriptException { Bindings module = engine.createBindings(); Bindings exports = engine.createBindings(); Module created = new Module(engine, folder, new ModuleCache(), "<main>", module, exports, null, null); created.setLoaded(); bindings.put("require", created); bindings.put("module", module); bindings.put("exports", exports); return created; } }
@Override public void config(final JsonObject config) { final Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); // expose the config as a global engineBindings.put("config", config != null ? config.getMap() : null); }
private static void test7() throws ScriptException { NashornScriptEngine engine = createEngine(); engine.eval("var foo = 23;"); ScriptContext defaultContext = engine.getContext(); Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); SimpleScriptContext context1 = new SimpleScriptContext(); context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); SimpleScriptContext context2 = new SimpleScriptContext(); context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo", defaultBindings.get("foo")); engine.eval("foo = 44;", context1); engine.eval("print(foo);", context1); engine.eval("print(foo);", context2); }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('java8-nashorn/src/main/resources/nashorn10.js')"); long t0 = System.nanoTime(); for (int i = 0; i < 100000; i++) { engine.invokeFunction("testPerf"); } long took = System.nanoTime() - t0; System.out.format("Elapsed time: %d ms", TimeUnit.NANOSECONDS.toMillis(took)); } }