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;
}
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)"));
}
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)"));
}
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"));
}
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')");
}
Aggregations