use of org.mozilla.javascript.WrapFactory in project sling by apache.
the class RhinoJavaScriptEngine method compile.
public CompiledScript compile(Reader scriptReader) throws ScriptException {
final String scriptName = getScriptName(scriptReader);
CachedScript cachedScript = scriptCache.getScript(scriptName);
if (cachedScript != null) {
LOGGER.debug("Detected cached script for {}.", scriptName);
return cachedScript.getCompiledScript();
} else {
scriptReader = wrapReaderIfEspScript(scriptReader, scriptName);
try {
final Context rhinoContext = Context.enter();
rhinoContext.setOptimizationLevel(optimizationLevel());
if (!ScriptRuntime.hasTopCall(rhinoContext)) {
// setup the context for use
WrapFactory wrapFactory = ((RhinoJavaScriptEngineFactory) getFactory()).getWrapFactory();
rhinoContext.setWrapFactory(wrapFactory);
}
final int lineNumber = 1;
final Object securityDomain = null;
final Script script = rhinoContext.compileReader(scriptReader, scriptName, lineNumber, securityDomain);
final SlingCompiledScript slingCompiledScript = new SlingCompiledScript(script, this);
cachedScript = new CachedScript() {
@Override
public String getScriptPath() {
return scriptName;
}
@Override
public CompiledScript getCompiledScript() {
return slingCompiledScript;
}
};
// SLING-4935 avoid caching scripts for which we cannot determine a name
if (!scriptName.equals(NO_SCRIPT_NAME)) {
scriptCache.putScript(cachedScript);
}
LOGGER.debug("Added {} script to Script Cache.", scriptName);
return slingCompiledScript;
} catch (IOException e) {
final ScriptException se = new ScriptException("Failure running script " + scriptName + ": " + e.getMessage());
se.initCause(e);
throw se;
} finally {
Context.exit();
}
}
}
Aggregations