-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstate.ex
More file actions
108 lines (91 loc) · 2.37 KB
/
state.ex
File metadata and controls
108 lines (91 loc) · 2.37 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
defmodule ElixirScript.State do
@moduledoc false
# Holds the state for the ElixirScript compiler
def start_link(compiler_opts) do
Agent.start_link(fn ->
%{
compiler_opts: compiler_opts,
modules: Keyword.new,
refs: [],
js_modules: []
}
end)
end
def stop(pid) do
Agent.stop(pid)
end
def get_compiler_opts(pid) do
Agent.get(pid, fn(state) ->
state.compiler_opts
end)
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, [])
%{ state | modules: Keyword.put(state.modules, module, value) }
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 = used ++ [func]
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 = js_modules ++ [{module, name, path}]
%{ 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_foreign_modules(pid) do
Agent.get(pid, fn(state) ->
state.modules
|> Enum.filter(fn
(%{attributes: [__foreign_info__: _]}) -> true
(_) -> false
end)
end)
end
def list_modules(pid) do
Agent.get(pid, fn(state) ->
state.modules
end)
end
end