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);
}
// create a rhino Context and execute the script
try {
final Context rhinoContext = Context.enter();
final ScriptableObject scope = new NativeObject();
// 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
rhinoContext.setWrapFactory(SlingWrapFactory.INSTANCE);
// add initial properties to the scope
for (Object entryObject : bindings.entrySet()) {
Entry<?, ?> entry = (Entry<?, ?>) entryObject;
Object wrapped = ScriptRuntime.toObject(scope, entry.getValue());
ScriptableObject.putProperty(scope, (String) entry.getKey(),
wrapped);
}
final int lineNumber = 1;
final Object securityDomain = null;
return rhinoContext.evaluateReader(scope, scriptReader, scriptName,
lineNumber, securityDomain);
} catch (JavaScriptException t) {
final ScriptException se = new ScriptException(t.details(),
t.sourceName(), t.lineNumber());
((Logger) bindings.get(SlingBindings.LOG)).error(t.getScriptStackTrace());
se.setStackTrace(t.getStackTrace());
throw se;
} catch (Throwable t) {