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 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)); } }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('java8-nashorn/src/main/resources/nashorn9.js')"); long t0 = System.nanoTime(); double result = 0; for (int i = 0; i < 1000; i++) { double num = (double) engine.invokeFunction("testPerf"); result += num; } System.out.println(result > 0); long took = System.nanoTime() - t0; System.out.format("Elapsed time: %d ms", TimeUnit.NANOSECONDS.toMillis(took)); } }
@Override public Object invokeFunction(String function, Object... args) { try { return engine.invokeFunction(function, args); } catch (ScriptException | NoSuchMethodException e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('res/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 static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('res/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)); } }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('res/nashorn9.js')"); long t0 = System.nanoTime(); double result = 0; for (int i = 0; i < 1000; i++) { double num = (double) engine.invokeFunction("testPerf"); result += num; } System.out.println(result > 0); long took = System.nanoTime() - t0; System.out.format("Elapsed time: %d ms", TimeUnit.NANOSECONDS.toMillis(took)); } }
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); } }
/** * 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); } }