Skip to content

Commit 813ee2d

Browse files
committed
Add send and self implementations
1 parent 3bfe6d6 commit 813ee2d

4 files changed

Lines changed: 83 additions & 55 deletions

File tree

lib/elixir_script/translator.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,16 @@ defmodule ElixirScript.Translator do
446446
Spawn.make_spawn_link(module, function, params, env)
447447
end
448448

449+
defp do_translate({:send, _, [id, msg]}, env) do
450+
js = Spawn.call_processes_func("send", [translate!(id, env), translate!(msg, env)])
451+
{js, env}
452+
end
453+
454+
defp do_translate({:self, _, []}, env) do
455+
js = Spawn.call_processes_func("pid", [])
456+
{js, env}
457+
end
458+
449459
defp do_translate({:{}, _, elements}, env) do
450460
quoted = quote do
451461
JS.new(Elixir.Core.Tuple, unquote(elements))

lib/elixir_script/translator/kernel/spawn.ex

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,8 @@ defmodule ElixirScript.Translator.Spawn do
1414

1515
defp do_spawn_with_fn({:fn, _, [{:->, _, [[], body]}]}, env, spawn_func_name) do
1616
{ body, env } = Function.prepare_function_body(body, %{ env | in_process: true })
17-
18-
js_ast = JS.call_expression(
19-
JS.member_expression(
20-
JS.member_expression(
21-
JS.identifier("Elixir"),
22-
JS.member_expression(
23-
JS.identifier("Core"),
24-
JS.identifier("Functions")
25-
)
26-
),
27-
JS.identifier("get_global")
28-
),
29-
[]
30-
)
31-
32-
33-
js_ast = JS.call_expression(
34-
JS.member_expression(
35-
JS.member_expression(
36-
js_ast,
37-
JS.identifier("processes")
38-
),
39-
JS.identifier(spawn_func_name)
40-
),
41-
[
42-
JS.function_expression([], [], JS.block_statement(body), true)
43-
]
44-
)
45-
46-
{ js_ast, env }
17+
js = call_processes_func(spawn_func_name, [JS.function_expression([], [], JS.block_statement(body), true)])
18+
{ js, env }
4719
end
4820

4921
def make_spawn(module, fun, args, env) do
@@ -55,19 +27,6 @@ defmodule ElixirScript.Translator.Spawn do
5527
end
5628

5729
defp do_spawn_with_mod(module, fun, args, env, spawn_func_name) do
58-
59-
js_ast = JS.call_expression(
60-
JS.member_expression(
61-
JS.member_expression(
62-
JS.identifier("Elixir"),
63-
JS.member_expression(
64-
JS.identifier("Core"),
65-
JS.identifier("Functions")
66-
)
67-
),
68-
JS.identifier("get_global")
69-
), [])
70-
7130
functions_module = JS.member_expression(
7231
JS.identifier("Elixir"),
7332
JS.member_expression(
@@ -92,24 +51,47 @@ defmodule ElixirScript.Translator.Spawn do
9251
JS.identifier("null")
9352
end
9453

54+
55+
js = call_processes_func(spawn_func_name, [
56+
functions_module,
57+
JS.literal("run"),
58+
JS.array_expression([func_to_run, JS.array_expression(args), context])
59+
])
60+
61+
{ js, env }
62+
end
63+
64+
65+
66+
def call_processes_func(func_name, params) do
67+
js_ast = JS.call_expression(
68+
JS.member_expression(
69+
JS.member_expression(
70+
JS.identifier("Elixir"),
71+
JS.member_expression(
72+
JS.identifier("Core"),
73+
JS.identifier("Functions")
74+
)
75+
),
76+
JS.identifier("get_global")
77+
),
78+
[]
79+
)
80+
81+
9582
js_ast = JS.call_expression(
9683
JS.member_expression(
9784
JS.member_expression(
9885
js_ast,
9986
JS.identifier("processes")
10087
),
101-
JS.identifier(spawn_func_name)
88+
JS.identifier(func_name)
10289
),
103-
[
104-
functions_module,
105-
JS.literal("run"),
106-
JS.array_expression([func_to_run, JS.array_expression(args), context])
107-
]
90+
params
10891
)
10992

110-
{ js_ast, env }
111-
end
11293

94+
end
11395

11496

11597
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defmodule ElixirScript.Translator.Send.Test do
2+
use ExUnit.Case
3+
import ElixirScript.TestHelper
4+
5+
test "call send outside process" do
6+
ex_ast = quote do
7+
send(pid, "hello")
8+
end
9+
10+
js_code = """
11+
Elixir.Core.Functions.get_global().processes.send(pid, 'hello')
12+
"""
13+
14+
assert_translation(ex_ast, js_code)
15+
end
16+
17+
18+
test "call send inside process" do
19+
ex_ast = quote do
20+
spawn(fn() ->
21+
inside = self()
22+
23+
send(pid, "hello")
24+
end)
25+
end
26+
27+
js_code = """
28+
Elixir.Core.Functions.get_global().processes.spawn(function*() {
29+
let [inside] = Elixir.Core.Patterns.match(Elixir.Core.Patterns.variable(),Elixir.Core.Functions.get_global().processes.pid());
30+
return yield* Elixir.Core.Functions.run(Elixir.Core.Functions.get_global().processes['send'],[pid, 'hello'], null);
31+
})
32+
"""
33+
34+
assert_translation(ex_ast, js_code)
35+
end
36+
end

test/translator/kernel/spawn_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ defmodule ElixirScript.Translator.Spawn.Test do
2424

2525
js_code = """
2626
Elixir.Core.Functions.get_global().processes.spawn(function*() {
27-
yield* Elixir.Core.Functions.run(Window['call'],[1])
28-
return yield* Elixir.Core.Functions.run(Elixir$ElixirScript$Tuple['to_list'],[new Elixir.Core.Tuple(1,2,3)]);
27+
yield* Elixir.Core.Functions.run(Window['call'],[1],null)
28+
return yield* Elixir.Core.Functions.run(Elixir$ElixirScript$Tuple['to_list'],[new Elixir.Core.Tuple(1,2,3)],null);
2929
})
3030
"""
3131

@@ -39,7 +39,7 @@ defmodule ElixirScript.Translator.Spawn.Test do
3939
end
4040

4141
js_code = """
42-
Elixir.Core.Functions.get_global().processes.spawn(Elixir.Core.Functions,'run',[Elixir$ElixirScript$Tuple['to_list'], [new Elixir.Core.Tuple(1,2,3)]])
42+
Elixir.Core.Functions.get_global().processes.spawn(Elixir.Core.Functions,'run',[Elixir$ElixirScript$Tuple['to_list'], [new Elixir.Core.Tuple(1,2,3)], null])
4343
"""
4444

4545
assert_translation(ex_ast, js_code)
@@ -52,7 +52,7 @@ defmodule ElixirScript.Translator.Spawn.Test do
5252
end
5353

5454
js_code = """
55-
Elixir.Core.Functions.get_global().processes.spawn(Elixir.Core.Functions,'run',[Window['call'], [new Elixir.Core.Tuple(1,2,3)]])
55+
Elixir.Core.Functions.get_global().processes.spawn(Elixir.Core.Functions,'run',[Window['call'], [new Elixir.Core.Tuple(1,2,3)], null])
5656
"""
5757

5858
assert_translation(ex_ast, js_code)

0 commit comments

Comments
 (0)