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)));
}
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")));
}
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")));
}
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")));
}
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)));
}
Aggregations