<commit_before>/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2020 Fujion Framework
 * %%
 * 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.
 *
 * #L%
 */
package org.fujion.script;

import org.apache.commons.lang3.StringUtils;
import org.fujion.common.Assert;
import org.fujion.common.MiscUtil;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Map;

/**
 * Base class for JSR-223 compliant scripts.
 */
public abstract class BaseScript implements IScriptLanguage {

    /**
     * Wrapper for a script.
     */
    public class ParsedScript implements IParsedScript {

        private final String source;

        public ParsedScript(String source) {
            this.source = StringUtils.trimToEmpty(source);
        }

        @Override
        public Object run(Map<String, Object> variables) {
            ScriptEngine engine = SCRIPT_ENGINE_MANAGER.getEngineByName(engineName);
            Assert.state(engine != null, () -> displayName + " scripting engine was not found.");

            if (variables != null) {
                for (Map.Entry<String, Object> entry : variables.entrySet()) {
                    engine.put(entry.getKey(), entry.getValue());
                }
            }

            try {
                return engine.eval(source);
            } catch (ScriptException e) {
                throw MiscUtil.toUnchecked(e);
            }
        }

    }<commit_msg>Support compilable scripts.<commit_after>/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2020 Fujion Framework
 * %%
 * 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.
 *
 * #L%
 */
package org.fujion.script;

import org.apache.commons.lang3.StringUtils;
import org.fujion.common.Assert;
import org.fujion.common.MiscUtil;

import javax.script.*;
import java.util.Map;

/**
 * Base class for JSR-223 compliant scripts.
 */
public abstract class BaseScript implements IScriptLanguage {

    /**
     * Wrapper for a script.
     */
    public class ParsedScript implements IParsedScript {

        private final String source;

        private final CompiledScript compiledScript;

        private final ScriptEngine engine;

        public ParsedScript(String source) {
            this.source = StringUtils.trimToEmpty(source);
            this.engine = SCRIPT_ENGINE_MANAGER.getEngineByName(engineName);
            Assert.state(engine != null, "%s scripting engine was not found.", displayName);

            try {
                compiledScript = engine instanceof Compilable ? ((Compilable) engine).compile(source) : null;
            } catch (ScriptException e) {
                throw MiscUtil.toUnchecked(e);
            }

        }

        @Override
        public Object run(Map<String, Object> variables) {
            Bindings bindings = null;

            if (variables != null) {
                bindings = engine.createBindings();
                bindings.putAll(variables);
            }

            try {
                if (compiledScript != null) {
                    return bindings == null ? compiledScript.eval() : compiledScript.eval(bindings);
                } else {
                    return bindings == null ? engine.eval(source) : engine.eval(source, bindings);
                }
            } catch (ScriptException e) {
                throw MiscUtil.toUnchecked(e);
            }
        }

    }
{'fraction_non_alphanumeric': 0.0921261629226231, 'fraction_numerical': 0.005445881552076242, 'mean_word_length': 2.3597560975609757, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '3832423', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1124, 'n_words': 367}