Search in sources :

Example 91 with SimpleBindings

use of javax.script.SimpleBindings in project yyl_example by Relucent.

the class CompilableTest method main.

public static void main(String[] args) {
    try {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        String script = "print('hello')";
        Bindings bindings = new SimpleBindings();
        if (engine instanceof Compilable) {
            System.out.println("Compiling....");
            Compilable compEngine = (Compilable) engine;
            CompiledScript cs = compEngine.compile(script);
            cs.eval(bindings);
        } else {
            engine.eval(script, bindings);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 92 with SimpleBindings

use of javax.script.SimpleBindings in project yyl_example by Relucent.

the class RhinoJsEngineExample method main.

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("rhino");
    Bindings bindings = new SimpleBindings();
    bindings.put("map", new HashMap<>());
    bindings.put("_", new PrimaryLibrary());
    String script = IoUtil.getResourceAsString(RhinoJsEngineExample.class, "sample.rhino.js");
    Object result = engine.eval(script, bindings);
    System.out.println(result);
}
Also used : SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 93 with SimpleBindings

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

the class CompilableTest method compileStringWithSimpleBindings.

@Test
public void compileStringWithSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 6);
    assertThat(script.eval(bindings), instanceOfWith(Number.class, is(numberCloseTo(12))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 94 with SimpleBindings

use of javax.script.SimpleBindings 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)

Example 95 with SimpleBindings

use of javax.script.SimpleBindings 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)

Aggregations

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4