Search in sources :

Example 6 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class ScalaTest method interactingWithGroovy.

@Test
public void interactingWithGroovy() throws Exception {
    ScriptEngine groovy = GroovyTest.getGroovyEngine();
    Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
    Function2<Object, Object, Object> times = toFunction(LambdaGroovy.toFn2(closure));
    assertEquals(6, times.apply(2, 3));
    scala.bind("timesGroovy", "Function2[Any, Any, Any]", times);
    assertEquals(120, scala.eval("List(1, 2, 3, 4, 5).reduceLeft(timesGroovy)"));
}
Also used : Closure(groovy.lang.Closure) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 7 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class ScalaTest 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;");
    Function2<Object, Object, Object> times = toFunction(LambdaJavaScript.toFn2(f));
    assertEquals(6.0, times.apply(2, 3));
    scala.bind("timesJS", "Function2[Any, Any, Any]", times);
    assertEquals(120.0, scala.eval("List(1, 2, 3, 4, 5).reduceLeft(timesJS)"));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 8 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class GroovyTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    Closure<?> closure = toClosure(LambdaJRuby.toFn2(proc));
    groovy.put("f", closure);
    assertEquals(6L, groovy.eval("f(2, 3)"));
}
Also used : RubyProc(org.jruby.RubyProc) ScriptEngine(javax.script.ScriptEngine) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 9 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class GroovyTest 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;");
    ClosureFn2 closure = toClosure(LambdaJavaScript.toFn2(f));
    assertEquals(6.0, closure.call(new Object[] { 2, 3 }));
    groovy.put("closure", closure);
    assertEquals(120.0, groovy.eval("[1, 2, 3, 4, 5].inject(1, closure)"));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) ClosureFn2(org.enumerable.lambda.support.groovy.LambdaGroovy.ClosureFn2) ScriptEngine(javax.script.ScriptEngine) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 10 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class JavaScriptTest method interactingWithGroovy.

@Test
public void interactingWithGroovy() throws Exception {
    ScriptEngine groovy = GroovyTest.getGroovyEngine();
    Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
    FunctionFn2 f = toFunction(LambdaGroovy.toFn2(closure));
    js.put("f", f);
    assertEquals(6.0, js.eval("f(2, 3)"));
}
Also used : Closure(groovy.lang.Closure) FunctionFn2(org.enumerable.lambda.support.javascript.LambdaJavaScript.FunctionFn2) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest)

Aggregations

ScriptEngine (javax.script.ScriptEngine)147 ScriptEngineManager (javax.script.ScriptEngineManager)49 ScriptException (javax.script.ScriptException)38 Test (org.junit.Test)26 IOException (java.io.IOException)16 Bindings (javax.script.Bindings)15 Map (java.util.Map)13 Invocable (javax.script.Invocable)12 FileReader (java.io.FileReader)9 HashMap (java.util.HashMap)9 ScriptContext (javax.script.ScriptContext)9 ScriptEngineFactory (javax.script.ScriptEngineFactory)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 SimpleBindings (javax.script.SimpleBindings)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7 File (java.io.File)6 InputStreamReader (java.io.InputStreamReader)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6