Search in sources :

Example 81 with Bindings

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

the class ScriptEngineScopeTest method simpleBindingsAndEval.

@Test
public void simpleBindingsAndEval() throws ScriptException {
    Bindings bindings = new SimpleBindings();
    assertThat(bindings, notNullValue());
    assertThat(bindings.entrySet(), is(empty()));
    bindings.put("nullValue", null);
    bindings.put("intValue", 1);
    bindings.put("doubleValue", 1.5);
    bindings.put("booleanValue", true);
    bindings.put("stringValue", "Cepheus");
    Object nullValue = engine.eval("nullValue", bindings);
    Object intValue = engine.eval("intValue", bindings);
    Object doubleValue = engine.eval("doubleValue", bindings);
    Object stringValue = engine.eval("stringValue", bindings);
    Object booleanValue = engine.eval("booleanValue", bindings);
    assertThat(nullValue, nullValue());
    assertThat(intValue, instanceOfWith(Number.class, is(numberCloseTo(1))));
    assertThat(doubleValue, instanceOfWith(Number.class, is(numberCloseTo(1.5))));
    assertThat(stringValue, instanceOfWith(String.class, is("Cepheus")));
    assertThat(booleanValue, instanceOfWith(Boolean.class, sameInstance(Boolean.TRUE)));
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 82 with Bindings

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

the class ScriptEngineScopeTest method globalScopeAccessNewEmptyContext.

@Test
public void globalScopeAccessNewEmptyContext() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Gacrux");
    assertThat(engine.eval("typeof globalVar", new SimpleScriptContext()), instanceOfWith(String.class, is("undefined")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 83 with Bindings

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

the class ScriptEngineScopeTest method globalScopeAccessNewContextWithGlobal.

@Test
public void globalScopeAccessNewContextWithGlobal() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Achernar");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Achernar")));
}
Also used : 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 84 with Bindings

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

the class ScriptEngineTest method evalExplicitBindings.

@Test
public void evalExplicitBindings() throws ScriptException {
    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    assertThat(bindings, notNullValue());
    Object value = engine.eval("function f() { return 'vvv' } f()", bindings);
    assertThat(value, instanceOfWith(String.class, is("vvv")));
}
Also used : Bindings(javax.script.Bindings) Test(org.junit.Test)

Example 85 with Bindings

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

the class ScriptEngineTest method getAndPut.

@Test
public void getAndPut() throws ScriptException {
    final String key1 = "key1", key2 = "key2";
    final String value1 = "value1", value2 = "value2", value3 = "value3";
    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    // key-value is initially null
    assertThat(engine.get(key1), nullValue());
    assertThat(engineScope.get(key1), nullValue());
    // can set from engine key-value and retrieve values from engine and bindings
    engine.put(key1, value1);
    assertThat(engine.get(key1), instanceOfWith(String.class, is(value1)));
    assertThat(engineScope.get(key1), instanceOfWith(String.class, is(value1)));
    // can override from engine key-value and retrieve new values from engine and bindings
    engine.put(key1, value2);
    assertThat(engine.get(key1), instanceOfWith(String.class, is(value2)));
    assertThat(engineScope.get(key1), instanceOfWith(String.class, is(value2)));
    // can override key-value from bindings and retrieve new values from engine and bindings
    Object oldValueA = engineScope.put(key1, value3);
    assertThat(oldValueA, instanceOfWith(String.class, is(value2)));
    assertThat(engine.get(key1), instanceOfWith(String.class, is(value3)));
    assertThat(engineScope.get(key1), instanceOfWith(String.class, is(value3)));
    // can set key-value from bindings and retrieve values from engine and bindings
    Object oldValueB = engineScope.put(key2, value1);
    assertThat(oldValueB, nullValue());
    assertThat(engine.get(key2), instanceOfWith(String.class, is(value1)));
    assertThat(engineScope.get(key2), instanceOfWith(String.class, is(value1)));
    // can override key-value from bindings and retrieve new values from engine and bindings
    Object oldValueC = engineScope.put(key2, value2);
    assertThat(oldValueC, instanceOfWith(String.class, is(value1)));
    assertThat(engine.get(key2), instanceOfWith(String.class, is(value2)));
    assertThat(engineScope.get(key2), instanceOfWith(String.class, is(value2)));
    // can override key-value from engine and retrieve new values from engine and bindings
    engine.put(key2, value3);
    assertThat(engine.get(key2), instanceOfWith(String.class, is(value3)));
    assertThat(engineScope.get(key2), instanceOfWith(String.class, is(value3)));
}
Also used : 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