Search in sources :

Example 56 with SimpleScriptContext

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);
}
Also used : ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine)

Example 57 with SimpleScriptContext

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))));
}
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 58 with SimpleScriptContext

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))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 59 with SimpleScriptContext

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());
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 60 with SimpleScriptContext

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")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2