use of javax.script.ScriptEngine in project enumerable by hraberg.
the class ClojureTest method interactingWithJavaScript.
@Test
public void interactingWithJavaScript() throws Exception {
ScriptEngine js = JavaScriptTest.getJavaScriptEngine();
Function f = (Function) js.eval("var f = function(n, m) { return n * m; }; f;");
IFn times = toIFn(LambdaJavaScript.toFn2(f));
assertEquals(6.0, times.invoke(2, 3));
defn("times-js", times);
assertEquals(120.0, clj.eval("(reduce times-js 1 [1, 2, 3, 4, 5])"));
}
use of javax.script.ScriptEngine in project enumerable by hraberg.
the class JRubyTest method interactingWithGroovy.
@Test
public void interactingWithGroovy() throws Exception {
Ruby ruby = Ruby.getGlobalRuntime();
ScriptEngine groovy = GroovyTest.getGroovyEngine();
Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
RubyProc proc = toProc(LambdaGroovy.toFn2(closure));
assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
rb.put("block", proc);
assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
use of javax.script.ScriptEngine in project jOOQ by jOOQ.
the class NashornTest method testScripts.
@Test
public void testScripts() throws Exception {
Stream.of(new File(getClass().getResource("/org/jooq/example/test").toURI()).listFiles((dir, name) -> name.endsWith(".js"))).forEach(file -> {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
Bindings bindings = engine.getBindings(ENGINE_SCOPE);
bindings.put("connection", connection);
try {
engine.eval(new FileReader(file));
} catch (Exception e) {
throw new RuntimeException("Error while running " + file, e);
}
});
}
use of javax.script.ScriptEngine in project groovy by apache.
the class JSR223SpecTest method testInvocableFunction.
@Test
public void testInvocableFunction() throws ScriptException, NoSuchMethodException {
// tag::jsr223_invocable[]
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }";
engine.eval(fact);
Invocable inv = (Invocable) engine;
Object[] params = { 5 };
Object result = inv.invokeFunction("factorial", params);
assertEquals(new Integer(120), result);
// end::jsr223_invocable[]
}
use of javax.script.ScriptEngine in project groovy by apache.
the class CustomCodeVisitorSupport method secureEval.
private void secureEval(ClassLoaderDefinitionType classLoaderDefType) throws Exception {
ScriptEngine engine = createScriptEngine(classLoaderDefType);
GroovySecurityManager securityMgr = GroovySecurityManager.instance();
securityMgr.overrideGroovyClassLoader(engine, classLoaderDefType);
securityMgr.forbid(testFixture.forbiddenInstruction);
engine.eval(testFixture.script);
}
Aggregations