use of org.mozilla.javascript.Context in project sling by apache.
the class RhinoJavaScriptEngineFactory method getRootScope.
@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
if (rootScope == null) {
final Context rhinoContext = Context.enter();
try {
rhinoContext.setOptimizationLevel(optimizationLevel);
Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
// default classes
addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
// provided classes
for (RhinoHostObjectProvider provider : hostObjectProvider) {
addHostObjects(tmpScope, provider.getHostObjectClasses());
addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
}
// only assign the root scope when complete set up
rootScope = tmpScope;
} finally {
// ensure the context is exited after setting up the
// the new root scope
Context.exit();
}
}
return rootScope;
}
use of org.mozilla.javascript.Context in project sling by apache.
the class JsUtils method callFn.
public static Object callFn(Function function, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
boolean exitContext = false;
if (Context.getCurrentContext() == null) {
Context.enter();
exitContext = true;
}
Context context = (cx == null) ? Context.getCurrentContext() : cx;
Object result = function.call(context, scope, thisObj, args);
if (exitContext) {
Context.exit();
}
return result;
}
use of org.mozilla.javascript.Context in project sling by apache.
the class JavascriptEngine method execute.
/** Execute supplied code against supplied data,
* see JavascriptEngineTest for examples */
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();
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method parseRhino.
public static <T> T parseRhino(File rhinoScript, ScopeOperation<T> operation) {
Context context = Context.enter();
try {
operation.initContext(context);
Scriptable scope = context.initStandardObjects();
String printFunction = "function print(message) {}";
context.evaluateString(scope, printFunction, "print", 1, null);
context.evaluateString(scope, readFile(rhinoScript, "UTF-8"), rhinoScript.getName(), 1, null);
return operation.action(scope, context);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method parse.
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
Context context = Context.enter();
if (contextConfig != null) {
contextConfig.execute(context);
}
Scriptable scope = context.initStandardObjects();
try {
Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
try {
context.evaluateReader(scope, reader, source.getName(), 0, null);
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
Context.exit();
}
return scope;
}
Aggregations