Skip to content

Commit b4db016

Browse files
committed
Added processes module
1 parent 71e7d49 commit b4db016

File tree

8 files changed

+98
-33
lines changed

8 files changed

+98
-33
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
defmodule ElixirScript.Process do
2+
3+
def alive?(pid) do
4+
Elixir.Core.processes.is_alive(pid)
5+
end
6+
7+
def delete(key) do
8+
Elixir.Core.processes.erase(key)
9+
end
10+
11+
def exit(pid, reason) do
12+
Elixir.Core.processes.exit(pid, reason)
13+
end
14+
15+
def flag(flag, value) do
16+
Elixir.Core.processes.process_flag(flag, value)
17+
end
18+
19+
def flag(pid, flag, value) do
20+
Elixir.Core.processes.process_flag(pid, flag, value)
21+
end
22+
23+
def get() do
24+
Elixir.Core.processes.get_process_dict()
25+
end
26+
27+
def get(key, default \\ nil) do
28+
Elixir.Core.processes.get(key, default)
29+
end
30+
31+
def get_keys() do
32+
Elixir.Core.processes.get_keys()
33+
end
34+
35+
def get_keys(value) do
36+
Elixir.Core.processes.get_keys(value)
37+
end
38+
39+
def put(key, value) do
40+
Elixir.Core.processes.put(key, value)
41+
end
42+
43+
def link(pid) do
44+
Elixir.Core.processes.link(pid)
45+
end
46+
47+
def unlink(pid) do
48+
Elixir.Core.processes.unlink(pid)
49+
end
50+
51+
def register(pid, name) when is_atom(name) do
52+
Elixir.Core.processes.register(pid, name)
53+
end
54+
55+
def registered() do
56+
Elixir.Core.processes.registered()
57+
end
58+
59+
def whereis(name) do
60+
Elixir.Core.processes.whereis(name)
61+
end
62+
63+
def list() do
64+
Elixir.Core.processes.list()
65+
end
66+
67+
68+
end

lib/elixir_script/translator.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule ElixirScript.Translator do
2929
alias ElixirScript.Translator.Rewriter
3030
alias ElixirScript.Translator.Spawn
3131
alias ElixirScript.Translator.Receive
32+
alias ElixirScript.Translator.LexicalScope
3233

3334
# A list of erlang modules. These are rewritten into equivalent
3435
# JavaScript functions using ElixirScript.Translator.Rewriter
@@ -443,6 +444,14 @@ defmodule ElixirScript.Translator do
443444
Spawn.make_spawn_link(module, function, params, env)
444445
end
445446

447+
defp do_translate({:spawn_monitor, _, [{:fn, _, _} = func]}, env) do
448+
Spawn.make_spawn_monitor(func, env)
449+
end
450+
451+
defp do_translate({:spawn_monitor, _, [module, function, params]}, env) do
452+
Spawn.make_spawn_monitor(module, function, params, env)
453+
end
454+
446455
defp do_translate({:send, _, [id, msg]}, env) do
447456
js = Spawn.call_processes_func("send", [translate!(id, env), translate!(msg, env)])
448457
{js, env}

lib/elixir_script/translator/kernel/spawn.ex

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ defmodule ElixirScript.Translator.Spawn do
1212
do_spawn_with_fn(func, env, "spawn_link")
1313
end
1414

15+
def make_spawn_monitor(func, env) do
16+
do_spawn_with_fn(func, env, "spawn_monitor")
17+
end
18+
1519
defp do_spawn_with_fn({:fn, _, [{:->, _, [[], body]}]}, env, spawn_func_name) do
1620
{ body, env } = Function.prepare_function_body(body, %{ env | in_process: true })
1721
js = call_processes_func(spawn_func_name, [JS.function_expression([], [], JS.block_statement(body), true)])
@@ -26,6 +30,10 @@ defmodule ElixirScript.Translator.Spawn do
2630
do_spawn_with_mod(module, fun, args, env, "spawn_link")
2731
end
2832

