Search in sources :

Example 66 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method isolatedContextsNoVarEvalContext.

@Test
public void isolatedContextsNoVarEvalContext() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    engine.eval("value = 'Deneb'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Deneb")));
    assertThat(engine.eval("typeof value"), instanceOfWith(String.class, is("undefined")));
    assertThat(engine.eval("typeof value", new SimpleScriptContext()), instanceOfWith(String.class, is("undefined")));
    assertThat(engine.getContext().getAttribute("value"), nullValue());
    assertThat(context.getAttribute("value"), instanceOfWith(String.class, is("Deneb")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 67 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method isolatedBindingsWithContext.

@Test
public void isolatedBindingsWithContext() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    Bindings binding = engine.createBindings();
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Alnilam'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Alnilam")));
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Alnilam")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 68 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessNewContextWithGlobalSimpleBindings.

@Test
public void globalScopeAccessNewContextWithGlobalSimpleBindings() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Sigma Sagittarii");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Sigma Sagittarii")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 69 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method scopeInteractionNewContextVarAssignment.

@Test
public void scopeInteractionNewContextVarAssignment() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
    engine.eval("var value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), instanceOfWith(String.class, is("Aludra")));
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.GLOBAL_SCOPE), instanceOfWith(String.class, is("Scheat")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 70 with SimpleScriptContext

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

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2