<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
 */
package utybo.branchingstorytree.jse;

import java.util.HashMap;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import utybo.branchingstorytree.api.BSTClient;
import utybo.branchingstorytree.api.BSTException;
import utybo.branchingstorytree.api.script.ScriptAction;
import utybo.branchingstorytree.api.script.VariableRegistry;
import utybo.branchingstorytree.api.story.BranchingStory;

/**
 * Implementation of the JSE module's action
 * 
 * @author utybo
 *
 */
public class JSEAction implements ScriptAction
{

    @Override
    public void exec(final String head, final String desc, final int line, final BranchingStory story, final BSTClient client) throws BSTException
    {
        // Initialize the engine
        final ScriptEngine e = new ScriptEngineManager().getEngineByName("JavaScript");
        final VariableRegistry registry = story.getRegistry();
        final HashMap<String, Integer> ints = registry.getAllInt();
        for(final String name : ints.keySet())
        {
            try
            {
                e.eval(name + " = " + ints.get(name));
            }
            catch(final ScriptException e1)
            {
                throw new BSTException(line, "Error during JSE initialization (step INT)", e1);
            }
        }
        final HashMap<String, String> strings = registry.getAllString();
        for(final String name : strings.keySet())
        {
            try
            {
                e.eval(name + " = \"" + strings.get(name) + "\"");
            }
            catch(final ScriptException e1)
            {
                throw new BSTException(line, "Error during JSE initialization (step STRING)", e1);
            }
        }

        // Parse
        final String varName = desc.split(",")[0];
        final String script = desc.substring(desc.indexOf(',') + 1);

        // Exec
        try
        {
            final Object result = e.eval(script);
            if(result instanceof Number)
            {
                registry.put(varName, ((Number)result).intValue());
            }
            else if(result instanceof String)
            {
                registry.put(varName, (String)result);
            }
            else if(result == null)
            {
                registry.remove(varName);
            }
            else
            {
                System.err.println("[line " + line + "] Unknown return type : " + result.getClass().getName() + ". Using toString!");
                registry.put(varName, result.toString());
            }
        }
        catch(final ScriptException e1)
        {
            throw new BSTException(line, "Error during script execution : " + e1.getMessage(), e1);
        }
    }

    @Override
    public String[] getName()
    {
        return new String[] {"jse_eval"};
    }

}
<commit_msg>bst-java : Added more functions to JSE<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
 */
package utybo.branchingstorytree.jse;

import java.util.HashMap;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import utybo.branchingstorytree.api.BSTClient;
import utybo.branchingstorytree.api.BSTException;
import utybo.branchingstorytree.api.script.ScriptAction;
import utybo.branchingstorytree.api.script.VariableRegistry;
import utybo.branchingstorytree.api.story.BranchingStory;

/**
 * Implementation of the JSE module's action
 * 
 * @author utybo
 *
 */
public class JSEAction implements ScriptAction
{
    private ScriptEngine engine;

    @Override
    public void exec(final String head, final String desc, final int line, final BranchingStory story, final BSTClient client) throws BSTException
    {
        if(head.equals("jse_reset"))
        {
            engine = null;
            return;
        }

        final VariableRegistry registry = story.getRegistry();

        if(engine == null)
        {
            engine = new ScriptEngineManager().getEngineByName("JavaScript");
        }

        switch(head)
        {
        case "jse_eval":
        {
            if(!registry.get("__jse__auto", "true").toString().equalsIgnoreCase("false"))
            {
                final HashMap<String, Integer> ints = registry.getAllInt();
                for(final String name : ints.keySet())
                {
                    try
                    {
                        engine.eval(name + " = " + ints.get(name));
                    }
                    catch(final ScriptException e1)
                    {
                        throw new BSTException(line, "Error during JSE initialization (step INT)", e1);
                    }
                }
                final HashMap<String, String> strings = registry.getAllString();
                for(final String name : strings.keySet())
                {
                    try
                    {
                        engine.eval(name + " = \"" + strings.get(name) + "\"");
                    }
                    catch(final ScriptException e1)
                    {
                        throw new BSTException(line, "Error during JSE initialization (step STRING)", e1);
                    }

                }

            }

            // Parse
            final String varName = desc.split(",")[0];
            final String script = desc.substring(desc.indexOf(',') + 1);

            // Exec
            try
            {
                final Object result = engine.eval(script);
                if(result instanceof Number)
                {
                    registry.put(varName, ((Number)result).intValue());
                }
                else if(result instanceof String)
                {
                    registry.put(varName, (String)result);
                }
                else if(result == null)
                {
                    registry.remove(varName);
                }
                else
                {
                    System.err.println("[line " + line + "] Unknown return type : " + result.getClass().getName() + ". Using toString!");
                    registry.put(varName, result.toString());
                }
            }
            catch(final ScriptException e1)
            {
                throw new BSTException(line, "Error during script execution : " + e1.getMessage(), e1);
            }
            break;
        }
        case "jse_update":
        {
            try
            {
                for(String varName : desc.split(","))
                {
                    Class<?> type = registry.typeOf(varName);

                    if(type.equals(Integer.class))
                        engine.eval(varName + " = " + registry.get(varName, ""));
                    else if(type.equals(String.class))
                        engine.eval(varName + " = \"" + registry.get(varName, ""));
                    else
                        throw new BSTException(line, "Unknown variable : " + varName);

                }
            }
            catch(ScriptException e)
            {
                throw new BSTException(line, "Internal error", e);
            }
            break;
        }
        case "jse_autoimport":
            registry.put("__jse__auto", desc);
        }

    }

    @Override
    public String[] getName()
    {
        return new String[] {"jse_eval", "jse_reset", "jse_autoimport", "jse_import"};
    }

}

{'fraction_non_alphanumeric': 0.10374675404970941, 'fraction_numerical': 0.0037096574749598123, 'mean_word_length': 1.6183230819035286, 'pattern_counts': {'":': 3, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '6743761', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2056, 'n_words': 514}