33+
def make_spawn_monitor(module, fun, args, env) do
34+
do_spawn_with_mod(module, fun, args, env, "spawn_monitor")
35+
end
36+
2937
defp do_spawn_with_mod(module, fun, args, env, spawn_func_name) do
3038
functions_module = JS.member_expression(
3139
JS.identifier("Elixir"),
@@ -64,33 +72,19 @@ defmodule ElixirScript.Translator.Spawn do
6472

6573

6674
def call_processes_func(func_name, params) do
67-
js_ast = JS.call_expression(
75+
JS.call_expression(
6876
JS.member_expression(
6977
JS.member_expression(
7078
JS.identifier("Elixir"),
7179
JS.member_expression(
7280
JS.identifier("Core"),
73-
JS.identifier("Functions")
81+
JS.identifier("processes")
7482
)
7583
),
76-
JS.identifier("get_global")
77-
),
78-
[]
79-
)
80-
81-
82-
js_ast = JS.call_expression(
83-
JS.member_expression(
84-
JS.member_expression(
85-
js_ast,
86-
JS.identifier("processes")
87-
),
8884
JS.identifier(func_name)
8985
),
9086
params
9187
)
92-
93-
9488
end
9589

9690

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ defmodule ElixirScript.Translator.Block do
3232
end
3333

3434
defp make_gen_call(callee, func, params) do
35-
context = case callee do
36-
%ESTree.Identifier{ name: "console" } ->
37-
JS.identifier("console")
38-
_ ->
39-
JS.identifier("null")
40-
end
35+
context = callee
4136

4237

4338
JS.yield_expression(

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ defmodule ElixirScript.Translator.Receive do
88

99

1010
def make_receive([do: clauses], %LexicalScope{ in_process: true} = env) do
11-
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, env)
12-
11+
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, %{ env | in_process: false})
1312

1413
js = JS.yield_expression(
1514
Spawn.call_processes_func("receive", [
@@ -29,7 +28,7 @@ defmodule ElixirScript.Translator.Receive do
2928
end
3029

3130
def make_receive([do: clauses], env) do
32-
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, env)
31+
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, %{ env | in_process: false})
3332

3433
js = Spawn.call_processes_func("receive", [
3534
JS.function_expression(
@@ -47,8 +46,8 @@ defmodule ElixirScript.Translator.Receive do
4746
end
4847

4948
def make_receive([do: clauses, after: [{:->, _, [[time], _body]}] = after_clause], %LexicalScope{ in_process: true } = env) do
50-
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, env)
51-
{anon_func, _} = ElixirScript.Translator.Function.make_anonymous_function(after_clause, env)
49+
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, %{ env | in_process: false})
50+
{anon_func, _} = ElixirScript.Translator.Function.make_anonymous_function(after_clause, %{ env | in_process: false})
5251

5352
js = JS.yield_expression(
5453
Spawn.call_processes_func("receive", [
@@ -70,8 +69,8 @@ defmodule ElixirScript.Translator.Receive do
7069
end
7170

7271
def make_receive([do: clauses, after: [{:->, _, [[time], _body]}] = after_clause], env) do
73-
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, env)
74-
{anon_func, _} = ElixirScript.Translator.Function.make_anonymous_function(after_clause, env)
72+
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, %{ env | in_process: false})
73+
{anon_func, _} = ElixirScript.Translator.Function.make_anonymous_function(after_clause, %{ env | in_process: false})
7574

7675

7776
js = Spawn.call_processes_func("receive", [

lib/elixir_script/translator/state.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ defmodule ElixirScript.Translator.State do
5252
|> Map.put(MapSet, ElixirScript.MapSet)
5353
|> Map.put(List, ElixirScript.List)
5454
|> Map.put(JS, ElixirScript.JS)
55-
|> Map.put(VDom, ElixirScript.VDom)
56-
|> Map.put(Html, ElixirScript.Html)
55+
|> Map.put(Process, ElixirScript.Process)
5756
end
5857

5958
def add_module(module) do

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"author": "",
2020
"license": "MIT",
2121
"dependencies": {
22-
"erlang-processes": "^1.0.2"
22+
"erlang-processes": "bryanjos/processes"
2323
},
2424
"devDependencies": {
2525
"babel": "^6.5.2",

src/javascript/lib/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import Patterns from './core/patterns';
55
import Functions from './core/functions';
66
import SpecialForms from './core/special_forms';
77

8-
Functions.get_global().processes = Functions.get_global().processes || new Processes.ProcessSystem();
8+
let processes = new Processes.ProcessSystem();
99

1010
export default {
1111
ProcessSystem: Processes.ProcessSystem,
12+
processes: processes,
1213
Tuple,
1314
PID,
1415
BitString,

0 commit comments

Comments
 (0)