Skip to content

Commit e5dabf5

Browse files
committed
Combining functions with same names and different arities
1 parent 30aa2fa commit e5dabf5

File tree

16 files changed

+64
-55
lines changed

16 files changed

+64
-55
lines changed

lib/elixir_script/next/passes/translate/clause.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule ElixirScript.Translate.Clause do
4141
),
4242
[
4343
J.array_expression(patterns),
44-
J.function_expression(
44+
J.arrow_function_expression(
4545
params,
4646
[],
4747
J.block_statement(body)
@@ -117,7 +117,7 @@ defmodule ElixirScript.Translate.Clause do
117117
|> process_guards
118118
|> Form.compile!(state)
119119

120-
J.function_expression(
120+
J.arrow_function_expression(
121121
params,
122122
[],
123123
J.block_statement([

lib/elixir_script/next/passes/translate/examples/example.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ defmodule Example do
44
[d, b, c] = [1, 2, 3]
55
[e | g] = [1, 2, 3]
66
end
7-
87
end

lib/elixir_script/next/passes/translate/form.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ defmodule ElixirScript.Translate.Form do
123123
{ translated_body, state } = Enum.map_reduce(List.wrap(clause_body), state, &compile(&1, &2))
124124

125125
translated_body = Clause.return_last_statement(translated_body)
126-
translated_body = J.function_expression([], [], J.block_statement(translated_body))
126+
translated_body = J.arrow_function_expression([], [], J.block_statement(translated_body))
127127

128128
translated_clause = compile(hd(clause), state)
129129

@@ -406,7 +406,7 @@ defmodule ElixirScript.Translate.Form do
406406
),
407407
J.identifier(module)
408408
),
409-
ElixirScript.Translator.Identifier.make_function_name(function, length(params))
409+
ElixirScript.Translator.Identifier.make_function_name(function)
410410
),
411411
Enum.map(params, &compile!(&1, state))
412412
)
@@ -422,7 +422,7 @@ defmodule ElixirScript.Translate.Form do
422422
{function_name, _} = Map.get(state, :function)
423423

424424
ast = J.call_expression(
425-
ElixirScript.Translator.Identifier.make_function_name(function_name, length(params)),
425+
ElixirScript.Translator.Identifier.make_function_name(function_name),
426426
Enum.map(params, &compile!(&1, state))
427427
)
428428

@@ -433,7 +433,7 @@ defmodule ElixirScript.Translate.Form do
433433
ast = case function_name do
434434
a when is_atom(a) ->
435435
J.call_expression(
436-
ElixirScript.Translator.Identifier.make_function_name(function_name, length(params)),
436+
ElixirScript.Translator.Identifier.make_function_name(function_name),
437437
Enum.map(params, &compile!(&1, state))
438438
)
439439
_ ->

lib/elixir_script/next/passes/translate/forms/call.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule ElixirScript.Translate.Forms.Call do
88
ElixirScript.Translate.Module.is_js_module(module, state) ->
99
ElixirScript.Translator.Identifier.make_extern_function_name(function)
1010
true ->
11-
ElixirScript.Translator.Identifier.make_function_name(function, length(params))
11+
ElixirScript.Translator.Identifier.make_function_name(function)
1212
end
1313

1414
ast = J.call_expression(

lib/elixir_script/next/passes/translate/forms/for.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ElixirScript.Translate.Forms.For do
1010
generators = JS.array_expression(args.generators)
1111

1212
into = args.into || JS.array_expression([])
13-
filter = args.filter || JS.function_expression([], [], JS.block_statement([JS.return_statement(JS.identifier("true"))]))
13+
filter = args.filter || JS.arrow_function_expression([], [], JS.block_statement([JS.return_statement(JS.identifier("true"))]))
1414
fun = args.fun
1515

1616

@@ -128,7 +128,7 @@ defmodule ElixirScript.Translate.Forms.For do
128128

129129
ast = Clause.return_last_statement(ast)
130130

131-
JS.function_expression(
131+
JS.arrow_function_expression(
132132
state.args,
133133
[],
134134
JS.block_statement(ast)

lib/elixir_script/next/passes/translate/forms/try.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule ElixirScript.Translate.Forms.Try do
1414
translated_body = prepare_function_body(try_block, state)
1515

1616
translated_body = JS.block_statement(translated_body)
17-
try_block = JS.function_expression([], [], translated_body)
17+
try_block = JS.arrow_function_expression([], [], translated_body)
1818

1919
rescue_block = if rescue_block do
2020
process_rescue_block(rescue_block, state)
@@ -88,7 +88,7 @@ defmodule ElixirScript.Translate.Forms.Try do
8888
translated_body = prepare_function_body(after_block, state)
8989
translated_body = JS.block_statement(translated_body)
9090

91-
JS.function_expression([], [], translated_body)
91+
JS.arrow_function_expression([], [], translated_body)
9292
end
9393

9494
defp prepare_function_body(body, state) do

lib/elixir_script/next/passes/translate/function.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule ElixirScript.Translate.Function do
3232
arg_matches_declaration = J.variable_declaration([arg_matches_declarator], :let)
3333

3434
declarator = J.variable_declarator(
35-
ElixirScript.Translator.Identifier.make_function_name(name, arity),
35+
ElixirScript.Translator.Identifier.make_function_name(name),
3636
J.function_expression(
3737
[J.rest_element(J.identifier("__function_args__"))],
3838
[],

lib/elixir_script/next/passes/translate/module.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,21 @@ defmodule ElixirScript.Translate.Module do
4848
end)
4949
end
5050

51+
#we combine our function arities
52+
combined_defs = used_defs
53+
|> Enum.sort(fn { {name1, arity1}, _, _, _ }, { {name2, arity2}, _, _, _ } -> "#{name1}#{arity1}" < "#{name2}#{arity2}" end)
54+
|> Enum.group_by(fn {{name, _}, _, _, _ } -> name end)
55+
|> Enum.map(fn {group, funs} ->
56+
{_, type, _, _} = hd(funs)
57+
Enum.reduce(funs, {{group, nil}, type, [], []}, fn {_, _, _, clauses}, {name, type, context, acc_clauses} ->
58+
{name, type, context, acc_clauses ++ clauses}
59+
end)
60+
end)
5161

52-
{ compiled_functions, _ } = used_defs
62+
{ compiled_functions, _ } = combined_defs
5363
|> Enum.map_reduce(state, &Function.compile(&1, &2))
5464

55-
exports = make_exports(used_defs)
65+
exports = make_exports(combined_defs)
5666

5767
js_ast = ElixirScript.ModuleSystems.Namespace.build(
5868
module,
@@ -67,7 +77,7 @@ defmodule ElixirScript.Translate.Module do
6777
defp make_exports(reachable_defs) do
6878
exports = Enum.reduce(reachable_defs, [], fn
6979
{{name, arity}, :def, _, _}, list ->
70-
function_name = ElixirScript.Translator.Identifier.make_function_name(name, arity)
80+
function_name = ElixirScript.Translator.Identifier.make_identifier(name)
7181
list ++ [J.property(function_name, function_name, :init, true)]
7282
_, list ->
7383
list

lib/elixir_script/next/passes/translate/protocol.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ defmodule ElixirScript.Translate.Protocol do
1111
This compiles and consolidates the given protocol
1212
"""
1313
def compile(module, %{protocol: true, impls: impls, functions: functions} = info, pid) do
14-
object = Enum.map(functions, fn {function, arity} ->
15-
{Identifier.make_function_name(function, arity), J.function_expression([], [], J.block_statement([]))}
14+
object = Enum.map(functions, fn {function, _} ->
15+
{Identifier.make_function_name(function), J.function_expression([], [], J.block_statement([]))}
1616
end)
1717
|> Enum.map(fn({key, value}) -> ElixirScript.Translate.Forms.Map.make_property(key, value) end)
1818
|> J.object_expression

lib/elixir_script/translator/kernel/special_forms/identifier.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ defmodule ElixirScript.Translator.Identifier do
8686
|> make_alias
8787
end
8888

89-
def make_function_name(name, arity) when is_atom(name) do
89+
def make_function_name(name) when is_atom(name) do
9090
name = filter_name(name)
91-
JS.identifier("#{name}#{arity}")
91+
JS.identifier(name)
9292
end
9393

9494
def make_extern_function_name(name) do

0 commit comments

Comments
 (0)