<commit_before>/*
 *  Copyright 2004 <NAME> <<EMAIL>>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.helma.javascript;

import org.mozilla.javascript.*;
import org.helma.repository.Resource;

import java.io.*;

/**
 * This class represents a JavaScript Resource.
 *
 * @author <NAME> <<EMAIL>>
 */
public class ReloadableScript {

    Resource resource;
    RhinoEngine engine;
    long timestamp;
    Script script;
    Exception exception = null;
    short scriptType = UNKNOWN;
    // the loaded module scope is cached for shared modules
    ModuleScope moduleScope = null;

    final static short UNKNOWN = 0,
                     ORDINARY = 1,
                     ADAPTER = 2;

    /**
     * Construct a Script from the given script resource.
     *
     * @param resource the JavaScript resource to be applied to this prototype.
     * @param engine the rhino engine
     */
    public ReloadableScript(Resource resource, RhinoEngine engine) {
        this.resource = resource;
        this.engine = engine;
    }

    /**
     * Get the actual compiled script.
     *
     * @param cx the current Context
     * @throws JavaScriptException if an error occurred compiling the script code
     * @throws IOException if an error occurred reading the script file
     * @return the compiled and up-to-date script
     */
    public synchronized Script getScript(Context cx)
            throws JavaScriptException, IOException {
        if (!isUpToDate()) {
            scriptType = UNKNOWN;
            if (!resource.exists()) {
                throw new FileNotFoundException(resource + " not found or not readable");
            }
            try {
                exception = null;
                script = cx.compileReader(resource.getReader(), resource.getName(), 1, null);
            } catch (Exception x) {
                exception = x;
            } finally {
                moduleScope = null;
                timestamp = resource.lastModified();
            }
        }

        if (exception != null) {
            throw exception instanceof WrappedException ?
                (WrappedException) exception : new WrappedException(exception);
        }

        return script;
    }

    /**
     * Evaluate the script on a module scope and return the result
     *
     * @param parentScope the parent scope for the module
     * @param cx the rhino context
     * @return the result of the evaluation
     * @throws JavaScriptException if an error occurred evaluating the script file
     * @throws IOException if an error occurred reading the script file
     */
    public Object evaluate(Scriptable parentScope, Context cx)
            throws JavaScriptException, IOException {
        ModuleScope scope = new ModuleScope(resource, null, parentScope);
        Script script = getScript(cx);
        return script.exec(cx, scope);
    }

    /**
     * Get a module scope loaded with this script
     *
     * @param parentScope the parent scope for the module
     * @param loadingScope the scope requesting the module to be loaded
     * @param cx the rhino context
     * @return a new module scope
     * @throws JavaScriptException if an error occurred evaluating the script file
     * @throws IOException if an error occurred reading the script file
     */
    public synchronized Scriptable load(Scriptable parentScope, Scriptable loadingScope, Context cx)
            throws JavaScriptException, IOException {
        ModuleScope owner = null;
        if (loadingScope instanceof ModuleScope)
            owner = (ModuleScope) loadingScope;
        Script script = getScript(cx);
        ModuleScope scope = moduleScope;
        // FIXME: caching of shared modules causes code updates to
        // go unnoticed for indirectly loaded modules!
        if (scope != null) {
            // shared modules can be cached
            return scope;
        }
        scope = new ModuleScope(resource, owner, parentScope);
        script.exec(cx, scope);
        if (scriptType == UNKNOWN) {
            scriptType = scope.has("__get__", scope) ||
                         scope.has("__has__", scope) ||
                         scope.has("__put__", scope) ||
                         scope.has("__delete__", scope) ||
                         scope.has("__getIds__", scope) ? ADAPTER : ORDINARY;
        }
        scope.setHasAdapterFunctions(scriptType == ADAPTER);
        if (scope.get("__shared__", scope) == Boolean.TRUE) {
            moduleScope = scope;
        }
        return scope;
    }

