Search in sources :

Example 36 with ScriptEngine

use of javax.script.ScriptEngine in project zaproxy by zaproxy.

the class ExtensionScript method invokeScriptImpl.

/**
	 * Invokes the given {@code script}, handling any {@code Exception} thrown during the invocation.
	 * <p>
	 * Script's (or default) {@code Writer} is set to the {@code ScriptContext} of the {@code ScriptEngine} before the
	 * invocation.
	 *
	 * @param script the script that will be invoked
	 * @return an {@code Invocable} for the {@code script}, or {@code null} if none.
	 * @see #getWriters(ScriptWrapper)
	 * @see Invocable
	 */
private Invocable invokeScriptImpl(ScriptWrapper script) {
    ScriptEngine se = script.getEngine().getEngine();
    Writer writer = getWriters(script);
    se.getContext().setWriter(writer);
    // Set the script name as a context attribute - this is used for script level variables 
    se.getContext().setAttribute(SCRIPT_NAME_ATT, script.getName(), ScriptContext.ENGINE_SCOPE);
    try {
        se.eval(script.getContents());
    } catch (Exception e) {
        handleScriptException(script, writer, e);
    }
    if (se instanceof Invocable) {
        return (Invocable) se;
    }
    return null;
}
Also used : Invocable(javax.script.Invocable) ScriptEngine(javax.script.ScriptEngine) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) MalformedInputException(java.nio.charset.MalformedInputException) InvalidParameterException(java.security.InvalidParameterException) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 37 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 38 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 39 with ScriptEngine

use of javax.script.ScriptEngine in project java8-tutorial by winterbe.

the class Nashorn2 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval(new FileReader("res/nashorn2.js"));
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) FileReader(java.io.FileReader) ScriptEngine(javax.script.ScriptEngine)

Example 40 with ScriptEngine

use of javax.script.ScriptEngine in project java8-tutorial by winterbe.

the class Nashorn3 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn3.js')");
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) 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