in src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java [31:58]
public String execute(String code, String jsonData) throws IOException {
final String jsCode = "data=" + jsonData + ";\n" + code;
final Context rhinoContext = Context.enter();
final ScriptableObject scope = rhinoContext.initStandardObjects();
// execute the script, out script variable maps to sw
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
final int lineNumber = 1;
final Object securityDomain = null;
try {
rhinoContext.evaluateString(
scope,
jsCode,
getClass().getSimpleName(),
lineNumber,
securityDomain);
} catch(Exception e) {
final IOException ioe = new IOException("While executing [" + code + "]:" + e);
ioe.initCause(e);
throw ioe;
}
// check script output
pw.flush();
return sw.toString().trim();
}