<commit_before>package info.thewordnerd.spiel.scripting

import android.util.Log
import android.view.accessibility.AccessibilityEvent

import org.mozilla.javascript.Context
import org.mozilla.javascript.ScriptableObject

import info.thewordnerd.spiel.utils.TTS

trait AccessibilityEventHandler {
  def run(e:AccessibilityEvent):Boolean
}

object Scripter {

  val js = """
    var pkg = null;

    function forPackage(pk) {
      pkg = pk;
    }

    function forClass(cls, script) {
      if(cls[0] == ".")
        cls = pkg+cls;
      key = new java.util.ArrayList();
      key.add(pkg);
      key.add(cls);
      h = new info.thewordnerd.spiel.scripting.AccessibilityEventHandler(script);
      for(e in script) {
        if(e == "onViewClicked")
          info.thewordnerd.spiel.presenters.ViewClicked.handlers.put(k, h);
        else if(e == "onViewFocused")
          info.thewordnerd.spiel.presenters.ViewFocused.handlers.put(k, h);
        else if(e == "onViewSelected")
          info.thewordnerd.spiel.presenters.ViewSelected.handlers.put(k, h);
        else if(e == "onViewTextChanged")
          info.thewordnerd.spiel.presenters.ViewTextChanged.handlers.put(k, h);
        else if(e == "onWindowStateChanged")
          info.thewordnerd.spiel.presenters.WindowStateChanged.handlers.put(k, h);
        else
          print("Invalid event: ",e);
      }
    }
  """

  def apply() = {
    val cx = Context.enter
    cx.setOptimizationLevel(-1)
    val  scope = cx.initStandardObjects()
    val wrappedTTS = Context.javaToJS(TTS, scope)
    ScriptableObject.putProperty(scope, "tts", wrappedTTS)
    val result = cx.evaluateString(scope, """print("Hello, world.");""", "<spiel>", 1, null)
    Log.d(this.toString, Context.toString(result))
    Context.exit
    true
  }
}<commit_msg>Actually evaluate the JS API rather than a simple test string.<commit_after>package info.thewordnerd.spiel.scripting

import android.util.Log
import android.view.accessibility.AccessibilityEvent

import org.mozilla.javascript.Context
import org.mozilla.javascript.ScriptableObject

import info.thewordnerd.spiel.utils.TTS

trait AccessibilityEventHandler {
  def run(e:AccessibilityEvent):Boolean
}

object Scripter {

  val js = """
    var pkg = null;

    function forPackage(pk) {
      pkg = pk;
    }

    function forClass(cls, script) {
      if(cls[0] == ".")
        cls = pkg+cls;
      key = new java.util.ArrayList();
      key.add(pkg);
      key.add(cls);
      h = new info.thewordnerd.spiel.scripting.AccessibilityEventHandler(script);
      for(e in script) {
        if(e == "onViewClicked")
          info.thewordnerd.spiel.presenters.ViewClicked.handlers.put(k, h);
        else if(e == "onViewFocused")
          info.thewordnerd.spiel.presenters.ViewFocused.handlers.put(k, h);
        else if(e == "onViewSelected")
          info.thewordnerd.spiel.presenters.ViewSelected.handlers.put(k, h);
        else if(e == "onViewTextChanged")
          info.thewordnerd.spiel.presenters.ViewTextChanged.handlers.put(k, h);
        else if(e == "onWindowStateChanged")
          info.thewordnerd.spiel.presenters.WindowStateChanged.handlers.put(k, h);
        else
          print("Invalid event: ",e);
      }
    }
  """

  def apply() = {
    val cx = Context.enter
    cx.setOptimizationLevel(-1)
    val  scope = cx.initStandardObjects()
    val wrappedTTS = Context.javaToJS(TTS, scope)
    ScriptableObject.putProperty(scope, "tts", wrappedTTS)
    val result = cx.evaluateString(scope, js, "<spiel>", 1, null)
    Context.exit
    true
  }
}
{'fraction_non_alphanumeric': 0.12651110486364914, 'fraction_numerical': 0.0016868147315153219, 'mean_word_length': 3.0385925085130534, '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': '6267311', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1164, 'n_words': 207}