Skip to content

Commit da96e97

Browse files
committed
More updates from testing
1 parent eb20acf commit da96e97

7 files changed

Lines changed: 125 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ defmodule ElixirScript.Translate.Form do
8282
def compile({:%, _, [module, params]}, state) do
8383
ast = J.call_expression(
8484
J.member_expression(
85-
Remote.process_module_name(module),
85+
Remote.process_module_name(module, state),
8686
J.identifier("__struct__")
8787
),
88-
[Form.compile!(params, state)]
88+
[compile!(params, state)]
8989
)
9090

9191
{ ast, state }

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ defmodule ElixirScript.Translate.Forms.Map do
88

99
ast = J.call_expression(
1010
J.member_expression(
11-
J.member_expression(
12-
J.identifier("Bootstrap"),
13-
J.member_expression(
14-
J.identifier("Core"),
15-
J.identifier("SpecialForms")
16-
)
17-
),
18-
J.identifier("map_update")
11+
J.identifier("Object"),
12+
J.identifier("assign")
1913
),
20-
[map, data]
14+
[
15+
J.object_expression([]),
16+
map,
17+
data
18+
]
2119
)
2220

2321
{ ast, state }

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ defmodule ElixirScript.Translate.Forms.Receive do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: J
44

5+
@doc """
6+
receive is not supported just yet, but we compile it
7+
to a stub function for now
8+
"""
59
def compile(blocks, state) do
6-
receive_block = Keyword.get(blocks, :do)
7-
after_block = Keyword.get(blocks, :after, nil)
10+
_receive_block = Keyword.get(blocks, :do)
11+
_after_block = Keyword.get(blocks, :after, nil)
812

913
receive_function = J.member_expression(
1014
J.member_expression(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule ElixirScript.Translate.Module do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: J
44
alias ElixirScript.Translate.Function
5+
alias ElixirScript.Translate.Form
56
alias ElixirScript.Translator.Identifier
67
alias ElixirScript.State, as: ModuleState
78

@@ -86,6 +87,13 @@ defmodule ElixirScript.Translate.Module do
8687
list
8788
end)
8889

90+
# Add an attribute to use to determine if this is a module
91+
# Will be used by the is_atom implementation
92+
exports ++ [ElixirScript.Translate.Forms.Map.make_property(
93+
Form.compile!("__MODULE__", %{}),
94+
Form.compile!(true, %{})
95+
)]
96+
8997
J.object_expression(exports)
9098
end
9199

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
defmodule ElixirScript.Translate.Forms.Map.Test do
2+
use ExUnit.Case
3+
alias ElixirScript.Translate.Form
4+
alias ESTree.Tools.Builder, as: J
5+
6+
test "map with atom key" do
7+
properties = [a: 1]
8+
ast = {:%{}, [], properties}
9+
state = %{}
10+
11+
{js_ast, _} = Form.compile(ast, state)
12+
assert js_ast == J.object_expression([
13+
J.property(
14+
J.call_expression(
15+
J.member_expression(
16+
J.identifier("Symbol"),
17+
J.identifier("for")
18+
),
19+
[J.literal(:a)]
20+
),
21+
J.literal(1),
22+
:init, false, false, true
23+
)
24+
])
25+
end
26+
27+
test "map with string key" do
28+
properties = [{"a", 1}]
29+
ast = {:%{}, [], properties}
30+
state = %{}
31+
32+
{js_ast, _} = Form.compile(ast, state)
33+
assert js_ast == J.object_expression([
34+
J.property(
35+
J.identifier("a"),
36+
J.literal(1)
37+
)
38+
])
39+
end
40+
41+
42+
test "map update" do
43+
properties = [{"a", 1}]
44+
map_ast = {:%{}, [], properties}
45+
new_values = [{"a", 2}]
46+
state = %{}
47+
48+
ast = {:%{}, [], [{:|, [], [map_ast, new_values]}]}
49+
50+
{js_ast, _} = Form.compile(ast, state)
51+
assert js_ast == J.call_expression(
52+
J.member_expression(
53+
J.identifier("Object"),
54+
J.identifier("assign")
55+
),
56+
[
57+
J.object_expression([]),
58+
J.object_expression([
59+
J.property(
60+
J.identifier("a"),
61+
J.literal(1)
62+
)
63+
]),
64+
J.object_expression([
65+
J.property(
66+
J.identifier("a"),
67+
J.literal(2)
68+
)
69+
])
70+
]
71+
)
72+
end
73+
74+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule ElixirScript.Translate.Forms.Receive.Test do
2+
use ExUnit.Case
3+
alias ElixirScript.Translate.Form
4+
alias ESTree.Tools.Builder, as: J
5+
6+
test "call to variable" do
7+
ast = {:receive, [line: 644], [[do: 1, after: 2]]}
8+
state = %{function: {:each, nil}, module: __MODULE__}
9+
10+
{js_ast, _} = Form.compile(ast, state)
11+
assert js_ast == J.call_expression(
12+
J.member_expression(
13+
J.member_expression(
14+
J.identifier("Bootstrap"),
15+
J.member_expression(
16+
J.identifier("Core"),
17+
J.identifier("SpecialForms")
18+
)
19+
),
20+
J.identifier("receive")
21+
),
22+
[]
23+
)
24+
end
25+
26+
end

test/next/passes/translate/forms/remote_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
defmodule ElixirScript.Translate.Forms.Remote.Test do
22
use ExUnit.Case
3-
alias ElixirScript.Translate.Forms.Remote
3+
alias ElixirScript.Translate.Form
44
alias ESTree.Tools.Builder, as: J
55

66
test "call to variable" do
77
ast = {:., [line: 644], [{:fun, [line: 644], nil}]}
88
state = %{function: {:each, nil}, module: Enum, vars: %{:_ => 0, "entry" => 0, "enumerable" => 0, "fun" => 0}}
99

10-
{js_ast, _} = Remote.compile(ast, state)
10+
{js_ast, _} = Form.compile(ast, state)
1111
assert js_ast == J.identifier("fun0")
1212
end
1313

0 commit comments

Comments
 (0)