Search in sources :

Example 6 with Bindings

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

the class CompilableTest method compileStringWithContextAndSimpleBindings.

@Test
public void compileStringWithContextAndSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    ScriptContext context = new SimpleScriptContext();
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 9);
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(18))));
}
Also used : CompiledScript(javax.script.CompiledScript) 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 7 with Bindings

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

the class ScriptEngineScopeTest method explicitDefaultBindingsRetrieval.

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

Example 8 with Bindings

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

the class ScriptEngineScopeTest method bindingValueWithThisExplicitSimpleBinding.

@Test
public void bindingValueWithThisExplicitSimpleBinding() throws ScriptException {
    Bindings binding = new SimpleBindings();
    binding.put("value", "Sabik");
    assertThat(engine.eval("this.value", binding), nullValue());
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 9 with Bindings

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

the class ScriptEngineScopeTest method isolatedBindings.

@Test
public void isolatedBindings() throws ScriptException {
    Bindings binding = engine.createBindings();
    engine.eval("var value = 'Betelgeuse'", binding);
    assertThat(engine.eval("value", binding), instanceOfWith(String.class, is("Betelgeuse")));
    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)

Example 10 with Bindings

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

the class ScriptEngineScopeTest method isolatedBindingsWithContextAndSimpleBindings.

@Test
public void isolatedBindingsWithContextAndSimpleBindings() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    Bindings binding = new SimpleBindings();
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Zeta Scorpii'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Zeta Scorpii")));
    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("Zeta Scorpii")));
}
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)

Aggregations

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