Search in sources :

Example 26 with ScriptEngine

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])"));
}
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 27 with ScriptEngine

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"));
}
Also used : RubyProc(org.jruby.RubyProc) Closure(groovy.lang.Closure) 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)

Example 28 with ScriptEngine

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);
        }
    });
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) FileReader(java.io.FileReader) File(java.io.File) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 29 with ScriptEngine

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[]
}
Also used : Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 30 with ScriptEngine

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);
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7