Search in sources :

Example 1 with NashornScriptEngine

use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.

the class Nashorn8 method main.

public static void main(String[] args) throws ScriptException, NoSuchMethodException {
    NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn8.js')");
    // [object global]
    engine.invokeFunction("evaluate1");
    // [object Object]
    engine.invokeFunction("evaluate2");
    // Foobar
    engine.invokeFunction("evaluate3", "Foobar");
    // [object global] <- ???????
    engine.invokeFunction("evaluate3", new Person("John", "Doe"));
}
Also used : NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) ScriptEngineManager(javax.script.ScriptEngineManager) Person(com.winterbe.java8.samples.lambda.Person)

Example 2 with NashornScriptEngine

use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.

the class Nashorn11 method test7.

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);
}
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 3 with NashornScriptEngine

use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.

the class Nashorn11 method test8.

private static void test8() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var obj = { 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("obj", defaultBindings.get("obj"));
    engine.eval("obj.foo = 44;", context1);
    engine.eval("print(obj.foo);", context1);
    engine.eval("print(obj.foo);", context2);
}
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 4 with NashornScriptEngine

use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.

the class Nashorn11 method test4.

private static void test4() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("function foo() { print('bar') };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context = new SimpleScriptContext();
    context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("foo();", context);
    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 5 with NashornScriptEngine

use of jdk.nashorn.api.scripting.NashornScriptEngine in project java8-tutorial by winterbe.

the class Nashorn11 method test5.

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);
}
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)

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