use of javax.script.SimpleScriptContext in project es6draft by anba.
the class ScriptEngineScopeTest method isolatedContextsNoVarEvalContext.
@Test
public void isolatedContextsNoVarEvalContext() throws ScriptException {
ScriptContext context = new SimpleScriptContext();
context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
engine.eval("value = 'Deneb'", context);
assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Deneb")));
assertThat(engine.eval("typeof value"), instanceOfWith(String.class, is("undefined")));
assertThat(engine.eval("typeof value", new SimpleScriptContext()), instanceOfWith(String.class, is("undefined")));
assertThat(engine.getContext().getAttribute("value"), nullValue());
assertThat(context.getAttribute("value"), instanceOfWith(String.class, is("Deneb")));
}
use of javax.script.SimpleScriptContext 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")));
}
use of javax.script.SimpleScriptContext 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")));
}
use of javax.script.SimpleScriptContext in project es6draft by anba.
the class ScriptEngineScopeTest method scopeInteractionNewContextVarAssignment.
@Test
public void scopeInteractionNewContextVarAssignment() throws ScriptException {
ScriptContext context = new SimpleScriptContext();
context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
engine.eval("var value = 'Aludra'", context);
assertThat(engine.eval("this.value", context), instanceOfWith(String.class, is("Aludra")));
assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Aludra")));
assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Aludra")));
assertThat(context.getAttribute("value", ScriptContext.GLOBAL_SCOPE), instanceOfWith(String.class, is("Scheat")));
}
use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.
the class Nashorn11 method test6.
private static void test6() throws ScriptException {
NashornScriptEngine engine = createEngine();
ScriptContext defaultContext = engine.getContext();
defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo", "hello");
ScriptContext customContext = new SimpleScriptContext();
customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.ENGINE_SCOPE);
Bindings bindings = new SimpleBindings();
bindings.put("foo", "world");
customContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
// engine.eval("foo = 23;"); // overrides foo in all contexts, why???
// hello
engine.eval("print(foo)");
// world
engine.eval("print(foo)", customContext);
// hello
engine.eval("print(foo)", defaultContext);
}
Aggregations