<commit_before>defmodule ElixirScript.State do
  @moduledoc false

  # Holds the state for the ElixirScript compiler

  def start_link() do
    Agent.start_link(fn ->
      %{
        modules: Keyword.new,
        js_modules: [],
        in_memory_modules: []
      }
    end)
  end

  def stop(pid) do
    Agent.stop(pid)
  end

  def get_module(pid, module) do
    Agent.get(pid, fn(state) ->
      Keyword.get(state.modules, module)
    end)
  end

  def put_module(pid, module, value) do
    Agent.update(pid, fn(state) ->
      value = Map.put(value, :used, [])
      modules = Keyword.put(state.modules, module, value)
      %{ state | modules: modules }
    end)
  end

  def add_used_module(pid, module, used_module) do
    Agent.update(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)

      used_module = Map.get(module_info, :used_module, [])
      used_module = Enum.uniq([used_module | used_module])

      module_info = Map.put(module_info, :used_module, used_module)
      modules = Keyword.put(state.modules, module, module_info)

      %{ state | modules: modules }
    end)
  end


  def has_used?(pid, module, func) do
    Agent.get(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)
      Enum.find(module_info.used, fn(x) -> x == func end) != nil
    end)
  end

  def add_used(pid, module, {_function, _arity} = func) do
    Agent.update(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)

      used = Map.get(module_info, :used, [])
      used = [func | used]

      module_info = Map.put(module_info, :used, used)
      modules = Keyword.put(state.modules, module, module_info)

      %{ state | modules: modules }
    end)
  end

  def put_javascript_module(pid, module, name, path) do
    Agent.update(pid, fn(state) ->
      js_modules = Map.get(state, :js_modules, [])
      js_modules = [{module, name, path} | js_modules]
      %{ state | js_modules: js_modules }
    end)
  end

  def list_javascript_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.js_modules
      |> Enum.map(fn
        {module, _name, _path} ->
          module
      end)
    end)
  end

  def js_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.js_modules
    end)
  end

  def get_js_module_name(pid, module) do
    Agent.get(pid, fn(state) ->
      {_, name, _} = state.js_modules
      |> Enum.find(fn {m, _, _} -> module == m end)
      name
    end)
  end

  def list_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.modules
    end)
  end

  def get_in_memory_module(pid, module) do
    Agent.get(pid, fn(state) ->
      Keyword.get(state.in_memory_modules, module)
    end)
  end

  def get_in_memory_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.in_memory_modules
    end)
  end

  def put_in_memory_module(pid, module, beam) do
    Agent.update(pid, fn(state) ->
      in_memory_modules = Map.get(state, :in_memory_modules, [])
      in_memory_modules = Keyword.put(in_memory_modules, module, beam)
      %{ state | in_memory_modules: in_memory_modules }
    end)
  end
end
<commit_msg>Add default used_modules as empty list<commit_after>defmodule ElixirScript.State do
  @moduledoc false

  # Holds the state for the ElixirScript compiler

  def start_link() do
    Agent.start_link(fn ->
      %{
        modules: Keyword.new,
        js_modules: [],
        in_memory_modules: []
      }
    end)
  end

  def stop(pid) do
    Agent.stop(pid)
  end

  def get_module(pid, module) do
    Agent.get(pid, fn(state) ->
      Keyword.get(state.modules, module)
    end)
  end

  def put_module(pid, module, value) do
    Agent.update(pid, fn(state) ->
      value = Map.put(value, :used, [])
      |> Map.put(:used_modules, [])

      modules = Keyword.put(state.modules, module, value)
      %{ state | modules: modules }
    end)
  end

  def add_used_module(pid, module, used_module) do
    Agent.update(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)

      used_modules = Map.get(module_info, :used_modules, [])
      used_modules = Enum.uniq([used_module | used_modules])

      module_info = Map.put(module_info, :used_modules, used_modules)
      modules = Keyword.put(state.modules, module, module_info)

      %{ state | modules: modules }
    end)
  end


  def has_used?(pid, module, func) do
    Agent.get(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)
      Enum.find(module_info.used, fn(x) -> x == func end) != nil
    end)
  end

  def add_used(pid, module, {_function, _arity} = func) do
    Agent.update(pid, fn(state) ->
      module_info = Keyword.get(state.modules, module)

      used = Map.get(module_info, :used, [])
      used = [func | used]

      module_info = Map.put(module_info, :used, used)
      modules = Keyword.put(state.modules, module, module_info)

      %{ state | modules: modules }
    end)
  end

  def put_javascript_module(pid, module, name, path) do
    Agent.update(pid, fn(state) ->
      js_modules = Map.get(state, :js_modules, [])
      js_modules = [{module, name, path} | js_modules]
      %{ state | js_modules: js_modules }
    end)
  end

  def list_javascript_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.js_modules
      |> Enum.map(fn
        {module, _name, _path} ->
          module
      end)
    end)
  end

  def js_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.js_modules
    end)
  end

  def get_js_module_name(pid, module) do
    Agent.get(pid, fn(state) ->
      {_, name, _} = state.js_modules
      |> Enum.find(fn {m, _, _} -> module == m end)
      name
    end)
  end

  def list_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.modules
    end)
  end

  def get_in_memory_module(pid, module) do
    Agent.get(pid, fn(state) ->
      Keyword.get(state.in_memory_modules, module)
    end)
  end

  def get_in_memory_modules(pid) do
    Agent.get(pid, fn(state) ->
      state.in_memory_modules
    end)
  end

  def put_in_memory_module(pid, module, beam) do
    Agent.update(pid, fn(state) ->
      in_memory_modules = Map.get(state, :in_memory_modules, [])
      in_memory_modules = Keyword.put(in_memory_modules, module, beam)
      %{ state | in_memory_modules: in_memory_modules }
    end)
  end
end

{'fraction_non_alphanumeric': 0.12432518259765005, 'fraction_numerical': 0.0, 'mean_word_length': 2.707474985285462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, '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': '210394', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2204, 'n_words': 484}