Skip to content

Commit 2b38d06

Browse files
committed
Add __info__ to each module
1 parent 455113e commit 2b38d06

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

lib/elixir_script/translator.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,18 @@ defmodule ElixirScript.Translator do
523523
end
524524

525525
defp do_translate({function, _, [{:when, _, [{name, _, _params} | _guards] }, _] } = ast, env) when function in @generator_types do
526-
Def.process_function(name, [ast], %{ env | context: :generator})
526+
{js_ast, _} = Def.process_function(name, [ast], %{ env | context: :generator})
527+
{js_ast, env}
527528
end
528529

529530
defp do_translate({function, _, [{name, _, params}, _]} = ast, env) when function in @generator_types and is_atom(params) do
530-
Def.process_function(name, [ast], %{ env | context: :generator})
531+
{js_ast, _} = Def.process_function(name, [ast], %{ env | context: :generator})
532+
{js_ast, env}
531533
end
532534

533535
defp do_translate({function, _, [{name, _, _params}, _]} = ast, env) when function in @generator_types do
534-
Def.process_function(name, [ast], %{ env | context: :generator})
536+
{js_ast, _} = Def.process_function(name, [ast], %{ env | context: :generator})
537+
{js_ast, env}
535538
end
536539

537540
defp do_translate({function, _, [{:when, _, [{name, _, _params} | _guards] }, _] } = ast, env) when function in @function_types do

lib/elixir_script/translator/kernel/defmodule.ex

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ defmodule ElixirScript.Translator.Defmodule do
5757
[]
5858
end
5959

60+
info_prop = [JS.property(Identifier.make_identifier("__info__"), Identifier.make_identifier("__info__"), :init, true)]
61+
6062
body = Enum.map(body, fn(x) ->
6163
case x do
6264
%ESTree.CallExpression{} ->
@@ -69,7 +71,7 @@ defmodule ElixirScript.Translator.Defmodule do
6971
body = Group.inflate_groups(body)
7072

7173
exported_object = JS.object_expression(
72-
struct_prop ++
74+
info_prop ++ struct_prop ++
7375
Enum.map(exported_functions, fn({key, _value}) ->
7476
JS.property(Identifier.make_identifier(key), Identifier.make_identifier(key), :init, true)
7577
end)
@@ -78,7 +80,7 @@ defmodule ElixirScript.Translator.Defmodule do
7880
exported_functions = Enum.map(exported_functions, fn({_key, value}) -> value end)
7981
private_functions = Enum.map(private_functions, fn({_key, value}) -> value end)
8082

81-
body = private_functions ++ exported_functions ++ body
83+
body = private_functions ++ exported_functions ++ [make_info_function(env)] ++ body
8284
{body, exported_object}
8385
end
8486

@@ -230,4 +232,36 @@ defmodule ElixirScript.Translator.Defmodule do
230232
JS.variable_declaration([declarator], :const)
231233
end
232234

235+
def make_info_function(env) do
236+
functions = Keyword.get(env.functions, env.module, [])
237+
macros = Keyword.get(env.macros, env.module, [])
238+
239+
info_case = quote do
240+
case kind do
241+
:functions ->
242+
unquote(functions)
243+
:macros ->
244+
unquote(macros)
245+
:module ->
246+
unquote(env.module)
247+
end
248+
end
249+
250+
translated_case = ElixirScript.Translator.translate!(info_case, env)
251+
252+
declarator = JS.variable_declarator(
253+
Identifier.make_identifier("__info__"),
254+
JS.function_expression(
255+
[JS.identifier("kind")],
256+
[],
257+
JS.block_statement([
258+
JS.return_statement(translated_case)
259+
])
260+
)
261+
)
262+
263+
JS.variable_declaration([declarator], :const)
264+
265+
end
266+
233267
end

test/translator/defmodule_test.exs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ defmodule ElixirScript.Translator.Defmodule.Test do
99
end
1010

1111
js_code = """
12-
const __exports = {};
12+
const __exports = {
13+
__info__
14+
};
1315
"""
1416

1517
assert_translation(ex_ast, js_code)
@@ -31,19 +33,25 @@ defmodule ElixirScript.Translator.Defmodule.Test do
3133
end
3234

3335
js_code = """
34-
const something = Bootstrap.Core.Patterns.defmatchgen(Bootstrap.Core.Patterns.clause([], function*() {
36+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([], function() {
3537
return ul;
3638
}));
3739
40+
const __info__ = function(kind) {
41+
return Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Symbol.for('functions')], function() {
42+
return Object.freeze([new Bootstrap.Core.Tuple(Symbol.for('something'), 0)]);
43+
}), Bootstrap.Core.Patterns.clause([Symbol.for('macros')], function() {
44+
return Object.freeze([]);
45+
}), Bootstrap.Core.Patterns.clause([Symbol.for('module')], function() {
46+
return Symbol.for('Elixir.Elephant');
47+
})).call(this, kind);
48+
};
49+
3850
const ul = '#todo-list';
3951
4052
const something_else = Bootstrap.Core.Patterns.defmatchgen(Bootstrap.Core.Patterns.clause([], function*() {
4153
return null;
4254
}));
43-
44-
const __exports = {
45-
something
46-
};
4755
"""
4856

4957
assert_translation(ex_ast, js_code)

0 commit comments

Comments
 (0)