<commit_before>/**
 * Copyright (c) 2012-2014 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft;

import static com.github.anba.es6draft.runtime.ExecutionContext.newScriptExecutionContext;

import java.util.EnumSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.github.anba.es6draft.ast.FunctionDefinition;
import com.github.anba.es6draft.ast.GeneratorDefinition;
import com.github.anba.es6draft.compiler.CompilationException;
import com.github.anba.es6draft.compiler.CompiledScript;
import com.github.anba.es6draft.compiler.Compiler;
import com.github.anba.es6draft.interpreter.InterpretedScript;
import com.github.anba.es6draft.interpreter.Interpreter;
import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.LexicalEnvironment;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.RuntimeInfo;

/**
 * <h1>15 ECMAScript Language: Modules and Scripts</h1>
 * <ul>
 * <li>15.2 Script
 * </ul>
 */
public final class ScriptLoader {
    private ScriptLoader() {
    }

    /**
     * [15.2.7 Runtime Semantics: Script Evaluation]
     */
    public static Object ScriptEvaluation(Script script, Realm realm, boolean deletableBindings) {
        /* steps 1-2 */
        RuntimeInfo.ScriptBody scriptBody = script.getScriptBody();
        if (scriptBody == null)
            return null;
        /* step 3 */
        LexicalEnvironment globalEnv = realm.getGlobalEnv();
        /* steps 4-5 */
        scriptBody.globalDeclarationInstantiation(realm.defaultContext(), globalEnv, globalEnv,
                deletableBindings);
        /* steps 6-9 */
        ExecutionContext progCxt = newScriptExecutionContext(realm, script);
        ExecutionContext oldScriptContext = realm.getScriptContext();
        try {
            realm.setScriptContext(progCxt);
            /* steps 10-14 */
            Object result = script.evaluate(progCxt);
            /* step 15 */
            return result;
        } finally {
            realm.setScriptContext(oldScriptContext);
        }
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(com.github.anba.es6draft.ast.Script parsedScript, String className,
            EnumSet<Compiler.Option> options) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(parsedScript, className, options);
        }
        return script;
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(com.github.anba.es6draft.ast.Script parsedScript, String className,
            ExecutorService executor, EnumSet<Compiler.Option> options) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(parsedScript, className, executor, options);
        }
        return script;
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(com.github.anba.es6draft.ast.Script parsedScript,
            String className, EnumSet<Compiler.Option> options) throws CompilationException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        try {
            Compiler compiler = new Compiler(executor, options);
            return compiler.compile(parsedScript, className);
        } finally {
            executor.shutdown();
        }
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(com.github.anba.es6draft.ast.Script parsedScript,
            String className, ExecutorService executor, EnumSet<Compiler.Option> options)
            throws CompilationException {
        Compiler compiler = new Compiler(executor, options);
        return compiler.compile(parsedScript, className);
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(Realm realm, com.github.anba.es6draft.ast.Script parsedScript)
            throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            String className = realm.nextEvalName();
            script = compile(realm, parsedScript, className);
        }
        return script;
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(Realm realm, com.github.anba.es6draft.ast.Script parsedScript,
            String className) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(realm, parsedScript, className);
        }
        return script;
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(Realm realm,
            com.github.anba.es6draft.ast.Script parsedScript, String className)
            throws CompilationException {
        Compiler compiler = new Compiler(realm.getExecutor(), realm.getCompilerOptions());
        return compiler.compile(parsedScript, className);
    }

    /**
     * Compiles the given {@link FunctionDefinition} to a
     * {@link com.github.anba.es6draft.runtime.internal.RuntimeInfo.Function} object
     */
    public static RuntimeInfo.Function compile(Realm realm, FunctionDefinition function)
            throws CompilationException {
        String className = realm.nextFunctionName();
        Compiler compiler = new Compiler(realm.getExecutor(), realm.getCompilerOptions());
        return compiler.compile(function, className).getFunction();
    }

    /**
     * Compiles the given {@link GeneratorDefinition} to a
     * {@link com.github.anba.es6draft.runtime.internal.RuntimeInfo.Function} object
     */
    public static RuntimeInfo.Function compile(Realm realm, GeneratorDefinition generator)
            throws CompilationException {
        String className = realm.nextFunctionName();
        Compiler compiler = new Compiler(realm.getExecutor(), realm.getCompilerOptions());
        return compiler.compile(generator, className).getFunction();
    }
}<commit_msg>Shared runtime executor may have been shutdown already, create a temporary executor service in that case<commit_after>/**
 * Copyright (c) 2012-2014 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft;

import static com.github.anba.es6draft.runtime.ExecutionContext.newScriptExecutionContext;

import java.util.EnumSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.github.anba.es6draft.ast.FunctionDefinition;
import com.github.anba.es6draft.ast.GeneratorDefinition;
import com.github.anba.es6draft.compiler.CompilationException;
import com.github.anba.es6draft.compiler.CompiledFunction;
import com.github.anba.es6draft.compiler.CompiledScript;
import com.github.anba.es6draft.compiler.Compiler;
import com.github.anba.es6draft.interpreter.InterpretedScript;
import com.github.anba.es6draft.interpreter.Interpreter;
import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.LexicalEnvironment;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.RuntimeInfo;

/**
 * <h1>15 ECMAScript Language: Modules and Scripts</h1>
 * <ul>
 * <li>15.2 Script
 * </ul>
 */
