public CompiledScript compile()

in src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java [89:139]


    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.setLanguageVersion(((RhinoJavaScriptEngineFactory) getFactory()).rhinoLanguageVersion());
                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();
            }
        }
    }