Search in sources :

Example 36 with Bindings

use of javax.script.Bindings 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)

Example 37 with Bindings

use of javax.script.Bindings 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 38 with Bindings

use of javax.script.Bindings 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 39 with Bindings

use of javax.script.Bindings 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 40 with Bindings

use of javax.script.Bindings in project incubator-atlas by apache.

the class Titan1Graph method executeGremlinScript.

@Override
public Object executeGremlinScript(ScriptEngine scriptEngine, Map<? extends String, ? extends Object> userBindings, String query, boolean isPath) throws ScriptException {
    Bindings bindings = scriptEngine.createBindings();
    bindings.putAll(userBindings);
    bindings.put("g", getGraph());
    Object result = scriptEngine.eval(query, bindings);
    return convertGremlinValue(result);
}
Also used : Bindings(javax.script.Bindings)

Aggregations

Bindings (javax.script.Bindings)150 SimpleBindings (javax.script.SimpleBindings)77 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)30 ScriptContext (javax.script.ScriptContext)26 SimpleScriptContext (javax.script.SimpleScriptContext)24 ScriptEngine (javax.script.ScriptEngine)20 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 Map (java.util.Map)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 ScriptEngineManager (javax.script.ScriptEngineManager)9 PrintWriter (java.io.PrintWriter)8 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)7