Search in sources :

Example 6 with NashornScriptEngine

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();");
}
Also used : NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine)

Example 7 with NashornScriptEngine

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);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 8 with NashornScriptEngine

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);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine)

Example 9 with NashornScriptEngine

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"));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 10 with NashornScriptEngine

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));
}
Also used : NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) ScriptEngineManager(javax.script.ScriptEngineManager)

Aggregations

NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)11 SimpleScriptContext (javax.script.SimpleScriptContext)7 Bindings (javax.script.Bindings)6 ScriptContext (javax.script.ScriptContext)6 SimpleBindings (javax.script.SimpleBindings)6 ScriptEngineManager (javax.script.ScriptEngineManager)3 Person (com.winterbe.java8.samples.lambda.Person)1