Skip to content

Commit b827e4e

Browse files
committed
Merge branch 'master' into async_switch
2 parents 80e47db + a4774c6 commit b827e4e

File tree

2 files changed

+86
-61
lines changed

2 files changed

+86
-61
lines changed

.eslintrc.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module.exports = {
22
rules: {
3-
camelcase: "off",
4-
"no-bitwise": "off",
5-
"no-plusplus": "off",
6-
"no-restricted-syntax": "off",
7-
"no-underscore-dangle": "off"
3+
camelcase: 'off',
4+
'no-bitwise': 'off',
5+
'no-plusplus': 'off',
6+
'no-restricted-syntax': 'off',
7+
'no-underscore-dangle': 'off',
8+
'import/extensions': 'off',
89
},
9-
extends: "airbnb-base",
10-
plugins: ["import"],
10+
extends: 'airbnb-base',
11+
plugins: ['import'],
1112
env: {
1213
browser: true,
1314
node: true,
14-
mocha: true
15-
}
15+
mocha: true,
16+
},
1617
};

lib/elixir_script/passes/translate/module.ex

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ defmodule ElixirScript.Translate.Module do
6161
_ ->
6262
{ compiled_functions, _ } = Enum.map_reduce(combined_defs, state, &Function.compile(&1, &2))
6363

64-
compiled_functions = compiled_functions ++ [make_info_function(module, defs, state)]
64+
info_map = make_info_map(module, state)
65+
info_function = make_info_function()
66+
compiled_functions = [info_map, info_function] ++ compiled_functions
6567

6668
js_ast = ElixirScript.ModuleSystems.Namespace.build(
6769
module,
@@ -146,70 +148,92 @@ defmodule ElixirScript.Translate.Module do
146148
end
147149
end
148150

149-
# Builds the __info__ function that Elixir modules
150-
# have. Only supports the `functions`, `macros` and
151-
# `module` kinds
152-
defp make_info_function(module, definitions, state) do
153-
functions = Enum.filter(definitions, fn
154-
{_, :def, _, _} ->
155-
true
156-
_ ->
157-
false
158-
end)
159-
|> Enum.map(fn
160-
{func, _, _, _} ->
161-
func
162-
end)
151+
defp make_info_map(module, state) do
152+
functions = module.__info__(:functions)
153+
|> Form.compile!(state)
163154

164-
functions = Form.compile!(functions, state)
155+
macros = module.__info__(:macros)
156+
|> Form.compile!(state)
165157

166-
macros = Enum.filter(definitions, fn
167-
{_, :defmacro, _, _} ->
168-
true
169-
_ ->
170-
false
171-
end)
172-
|> Enum.map(fn
173-
{func, _, _, _} ->
174-
func
175-
end)
158+
attributes = module.__info__(:attributes)
159+
|> Form.compile!(state)
160+
161+
compile = module.__info__(:compile)
162+
|> Keyword.update(:source, "", fn(x) -> :erlang.list_to_binary(x) end)
163+
|> Form.compile!(state)
164+
165+
md5 = module.__info__(:md5)
166+
|> :erlang.binary_to_list
176167

177-
macros = Form.compile!(macros, state)
168+
md5 = Form.compile!({:<<>>, [], md5}, state)
178169

179170
module = Helpers.symbol(to_string(module))
180171

172+
map_entries = J.array_expression([
173+
J.array_expression([
174+
Helpers.symbol("functions"),
175+
functions
176+
]),
177+
J.array_expression([
178+
Helpers.symbol("macros"),
179+
macros
180+
]),
181+
J.array_expression([
182+
Helpers.symbol("attributes"),
183+
attributes
184+
]),
185+
J.array_expression([
186+
Helpers.symbol("compile"),
187+
compile
188+
]),
189+
J.array_expression([
190+
Helpers.symbol("md5"),
191+
md5
192+
]),
193+
J.array_expression([
194+
Helpers.symbol("module"),
195+
module
196+
]),
197+
])
198+
199+
map = Helpers.new(
200+
J.identifier("Map"),
201+
[
202+
map_entries
203+
]
204+
)
205+
206+
Helpers.declare("__info__map__", map)
207+
end
208+
209+
# Builds the __info__ function that Elixir modules
210+
# have.
211+
defp make_info_function do
212+
get_call = Helpers.call(
213+
J.member_expression(
214+
J.identifier("__info__map__"),
215+
J.identifier("get")
216+
),
217+
[
218+
J.identifier("kind")
219+
]
220+
)
221+
222+
value = Helpers.declare("value", get_call)
223+
181224
body = J.if_statement(
182225
J.binary_expression(
183-
:===,
184-
J.identifier("kind"),
185-
Helpers.symbol("functions")
226+
:!==,
227+
J.identifier("value"),
228+
J.identifier("null")
186229
),
187230
J.block_statement([
188-
J.return_statement(functions)
189-
]),
190-
J.if_statement(
191-
J.binary_expression(
192-
:===,
193-
J.identifier("kind"),
194-
Helpers.symbol("macros")
195-
),
196-
J.block_statement([
197-
J.return_statement(macros)
198-
]),
199-
J.if_statement(
200-
J.binary_expression(
201-
:===,
202-
J.identifier("kind"),
203-
Helpers.symbol("module")
204-
),
205-
J.block_statement([
206-
J.return_statement(module)
207-
])
208-
)
209-
)
231+
J.return_statement(J.identifier("value"))
232+
])
210233
)
211234

212235
body = J.block_statement([
236+
value,
213237
body,
214238
J.throw_statement(
215239
Helpers.new(

0 commit comments

Comments
 (0)