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