-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstate.ex
More file actions
141 lines (120 loc) · 3.62 KB
/
Copy pathstate.ex
File metadata and controls
141 lines (120 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This agent holds references to the compiler options, a map all of the modules, and
# a map of modules that define the standard library.
#
# The modules map has the module's name a the key and a ElixirScript.Module struct as the value.
#
# The std_lib_map holds a mapping of the Elixir standard lib module to the
# version implemented here in ElixirScript.
defmodule ElixirScript.Translator.State do
@moduledoc false
alias ElixirScript.Translator.Utils
def start_link(compiler_opts, loaded_modules) do
Agent.start_link(fn ->
%{
compiler_opts: compiler_opts,
modules: Keyword.new,
std_lib_map: build_standard_lib_map(),
loaded_modules: [JS | loaded_modules],
module_references: Keyword.new()
}
end)
end
defp build_standard_lib_map() do
Application.spec(:elixir, :modules)
|> Enum.reduce(Map.new, fn(x, acc) ->
try do
elixirscript_module = (["ElixirScript"] ++ Module.split(x)) |> Module.concat()
Map.put(acc, x, elixirscript_module)
rescue
FunctionClauseError ->
acc
end
end)
end
def set_module_data(pid, module_data) do
Agent.update(pid, fn state ->
%{ state | modules: Keyword.merge(state.modules, module_data) }
end)
end
def get_module_data(pid) do
Agent.get(pid, fn state ->
state.modules
end)
end
def set_loaded_modules(pid, modules) do
Agent.update(pid, fn state ->
%{ state | loaded_modules: [ JS | modules ] }
end)
end
def get(pid) do
Agent.get(pid, &(&1))
end
def get_module_name(pid, {:__aliases__, _, _} = name) do
get_module_name(pid, Utils.quoted_to_name(name))
end
def get_module_name(pid, module_name) do
Agent.get(pid, fn(state) ->
do_get_module_name(module_name, state)
end)
end
defp do_get_module_name(module_name, state) do
std_lib_map = state.std_lib_map
case Map.get(std_lib_map, module_name) do
nil ->
module_name
actual_module_name ->
actual_module_name
end
end
def is_module_loaded?(pid, module) when is_atom(module) do
Agent.get(pid, fn(state) ->
(module in state.loaded_modules)
end)
end
def is_module_loaded?(pid, {:__aliases__, _, _} = module) do
is_module_loaded?(pid, Utils.quoted_to_name(module))
end
def get_module(pid, module) when is_atom(module) do
do_get_module(pid, module)
end
def get_module(pid, {:__aliases__, _, _} = name) do
do_get_module(pid, Utils.quoted_to_name(name))
end
def get_module(pid, module_name_list) when is_list(module_name_list) do
do_get_module(pid, Utils.quoted_to_name({:__aliases__, [], module_name_list}))
end
defp do_get_module(pid, name) do
Agent.get(pid, fn(state) ->
Keyword.get(state.modules, do_get_module_name(name, state))
end)
end
def add_module_reference(pid, module_name, module_ref) do
Agent.update(pid, fn(state) ->
case Keyword.get(state.modules, do_get_module_name(module_ref, state)) do
nil ->
state
module ->
module_references = Keyword.update(state.module_references, module.name, [module_name], fn(x) -> Enum.uniq(x ++ [module_name]) end)
%{ state | module_references: module_references }
end
end)
end
def list_module_references(pid) do
Agent.get(pid, fn(state) ->
state.module_references
end)
end
def list_modules(pid) do
Agent.get(pid, fn(state) ->
Keyword.values(state.modules)
end)
end
def list_module_names(pid) do
Agent.get(pid, fn(state) ->
Keyword.keys(state.modules)
end)
end
def stop(pid) do
Agent.stop(pid)
end
end