<commit_before>package com.nudemeth.example.web.engine

import javax.script.CompiledScript

import jdk.nashorn.api.scripting.{JSObject, NashornScriptEngine, NashornScriptEngineFactory}

object NashornEngine {
  /*
  Shared engine and compiled script. See link below
  https://stackoverflow.com/questions/30140103/should-i-use-a-separate-scriptengine-and-compiledscript-instances-per-each-threa
  https://blogs.oracle.com/nashorn/nashorn-multithreading-and-mt-safety
  */
  private val engine = new NashornScriptEngineFactory().getScriptEngine.asInstanceOf[NashornScriptEngine]
  private var compiledScript: Option[CompiledScript] = None
}

class NashornEngine(scripts: Seq[ScriptSource]) extends JavaScriptEngine(scripts) {
  import NashornEngine._

  private def init(): Unit = {
    NashornEngine.compiledScript match {
      case Some(_) =>
      case None => NashornEngine.compiledScript = Some(compileScript())
    }
  }

  private def compileScript(): CompiledScript = {
    val allScript = scripts.map{
      case ScriptText(ss) => ss
      case ScriptURL(ss) => scala.io.Source.fromURL(ss)("UTF-8").mkString
    }.mkString(sys.props("line.separator"))
    NashornEngine.engine.compile(allScript)
  }

  init()

  /**
    * To invoke javascript object method. Always create new bindings when calling this because of multithreading environment
    * @param objectName Object to call
    * @param methodName Method name to call
    * @param args Parameters to pass into the method
    * @tparam T Expected return type
    * @return Instance of expected T type
    */
  override def invokeMethod[T](objectName: String, methodName: String, args: Any*): T = {
    val bindings = engine.createBindings()
    compiledScript.get.eval(bindings)
    val obj = bindings.get(objectName).asInstanceOf[JSObject]
    val method = obj.getMember(methodName).asInstanceOf[JSObject]
    method.call(obj, args.map(_.asInstanceOf[AnyRef]): _*).asInstanceOf[T]
  }

}

<commit_msg>Refactor NashornEngine class<commit_after>package com.nudemeth.example.web.engine

import javax.script.CompiledScript

import jdk.nashorn.api.scripting.{JSObject, NashornScriptEngine, NashornScriptEngineFactory}

object NashornEngine {
  /*
  Shared engine and compiled script. See links below:
  https://stackoverflow.com/questions/30140103/should-i-use-a-separate-scriptengine-and-compiledscript-instances-per-each-threa
  https://blogs.oracle.com/nashorn/nashorn-multithreading-and-mt-safety
  */
  private val engine = new NashornScriptEngineFactory().getScriptEngine.asInstanceOf[NashornScriptEngine]
  private var compiledScript: Option[CompiledScript] = None
}

class NashornEngine(scripts: Seq[ScriptSource]) extends JavaScriptEngine(scripts) {
  import NashornEngine._

  private def init(): Unit = {
    NashornEngine.compiledScript match {
      case Some(_) =>
      case None => NashornEngine.compiledScript = Some(compileScript())
    }
  }

  private def appendScript(): String = {
    scripts.map{
      case ScriptText(s) => s
      case ScriptURL(s) => scala.io.Source.fromURL(s)("UTF-8").mkString
    }.mkString(sys.props("line.separator"))
  }

  private def compileScript(): CompiledScript = {
    val allScript = appendScript()
    NashornEngine.engine.compile(allScript)
  }

  init()

  /**
    * Invoking javascript object method. Always create new bindings when calling this because of multithreading environment
    * @param objectName Object to call
    * @param methodName Method name to call
    * @param args Parameters to pass into the method
    * @tparam T Expected return type
    * @return Instance of expected T type
    */
  override def invokeMethod[T](objectName: String, methodName: String, args: Any*): T = {
    val bindings = engine.createBindings()
    compiledScript.get.eval(bindings)
    val obj = bindings.get(objectName).asInstanceOf[JSObject]
    val method = obj.getMember(methodName).asInstanceOf[JSObject]
    method.call(obj, args.map(_.asInstanceOf[AnyRef]): _*).asInstanceOf[T]
  }

}


{'fraction_non_alphanumeric': 0.10551259665752058, 'fraction_numerical': 0.004489897730107259, 'mean_word_length': 4.241830065359477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 4, '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': '6889483', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1189, 'n_words': 272}