<commit_before>package solace.script;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.nio.*;
import java.nio.file.*;
import java.util.*;
import java.io.*;
import solace.util.Log;

/**
 * The main scripting engine for Solace. Allows for the creation of JavaScript
 * scripts that can modify the game in a plethora of ways. This includes, but
 * is not limited to: mobile behaviors, cooldown actions, custom commands,
 * buffs, etc.
 * @author <NAME>
 */
public class Engine {
  private ScriptEngineManager engineManager;
  private ScriptEngine engine;

  /**
   * Path to the engine globals initialization script.
   */
  protected static final String GLOBALS_JS = "script/engine/globals.js";

  /**
   * Singleton instance of the scripting engine.
   */
  private static final Engine instance = new Engine();

  /**
   * Creates a new solace scripting engine.
   */
  Engine() {
    engineManager = new ScriptEngineManager();
    engine = engineManager.getEngineByName("nashorn");
  }

  /**
   * @return The singelton instance of the scripting engine.
   */
  public static Engine getInstance() {
    return instance;
  }

  /**
   * Starts the scripting engine.
   * @throws IOException If a required startup script fails to load.
   * @throws ScriptException If a requires startup script fails to evaluate.
   */
  public static void start() throws IOException, ScriptException {
    Log.info("Starting scripting engine...");
    instance.run(GLOBALS_JS);
  }

  /**
   * Runs the javascript file with the given path on the scripting engine.
   * @param path Path of the javascript file to run.
   * @throws IOException If an io error occurs when reading the file.
   * @throws ScriptException If an error occurs executing the script.
   */
  protected void run(String path) throws IOException, ScriptException {
    String script = new String(Files.readAllBytes(Paths.get(path)));
    engine.eval(script);
  }
}<commit_msg>Added a slew of helper functions to the scripting engine.<commit_after>package solace.script;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.nio.*;
import java.nio.file.*;
import java.util.*;
import java.io.*;
import solace.util.Log;

/**
 * The main scripting engine for Solace. Allows for the creation of JavaScript
 * scripts that can modify the game in a plethora of ways. This includes, but
 * is not limited to: mobile behaviors, cooldown actions, custom commands,
 * buffs, etc.
 * @author <NAME>
 */
public class Engine {
  private ScriptEngineManager engineManager;
  private ScriptEngine engine;

  /**
   * Path to the engine globals initialization script.
   */
  protected static final String GLOBALS_JS = "script/engine/globals.js";

  /**
   * Singleton instance of the scripting engine.
   */
  private static final Engine instance = new Engine();

  /**
   * Creates a new solace scripting engine.
   */
  Engine() {
    engineManager = new ScriptEngineManager();
    engine = engineManager.getEngineByName("nashorn");
  }

  /**
   * @return The singelton instance of the scripting engine.
   */
  public static Engine getInstance() {
    return instance;
  }

  /**
   * Starts the scripting engine.
   * @throws IOException If a required startup script fails to load.
   * @throws ScriptException If a requires startup script fails to evaluate.
   */
  public static void start() throws IOException, ScriptException {
    Log.info("Starting scripting engine...");
    instance.runGlobal(GLOBALS_JS);
    instance.run("script/random.js");
  }

  /**
   * Safe script evaluation that prevents global scope pollution.
   * @param script The script to evaluate.
   * @throws ScriptException If an error occurs executing the script.
   */
  public void eval(String script) throws ScriptException {
    engine.eval(
      "'use strict';\n" +
      "(function() {\n" +
      script.replaceAll("[\"']use\\s+strict[\"'][;]?", "") +
      "})();\n");
  }
  /**
   * Loads the string contents for the script with the given path.
   * @param path Path of the script to load.
   * @return The string contents of the script.
   * @throws IOException If an io error occurs when reading the file.
   * @throws ScriptException If an error occurs executing the script.
   */
  private String loadScript(String path)
    throws IOException, ScriptException
  {
    return new String(Files.readAllBytes(Paths.get(path)));
  }

  /**
   * Runs the javascript file with the given path on the scripting engine.
   * @param path Path of the javascript file to run.
   * @throws IOException If an io error occurs when reading the file.
   * @throws ScriptException If an error occurs executing the script.
   */
  public void run(String path) throws IOException, ScriptException {
    eval(loadScript(path));
  }

  /**
   * Runs the javascript file with the given path on the scripting engine in
   * the global scope.
   * @param path Path of the javascript file to run.
   * @throws IOException If an io error occurs when reading the file.
   * @throws ScriptException If an error occurs executing the script.
   */
  protected void runGlobal(String path) throws IOException, ScriptException {
    engine.eval(loadScript(path));
  }
}
{'fraction_non_alphanumeric': 0.09469126506024096, 'fraction_numerical': 0.0, 'mean_word_length': 3.4460251046025103, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4480101', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1414, 'n_words': 565}