use of javax.script.SimpleScriptContext in project CitizensAPI by CitizensDev.
the class ScriptCompiler method run.
/**
* Run the given source code.
*
* @param code
* the source code to compile/run
* @param extension
* the code extension e.g. js
* @param vars
* variables to pass to the script
*/
public void run(String code, String extension, Map<String, Object> vars) throws ScriptException {
ScriptEngine engine = loadEngine(extension);
if (engine == null)
throw new ScriptException("Couldn't load engine with extension " + extension);
ScriptContext context = new SimpleScriptContext();
if (vars != null) {
context.setBindings(new SimpleBindings(vars), ScriptContext.ENGINE_SCOPE);
}
engine.eval(extension, context);
}
use of javax.script.SimpleScriptContext 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))));
}
use of javax.script.SimpleScriptContext in project es6draft by anba.
the class CompilableTest method compileStringWithContext.
@Test
public void compileStringWithContext() throws ScriptException {
CompiledScript script = compilable.compile("numberVal * 2");
ScriptContext context = new SimpleScriptContext();
context.setAttribute("numberVal", 7, ScriptContext.ENGINE_SCOPE);
assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(14))));
}
use of javax.script.SimpleScriptContext in project es6draft by anba.
the class ScriptEngineScopeTest method isolatedContextsNoVarEvalContextWithDefaults.
@Test
public void isolatedContextsNoVarEvalContextWithDefaults() throws ScriptException {
ScriptContext context = new SimpleScriptContext();
engine.eval("value = 'Polaris'", context);
assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
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"), nullValue());
}
use of javax.script.SimpleScriptContext in project es6draft by anba.
the class ScriptEngineScopeTest method scopeInteractionNewContextSimpleBindingsVarAssignment.
@Test
public void scopeInteractionNewContextSimpleBindingsVarAssignment() throws ScriptException {
ScriptContext context = new SimpleScriptContext();
context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
context.setBindings(new SimpleBindings(), 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), nullValue());
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")));
}
Aggregations