Skip to content

Commit d8bd1d6

Browse files
committed
Fixed bugs from testing. Got app test working again
1 parent a125a00 commit d8bd1d6

File tree

9 files changed

+37
-30
lines changed

9 files changed

+37
-30
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ defmodule ElixirScript.Translate.Clause do
3030
List.flatten(list)
3131
_ ->
3232
Form.compile!(body, state)
33+
|> List.wrap
34+
|> List.flatten
3335
end
3436

35-
body = return_last_statement(body)
37+
body = body
38+
|> return_last_statement
3639

3740
ast = J.call_expression(
3841
J.member_expression(

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule ElixirScript.Translate.Form do
22
alias ESTree.Tools.Builder, as: J
3-
alias ElixirScript.Translate.Forms.{Bitstring, Match, Try, For, Struct, Receive, Remote}
3+
alias ElixirScript.Translate.Forms.{Bitstring, Match, Try, For, Struct, Receive, Remote, Pattern}
44
alias ElixirScript.Translate.Functions.{Erlang, Lists, Maps}
55
alias ElixirScript.Translator.Identifier
66
alias ElixirScript.Translate.Clause
@@ -62,7 +62,7 @@ defmodule ElixirScript.Translate.Form do
6262
),
6363
J.identifier("Tuple")
6464
),
65-
Enum.map(elements, &compile!(&1, state))
65+
Enum.map(elements, &compile!(&1, state)) |> List.flatten
6666
)
6767

6868
{ast, state}
@@ -94,7 +94,7 @@ defmodule ElixirScript.Translate.Form do
9494
ElixirScript.Translate.Function.patterns_ast(),
9595
J.identifier("defmatch")
9696
),
97-
Enum.map(clauses, fn x -> Clause.compile(x, state) |> elem(0) end)
97+
Enum.map(clauses, fn x -> Clause.compile(x, state) |> elem(0) end) |> List.flatten
9898
)
9999

100100
ast = J.call_expression(
@@ -109,10 +109,13 @@ defmodule ElixirScript.Translate.Form do
109109
processed_clauses = Enum.map(clauses, fn({:->, _, [clause, clause_body]}) ->
110110
{ translated_body, state } = Enum.map_reduce(List.wrap(clause_body), state, &compile(&1, &2))
111111

112-
translated_body = Clause.return_last_statement(translated_body)
112+
translated_body = translated_body
113+
|> List.flatten
114+
|> Clause.return_last_statement
115+
113116
translated_body = J.arrow_function_expression([], [], J.block_statement(translated_body))
114117

115-
translated_clause = compile(hd(clause), state)
118+
{ translated_clause, _ } = compile(hd(clause), state)
116119

117120

118121
J.array_expression([translated_clause, translated_body])
@@ -172,7 +175,16 @@ defmodule ElixirScript.Translate.Form do
172175

173176
ast = J.call_expression(
174177
ElixirScript.Translator.Identifier.make_function_name(function_name),
175-
Enum.map(params, &compile!(&1, state))
178+
Enum.map(params, &compile!(&1, state)) |> List.flatten
179+
)
180+
181+
{ast, state}
182+
end
183+
184+
def compile({var, _, params}, state) when is_list(params) and is_atom(var) do
185+
ast = J.call_expression(
186+
ElixirScript.Translator.Identifier.make_function_name(var),
187+
Enum.map(params, &compile!(&1, state)) |> List.flatten
176188
)
177189

178190
{ast, state}
@@ -181,13 +193,14 @@ defmodule ElixirScript.Translate.Form do
181193
def compile({function, _, params}, state) when is_list(params) do
182194
ast = J.call_expression(
183195
compile!(function, state),
184-
Enum.map(params, &compile!(&1, state))
196+
Enum.map(params, &compile!(&1, state)) |> List.flatten
185197
)
186198

187199
{ast, state}
188200
end
189201

190202
def compile({var, _, _}, state) do
203+
var = Pattern.get_variable_name(to_string(var), state)
191204
{ ElixirScript.Translator.Identifier.make_identifier(var), state }
192205
end
193206

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule ElixirScript.Translate.Forms.For do
6565
{var, collection}
6666
end)
6767

68-
{patterns, params, state} = Pattern.compile([{:<<>>, [], bs_parts}], module_state)
68+
{patterns, params, module_state} = Pattern.compile([{:<<>>, [], bs_parts}], module_state)
6969

7070
gen = JS.call_expression(
7171
JS.member_expression(
@@ -126,7 +126,9 @@ defmodule ElixirScript.Translate.Forms.For do
126126
Form.compile(x, acc_state)
127127
end)
128128

129-
ast = Clause.return_last_statement(ast)
129+
ast = ast
130+
|> List.flatten
131+
|> Clause.return_last_statement
130132

131133
JS.arrow_function_expression(
132134
state.args,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule ElixirScript.Translate.Forms.Pattern do
4040
{ patterns, params, state }
4141
end
4242

43-
defp get_variable_name(function, state) do
43+
def get_variable_name(function, state) do
4444
number = Map.get(state.vars, function)
4545
String.to_atom("#{function}#{number}")
4646
end

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ defmodule ElixirScript.Translate.Forms.Try do
9696
|> List.wrap
9797
|> Enum.map_reduce(state, &Form.compile(&1, &2))
9898

99-
Clause.return_last_statement(ast)
99+
ast
100+
|> List.flatten
101+
|> Clause.return_last_statement
100102
end
101103
end

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ defmodule ElixirScript.Translate.Function do
104104
Form.compile!(body, state)
105105
end
106106

107-
body = Clause.return_last_statement(body)
107+
body = body
108+
|> Clause.return_last_statement
108109

109110
declarator = J.variable_declarator(
110111
J.array_expression(params),

lib/elixir_script/passes/create_js_modules.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ defmodule ElixirScript.Passes.CreateJSModules do
100100
),
101101
[JS.identifier("Elixir")]
102102
),
103-
JS.identifier("start2")
103+
JS.identifier("start")
104104
),
105105
[ElixirScript.Translator.Primitive.make_atom(:normal), JS.identifier(:args)]
106106
)

test/app/spec/main.spec.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ const Elixir = require('../build/Elixir.App');
66
test('Elixir.start:calls the modules start function', t => {
77
const callback = sinon.spy();
88

9-
Elixir.start(Elixir.Main, callback);
9+
Elixir.start(Elixir.Main, [callback]);
1010

1111
t.true(callback.called);
1212
});
13-
14-
test('Elixir.load:loads the modules exports', t => {
15-
const main = Elixir.load(Elixir.Main);
16-
17-
t.truthy(main.start);
18-
t.truthy(main.hello);
19-
t.is(main.hello(), 'Hello!');
20-
});

test/support/main.ex

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
defmodule Main do
2-
def start(:normal, callback) do
3-
elem({1, 2}, 0)
4-
Enum.map([], fn(x) -> x end)
2+
def start(:normal, [callback]) do
53
callback.("started")
64
end
7-
8-
def hello do
9-
"Hello!"
10-
end
115
end

0 commit comments

Comments
 (0)