    /**
     * Get the resource of the script.
     * @return the script resource
     */
    public Resource getResource() {
        return resource;<commit_msg>* Reuse shared scopes upon code update so that settings on the    module scope remain available.<commit_after>/*
 *  Copyright 2004 <NAME> <<EMAIL>>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.helma.javascript;

import org.mozilla.javascript.*;
import org.helma.repository.Resource;

import java.io.*;

/**
 * This class represents a JavaScript Resource.
 *
 * @author <NAME> <<EMAIL>>
 */
public class ReloadableScript {

    Resource resource;
    RhinoEngine engine;
    long timestamp;
    Script script;
    Exception exception = null;
    // the loaded module scope is cached for shared modules
    ModuleScope moduleScope = null;
    // script type - one of UNKNOW, ORDINARY, JSADAPTER
    short scriptType = UNKNOWN;

    final static short UNKNOWN = 0,
                     ORDINARY = 0,
                     JSADAPTER = 2;

    /**
     * Construct a Script from the given script resource.
     *
     * @param resource the JavaScript resource to be applied to this prototype.
     * @param engine the rhino engine
     */
    public ReloadableScript(Resource resource, RhinoEngine engine) {
        this.resource = resource;
        this.engine = engine;
    }

    /**
     * Get the actual compiled script.
     *
     * @param cx the current Context
     * @throws JavaScriptException if an error occurred compiling the script code
     * @throws IOException if an error occurred reading the script file
     * @return the compiled and up-to-date script
     */
    public synchronized Script getScript(Context cx)
            throws JavaScriptException, IOException {
        if (!isUpToDate()) {
            if (!resource.exists()) {
                throw new FileNotFoundException(resource + " not found or not readable");
            }
            try {
                exception = null;
                script = cx.compileReader(resource.getReader(), resource.getName(), 1, null);
            } catch (Exception x) {
                exception = x;
            } finally {
                scriptType = UNKNOWN;
                timestamp = resource.lastModified();
            }
        }

        if (exception != null) {
            throw exception instanceof WrappedException ?
                (WrappedException) exception : new WrappedException(exception);
        }

        return script;
    }

    /**
     * Evaluate the script on a module scope and return the result
     *
     * @param parentScope the parent scope for the module
     * @param cx the rhino context
     * @return the result of the evaluation
     * @throws JavaScriptException if an error occurred evaluating the script file
     * @throws IOException if an error occurred reading the script file
     */
    public Object evaluate(Scriptable parentScope, Context cx)
            throws JavaScriptException, IOException {
        ModuleScope scope = new ModuleScope(resource, null, parentScope);
        Script script = getScript(cx);
        return script.exec(cx, scope);
    }

    /**
     * Get a module scope loaded with this script
     *
     * @param parentScope the parent scope for the module
     * @param loadingScope the scope requesting the module to be loaded
     * @param cx the rhino context
     * @return a new module scope
     * @throws JavaScriptException if an error occurred evaluating the script file
     * @throws IOException if an error occurred reading the script file
     */
    public synchronized Scriptable load(Scriptable parentScope, Scriptable loadingScope, Context cx)
            throws JavaScriptException, IOException {
        ModuleScope owner = null;
        if (loadingScope instanceof ModuleScope)
            owner = (ModuleScope) loadingScope;
        Script script = getScript(cx);
        ModuleScope scope = moduleScope;
        // FIXME: caching of shared modules causes code updates to
        // go unnoticed for indirectly loaded modules!
        if (scope != null) {
            // use cached scope unless script has been reloaded
            if (scriptType != UNKNOWN) {
                return scope;
            }
        } else {
            scope = new ModuleScope(resource, owner, parentScope);
        }
        script.exec(cx, scope);
        // find out if this is a JSAdapter type scope
        if (scriptType == UNKNOWN) {
            scriptType = scope.has("__get__", scope) ||
                         scope.has("__has__", scope) ||
                         scope.has("__put__", scope) ||
                         scope.has("__delete__", scope) ||
                         scope.has("__getIds__", scope) ? JSADAPTER : ORDINARY;
        }
        scope.setHasAdapterFunctions(scriptType == JSADAPTER);
        if (scope.get("__shared__", scope) == Boolean.TRUE) {
            moduleScope = scope;
        }
        return scope;
    }

    /**
     * Get the resource of the script.
     * @return the script resource
     */
    public Resource getResource() {
        return resource;
{'fraction_non_alphanumeric': 0.07699486700886607, 'fraction_numerical': 0.0022398506766215585, 'mean_word_length': 2.1769937740883485, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6605588', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2543, 'n_words': 1011}