Search in sources :

Example 6 with Function

use of sun.org.mozilla.javascript.internal.Function 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 7 with Function

use of sun.org.mozilla.javascript.internal.Function in project enumerable by hraberg.

the class JavaScriptTest method convertFnToFunction.

@Test
public void convertFnToFunction() throws ScriptException {
    Function f = toFunction(Lambda.λ(s, s.toUpperCase()));
    assertEquals("HELLO", f.call(Context.getCurrentContext(), null, null, new Object[] { "hello" }));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) 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)

Example 8 with Function

use of sun.org.mozilla.javascript.internal.Function in project enumerable by hraberg.

the class JavaScriptTest method convertFnToFunctionKeepsDefaultValues.

@Test
public void convertFnToFunctionKeepsDefaultValues() throws ScriptException {
    Function f = toFunction(Lambda.λ(s = "world", s.toUpperCase()));
    assertEquals("WORLD", f.call(Context.getCurrentContext(), null, null, new Object[0]));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) 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)

Example 9 with Function

use of sun.org.mozilla.javascript.internal.Function in project enumerable by hraberg.

the class JRubyTest method interactingWithJavaScript.

@Test
public void interactingWithJavaScript() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    ScriptEngine js = JavaScriptTest.getJavaScriptEngine();
    Function f = (Function) js.eval("var f = function(n, m) { return n * m; }; f;");
    RubyProc proc = toProc(LambdaJavaScript.toFn2(f));
    assertEquals(ruby.newFloat(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120.0, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) RubyProc(org.jruby.RubyProc) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Aggregations

Function (sun.org.mozilla.javascript.internal.Function)9 Test (org.junit.Test)8 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)7 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)7 ScriptEngine (javax.script.ScriptEngine)4 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)4 IFn (clojure.lang.IFn)1 ClosureFn2 (org.enumerable.lambda.support.groovy.LambdaGroovy.ClosureFn2)1 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)1 Ruby (org.jruby.Ruby)1 RubyProc (org.jruby.RubyProc)1 IRubyObject (org.jruby.runtime.builtin.IRubyObject)1