Search in sources :

Example 1 with Function

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

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

the class JavaScriptTest method convertFunctionToFn.

@Test
public void convertFunctionToFn() throws ScriptException {
    Function f = (Function) js.eval("var f = function(s) { return s.toUpperCase(); }; f;");
    assertEquals("HELLO", toFn1(f).call("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 3 with Function

use of sun.org.mozilla.javascript.internal.Function 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])"));
}
Also used : IFn(clojure.lang.IFn) Function(sun.org.mozilla.javascript.internal.Function) 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) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 4 with Function

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

the class JavaScriptTest method convertedFunctionIgnoresExtraArguments.

public void convertedFunctionIgnoresExtraArguments() throws ScriptException {
    Function f = toFunction(Lambda.λ(s, s.toUpperCase()));
    assertEquals("HELLO", f.call(Context.getCurrentContext(), null, null, new Object[] { "hello", "world" }));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function)

Example 5 with Function

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

the class JavaScriptTest method convertedFunctionThrowsExceptionWhenCalledWithTooFewArguments.

@Test(expected = NullPointerException.class)
public void convertedFunctionThrowsExceptionWhenCalledWithTooFewArguments() throws ScriptException {
    Function f = toFunction(Lambda.λ(s, s.toUpperCase()));
    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)

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