public final class ScriptLoader {
    private ScriptLoader() {
    }

    /**
     * [15.2.7 Runtime Semantics: Script Evaluation]
     */
    public static Object ScriptEvaluation(Script script, Realm realm, boolean deletableBindings) {
        /* steps 1-2 */
        RuntimeInfo.ScriptBody scriptBody = script.getScriptBody();
        if (scriptBody == null)
            return null;
        /* step 3 */
        LexicalEnvironment globalEnv = realm.getGlobalEnv();
        /* steps 4-5 */
        scriptBody.globalDeclarationInstantiation(realm.defaultContext(), globalEnv, globalEnv,
                deletableBindings);
        /* steps 6-9 */
        ExecutionContext progCxt = newScriptExecutionContext(realm, script);
        ExecutionContext oldScriptContext = realm.getScriptContext();
        try {
            realm.setScriptContext(progCxt);
            /* steps 10-14 */
            Object result = script.evaluate(progCxt);
            /* step 15 */
            return result;
        } finally {
            realm.setScriptContext(oldScriptContext);
        }
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(com.github.anba.es6draft.ast.Script parsedScript, String className,
            EnumSet<Compiler.Option> options) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(parsedScript, className, options);
        }
        return script;
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(com.github.anba.es6draft.ast.Script parsedScript, String className,
            ExecutorService executor, EnumSet<Compiler.Option> options) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(parsedScript, className, executor, options);
        }
        return script;
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(com.github.anba.es6draft.ast.Script parsedScript,
            String className, EnumSet<Compiler.Option> options) throws CompilationException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        try {
            Compiler compiler = new Compiler(executor, options);
            return compiler.compile(parsedScript, className);
        } finally {
            executor.shutdown();
        }
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(com.github.anba.es6draft.ast.Script parsedScript,
            String className, ExecutorService executor, EnumSet<Compiler.Option> options)
            throws CompilationException {
        return tryCompile(parsedScript, className, executor, options);
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(Realm realm, com.github.anba.es6draft.ast.Script parsedScript)
            throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            String className = realm.nextEvalName();
            script = compile(realm, parsedScript, className);
        }
        return script;
    }

    /**
     * Returns an executable {@link Script} object for given
     * {@link com.github.anba.es6draft.ast.Script} AST-node. This may either be an
     * {@link InterpretedScript} or {@link CompiledScript} instance.
     */
    public static Script load(Realm realm, com.github.anba.es6draft.ast.Script parsedScript,
            String className) throws CompilationException {
        Script script = Interpreter.script(parsedScript);
        if (script == null) {
            script = compile(realm, parsedScript, className);
        }
        return script;
    }

    /**
     * Compiles the given {@link com.github.anba.es6draft.ast.Script} to an executable
     * {@link Script} object
     */
    public static CompiledScript compile(Realm realm,
            com.github.anba.es6draft.ast.Script parsedScript, String className)
            throws CompilationException {
        ExecutorService executor = realm.getExecutor();
        return tryCompile(parsedScript, className, executor, realm.getCompilerOptions());
    }

    /**
     * Compiles the given {@link FunctionDefinition} to a
     * {@link com.github.anba.es6draft.runtime.internal.RuntimeInfo.Function} object
     */
    public static RuntimeInfo.Function compile(Realm realm, FunctionDefinition function)
            throws CompilationException {
        ExecutorService executor = realm.getExecutor();
        String className = realm.nextFunctionName();
        return tryCompile(function, className, executor, realm.getCompilerOptions()).getFunction();
    }

    /**
     * Compiles the given {@link GeneratorDefinition} to a
     * {@link com.github.anba.es6draft.runtime.internal.RuntimeInfo.Function} object
     */
    public static RuntimeInfo.Function compile(Realm realm, GeneratorDefinition generator)
            throws CompilationException {
        ExecutorService executor = realm.getExecutor();
        String className = realm.nextFunctionName();
        return tryCompile(generator, className, executor, realm.getCompilerOptions()).getFunction();
    }

    /**
     * Try to compile the script with the given executor, unless it has been shutdown, in that case
     * create a new executor for compilation
     */
    private static CompiledScript tryCompile(com.github.anba.es6draft.ast.Script parsedScript,
            String className, ExecutorService executor, EnumSet<Compiler.Option> options) {
        if (executor.isShutdown()) {
            return compile(parsedScript, className, options);
        }
        Compiler compiler = new Compiler(executor, options);
        return compiler.compile(parsedScript, className);
    }

    /**
     * Try to compile the function with the given executor, unless it has been shutdown, in that
     * case create a new executor for compilation
     */
    private static CompiledFunction tryCompile(FunctionDefinition function, String className,
            ExecutorService executor, EnumSet<Compiler.Option> options) {
        if (executor.isShutdown()) {
            return compile(function, className, options);
        }
        Compiler compiler = new Compiler(executor, options);
        return compiler.compile(function, className);
    }

    private static CompiledFunction compile(FunctionDefinition function, String className,
            EnumSet<Compiler.Option> options) throws CompilationException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        try {
            Compiler compiler = new Compiler(executor, options);
            return compiler.compile(function, className);
        } finally {
            executor.shutdown();
        }
    }

    /**
     * Try to compile the generator with the given executor, unless it has been shutdown, in that
     * case create a new executor for compilation
     */
    private static CompiledFunction tryCompile(GeneratorDefinition generator, String className,
            ExecutorService executor, EnumSet<Compiler.Option> options) {
        if (executor.isShutdown()) {
            return compile(generator, className, options);
        }
        Compiler compiler = new Compiler(executor, options);
        return compiler.compile(generator, className);
    }

    private static CompiledFunction compile(GeneratorDefinition generator, String className,
            EnumSet<Compiler.Option> options) throws CompilationException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        try {
            Compiler compiler = new Compiler(executor, options);
            return compiler.compile(generator, className);
        } finally {
            executor.shutdown();
        }
    }
}
{'fraction_non_alphanumeric': 0.09612013556152857, 'fraction_numerical': 0.007596120135561529, 'mean_word_length': 2.889772727272727, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '674341', 'n_tokens_mistral': 4594, 'n_tokens_neox': 4494, 'n_words': 1140}