this.rootScope = rootScope;
}
public Object eval(Reader scriptReader, ScriptContext scriptContext)
throws ScriptException {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
String scriptName = "NO_SCRIPT_NAME";
{
SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper != null) {
scriptName = helper.getScript().getScriptResource().getPath();
}
}
// wrap the reader in an EspReader for ESP scripts
if (scriptName.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
scriptReader = new EspReader(scriptReader);
}
// container for replaced properties
Map<String, Object> replacedProperties = null;
Scriptable scope = null;
boolean isTopLevelCall = false;
// create a rhino Context and execute the script
try {
final Context rhinoContext = Context.enter();
rhinoContext.setOptimizationLevel(optimizationLevel());
if (ScriptRuntime.hasTopCall(rhinoContext)) {
// reuse the top scope if we are included
scope = ScriptRuntime.getTopCallScope(rhinoContext);
} else {
// create the request top scope, use the ImporterToplevel here
// to support the importPackage and importClasses functions
scope = new ImporterTopLevel();
// Set the global scope to be our prototype
scope.setPrototype(rootScope);
// We want "scope" to be a new top-level scope, so set its
// parent scope to null. This means that any variables created
// by assignments will be properties of "scope".
scope.setParentScope(null);
// setup the context for use
WrapFactory wrapFactory = ((RhinoJavaScriptEngineFactory) getFactory()).getWrapFactory();
rhinoContext.setWrapFactory(wrapFactory);
// this is the top level call
isTopLevelCall = true;
}
// add initial properties to the scope
replacedProperties = setBoundProperties(scope, bindings);
final int lineNumber = 1;
final Object securityDomain = null;
Object result = rhinoContext.evaluateReader(scope, scriptReader, scriptName,
lineNumber, securityDomain);
if (result instanceof Wrapper) {
result = ((Wrapper) result).unwrap();
}
return (result instanceof Undefined) ? null : result;
} catch (JavaScriptException t) {
// prevent variables to be pushed back in case of errors
isTopLevelCall = false;
final ScriptException se = new ScriptException(t.details(),
t.sourceName(), t.lineNumber());
// log the script stack trace
((Logger) bindings.get(SlingBindings.LOG)).error(t.getScriptStackTrace());
// set the exception cause
Object value = t.getValue();
if (value != null) {
if (value instanceof Wrapper) {