Search in sources :

Example 76 with Bindings

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

the class CompilableTest method compileStringWithContextAndBindings.

@Test
public void compileStringWithContextAndBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    ScriptContext context = new SimpleScriptContext();
    Bindings bindings = engine.createBindings();
    bindings.put("numberVal", 8);
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(16))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 77 with Bindings

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

the class CompilableTest method compileStringWithBindings.

@Test
public void compileStringWithBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    Bindings bindings = engine.createBindings();
    bindings.put("numberVal", 5);
    assertThat(script.eval(bindings), instanceOfWith(Number.class, is(numberCloseTo(10))));
}
Also used : CompiledScript(javax.script.CompiledScript) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 78 with Bindings

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

the class CompilableTest method compileStringWithSimpleBindings.

@Test
public void compileStringWithSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 6);
    assertThat(script.eval(bindings), instanceOfWith(Number.class, is(numberCloseTo(12))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 79 with Bindings

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

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

the class TypeConversionTest method testUnsupportedWithBindings.

@Test
public void testUnsupportedWithBindings() throws ScriptException {
    // Unsupported Java classes end up as `null` in default bindings
    Bindings bindings = engine.createBindings();
    Object javaObject = new JavaObject();
    bindings.put("javaObject", javaObject);
    assertThat(bindings.get("javaObject"), nullValue());
    assertThat(engine.eval("javaObject", bindings), nullValue());
    assertThat(engine.eval("typeof javaObject", bindings), instanceOfWith(String.class, is("object")));
    assertThat(engine.eval("javaObject == null", bindings), instanceOfWith(Boolean.class, sameInstance(Boolean.TRUE)));
    assertThat(engine.eval("javaObject === void 0", bindings), instanceOfWith(Boolean.class, sameInstance(Boolean.FALSE)));
    assertThat(engine.eval("javaObject === null", bindings), instanceOfWith(Boolean.class, sameInstance(Boolean.TRUE)));
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.junit.Test)

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