use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.
the class Nashorn11 method test1.
private static void test1() throws ScriptException {
NashornScriptEngine engine = createEngine();
engine.eval("function foo() { print('bar') };");
engine.eval("foo();");
}
use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.
the class Nashorn11 method test6.
private static void test6() throws ScriptException {
NashornScriptEngine engine = createEngine();
ScriptContext defaultContext = engine.getContext();
defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo", "hello");
ScriptContext customContext = new SimpleScriptContext();
customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.ENGINE_SCOPE);
Bindings bindings = new SimpleBindings();
bindings.put("foo", "world");
customContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
// engine.eval("foo = 23;"); // overrides foo in all contexts, why???
// hello
engine.eval("print(foo)");
// world
engine.eval("print(foo)", customContext);
// hello
engine.eval("print(foo)", defaultContext);
}
use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.
the class Nashorn11 method test2.
private static void test2() throws ScriptException {
NashornScriptEngine engine = createEngine();
engine.eval("function foo() { print('bar') };");
SimpleScriptContext context = new SimpleScriptContext();
engine.eval("print(Function);", context);
engine.eval("foo();", context);
}
use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.
the class Nashorn11 method test3.
private static void test3() throws ScriptException {
NashornScriptEngine engine = createEngine();
ScriptContext defaultContext = engine.getContext();
Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
SimpleScriptContext context = new SimpleScriptContext();
context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
engine.eval("function foo() { print('bar') };", context);
engine.eval("foo();", context);
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
System.out.println(bindings.get("foo"));
System.out.println(context.getAttribute("foo"));
}
use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.
the class Nashorn9 method main.
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));
}
Aggregations