Search in sources :

Example 96 with SimpleBindings

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

the class ScriptEngineScopeTest method simpleBindingsRetrieval.

@Test
public void simpleBindingsRetrieval() throws ScriptException {
    Bindings bindings = new SimpleBindings();
    engine.eval("var a = 0; b = 3", bindings);
    assertThat(bindings.get("a"), instanceOfWith(Number.class, is(numberCloseTo(0))));
    assertThat(bindings.get("b"), nullValue());
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 97 with SimpleBindings

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

the class ScriptEngineScopeTest method accessToBuiltinsNonSharedSimpleBindings.

@Test
public void accessToBuiltinsNonSharedSimpleBindings() throws ScriptException {
    Object defaultObject = engine.eval("Object");
    Object bindingsObject = engine.eval("Object", new SimpleBindings());
    assertThat(defaultObject, notNullValue());
    assertThat(bindingsObject, notNullValue());
    assertThat(defaultObject, Matchers.not(sameInstance(bindingsObject)));
}
Also used : SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 98 with SimpleBindings

use of javax.script.SimpleBindings 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 99 with SimpleBindings

use of javax.script.SimpleBindings 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 100 with SimpleBindings

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

the class ScriptEngineScopeTest method isolatedBindingsAndSimpleBindings.

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

Aggregations

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4