Skip to content

Commit 6e333f4

Browse files
committed
Some refactoring of process functions and javascript functions
1 parent 5ac6fc8 commit 6e333f4

File tree

11 files changed

+106
-219
lines changed

11 files changed

+106
-219
lines changed

lib/elixir_script/prelude/kernel.ex

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ defmodule ElixirScript.Kernel do
3535
end
3636
end
3737

38+
defmacro typeof(term) do
39+
quote do
40+
JS.typeof(unquote(term))
41+
end
42+
end
43+
44+
defmacro instanceof(term, type) do
45+
quote do
46+
JS.instanceof(unquote(term), unquote(type))
47+
end
48+
end
49+
3850
def abs(number) do
3951
Math.abs(number)
4052
end
@@ -61,19 +73,19 @@ defmodule ElixirScript.Kernel do
6173
end
6274

6375
def is_atom(term) do
64-
JS.typeof(term) === "symbol"
76+
typeof(term) === "symbol"
6577
end
6678

6779
def is_binary(term) do
68-
JS.typeof(term) === "string"
80+
typeof(term) === "string"
6981
end
7082

7183
def is_bitstring(term) do
72-
is_binary(term) || JS.instanceof(term, Elixir.Core.BitString)
84+
is_binary(term) || instanceof(term, Elixir.Core.BitString)
7385
end
7486

7587
def is_boolean(term) do
76-
JS.typeof(term) === "boolean" || JS.instanceof(term, Boolean)
88+
JS.typeof(term) === "boolean" || instanceof(term, Boolean)
7789
end
7890

7991
def is_float(term) do
@@ -85,7 +97,7 @@ defmodule ElixirScript.Kernel do
8597
end
8698

8799
def is_function(term, _) do
88-
JS.typeof(term) === "function" || JS.instanceof(term, Function)
100+
typeof(term) === "function" || instanceof(term, Function)
89101
end
90102

91103
def is_integer(term) do
@@ -97,7 +109,7 @@ defmodule ElixirScript.Kernel do
97109
end
98110

99111
def is_number(term) do
100-
JS.typeof(term) === "number" || JS.instanceof(term, Number)
112+
typeof(term) === "number" || instanceof(term, Number)
101113
end
102114

103115
def is_pid(term) do
@@ -109,7 +121,11 @@ defmodule ElixirScript.Kernel do
109121
end
110122

111123
def is_map(term) do
112-
JS.typeof(term) === "object" || JS.instanceof(term, Object)
124+
typeof(term) === "object" || instanceof(term, Object)
125+
end
126+
127+
def is_generator(term) do
128+
term.constructor.name === "GeneratorFunction"
113129
end
114130

115131
def is_port(_) do
@@ -160,6 +176,29 @@ defmodule ElixirScript.Kernel do
160176
Elixir.Core.processes.make_ref()
161177
end
162178

179+
def spawn(module, fun, args) do
180+
fun = if Elixir.Core.is_atom(fun), do: Atom.to_string(fun), else: fun
181+
Elixir.Core.processes.spawn(module, fun, args)
182+
end
183+
184+
def spawn_link(module, fun, args) do
185+
fun = if Elixir.Core.is_atom(fun), do: Atom.to_string(fun), else: fun
186+
Elixir.Core.processes.spawn_link(module, fun, args)
187+
end
188+
189+
def spawn_monitor(module, fun, args) do
190+
fun = if Elixir.Core.is_atom(fun), do: Atom.to_string(fun), else: fun
191+
Elixir.Core.processes.spawn_monitor(module, fun, args)
192+
end
193+
194+
def send(pid, message) do
195+
Elixir.Core.processes.send(pid, message)
196+
end
197+
198+
def self() do
199+
Elixir.Core.processes.pid()
200+
end
201+
163202
defmacro match?(left, right) do
164203
quote do
165204
case unquote(right) do
@@ -197,6 +236,24 @@ defmodule ElixirScript.Kernel do
197236
end
198237
end
199238

239+
defmacro yield() do
240+
quote do
241+
JS.yield()
242+
end
243+
end
244+
245+
defmacro yield(term) do
246+
quote do
247+
JS.yield(unquote(term))
248+
end
249+
end
250+
251+
defmacro yield_to(term) do
252+
quote do
253+
JS.yield_to(unquote(term))
254+
end
255+
end
256+
200257
@doc """
201258
Provides a convenient way to create a string-based map.
202259

lib/elixir_script/translator.ex

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ defmodule ElixirScript.Translator do
2727
alias ElixirScript.Translator.JS, as: JSLib
2828
alias ESTree.Tools.Builder, as: JS
2929
alias ElixirScript.Translator.Rewriter
30-
alias ElixirScript.Translator.Spawn
3130
alias ElixirScript.Translator.Receive
3231
alias ElixirScript.Translator.LexicalScope
3332

@@ -137,7 +136,7 @@ defmodule ElixirScript.Translator do
137136
Expression.make_unary_expression(:!, value, env)
138137
end
139138

140-
defp do_translate({operator, _, [left, right]}, env) when operator in [:+, :-, :/, :*, :==, :!=, :&&, :||, :>, :<, :>=, :<=, :===, :!==] do
139+
defp do_translate({operator, _, [left, right]}, env) when operator in [:+, :-, :/, :*, :==, :!=, :&&, :||, :>, :<, :>=, :<=, :===, :!==, :"**"] do
141140
Expression.make_binary_expression(operator, left, right, env)
142141
end
143142

@@ -301,8 +300,9 @@ defmodule ElixirScript.Translator do
301300
{ Identifier.make_identifier(:undefined), env }
302301
end
303302

304-
defp do_translate({:__aliases__, _, aliases}, env) do
305-
{ Identifier.make_identifier({:__aliases__, [], aliases}), env }
303+
defp do_translate({:__aliases__, _, aliases} = ast, env) do
304+
module_name = create_module_name(ast, env)
305+
Call.make_module_name(module_name, env)
306306
end
307307

308308
defp do_translate({:__MODULE__, _, _ }, env) do
@@ -456,49 +456,6 @@ defmodule ElixirScript.Translator do
456456
Function.make_anonymous_function(clauses, env)
457457
end
458458

459-
defp do_translate({:gn, _, clauses}, env) do
460-
Function.make_anonymous_function(clauses, %{ env | context: :generator})
461-
end
462-
463-
defp do_translate({:spawn, _, [{:fn, _, _} = func]}, env) do
464-
Spawn.make_spawn(func, %{ env | context: :generator})
465-
end
466-
467-
defp do_translate({:spawn, _, [module, function, params]}, env) do
468-
Spawn.make_spawn(module, function, params, env)
469-
end
470-
471-
defp do_translate({:spawn_link, _, [{:fn, _, _} = func]}, env) do
472-
Spawn.make_spawn_link(func, %{ env | context: :generator})
473-
end
474-
475-
defp do_translate({:spawn_link, _, [module, function, params]}, env) do
476-
Spawn.make_spawn_link(module, function, params, env)
477-
end
478-
479-
defp do_translate({:spawn_monitor, _, [{:fn, _, _} = func]}, env) do
480-
Spawn.make_spawn_monitor(func, %{ env | context: :generator})
481-
end
482-
483-
defp do_translate({:spawn_monitor, _, [module, function, params]}, env) do
484-
Spawn.make_spawn_monitor(module, function, params, env)
485-
end
486-
487-
defp do_translate({:send, _, [id, msg]}, env) do
488-
js = Spawn.call_processes_func("send", [translate!(id, env), translate!(msg, env)])
489-
{js, env}
490-
end
491-
492-
defp do_translate({:send, _, [id, msg, _]}, env) do
493-
js = Spawn.call_processes_func("send", [translate!(id, env), translate!(msg, env)])
494-
{js, env}
495-
end
496-
497-
defp do_translate({:self, _, []}, env) do
498-
js = Spawn.call_processes_func("pid", [])
499-
{js, env}
500-
end
501-
502459
defp do_translate({:receive, _, [expressions] }, env) do
503460
Receive.make_receive(expressions, env)
504461
end

lib/elixir_script/translator/kernel/js.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ defmodule ElixirScript.Translator.JS do
4545
)
4646
end
4747

48-
defp do_translate({:yield_all, _, [term]}, env) do
48+
defp do_translate({:yield_to, _, [term]}, env) do
4949
Builder.yield_expression(
5050
Translator.translate!(term, env),
5151
true

lib/elixir_script/translator/kernel/spawn.ex

Lines changed: 0 additions & 93 deletions
This file was deleted.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ defmodule ElixirScript.Translator.Call do
55
alias ElixirScript.Translator.Utils
66
alias ElixirScript.Translator.Identifier
77

8+
def make_module_name(module_name, env) do
9+
the_name = get_module_name_for_function(module_name, env)
10+
{ make_module_expression_tree(the_name, false, env), env }
11+
end
12+
813

914
def make_function_or_property_call(module_name, function_name, env) do
1015
the_name = get_module_name_for_function(module_name, env)

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ defmodule ElixirScript.Translator.Receive do
33

44
alias ESTree.Tools.Builder, as: JS
55
alias ElixirScript.Translator
6-
alias ElixirScript.Translator.Spawn
76
alias ElixirScript.Translator.Primitive
87
alias ElixirScript.Translator.LexicalScope
98

109
def make_receive([do: clauses], env) do
1110
{made_case, _} = ElixirScript.Translator.Case.make_case({:__aliases__, [], [:message]}, clauses, env)
1211

13-
js = Spawn.call_processes_func("receive", [
12+
js = call_processes_func("receive", [
1413
JS.function_expression(
1514
[JS.identifier(:message)],
1615
[],
@@ -30,7 +29,7 @@ defmodule ElixirScript.Translator.Receive do
3029
{anon_func, _} = ElixirScript.Translator.Function.make_anonymous_function(after_clause, env)
3130

3231

33-
js = Spawn.call_processes_func("receive", [
32+
js = call_processes_func("receive", [
3433
JS.function_expression(
3534
[JS.identifier(:message)],
3635
[],
@@ -47,4 +46,20 @@ defmodule ElixirScript.Translator.Receive do
4746
{js, env}
4847
end
4948

49+
def call_processes_func(func_name, params) do
50+
JS.call_expression(
51+
JS.member_expression(
52+
JS.member_expression(
53+
JS.identifier("Elixir"),
54+
JS.member_expression(
55+
JS.identifier("Core"),
56+
JS.identifier("processes")
57+
)
58+
),
59+
JS.identifier(func_name)
60+
),
61+
params
62+
)
63+
end
64+
5065
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ElixirScript.Mixfile do
44
def project do
55
[
66
app: :elixir_script,
7-
version: "0.21.0",
7+
version: "0.22.0-dev",
88
elixir: "~> 1.0",
99
escript: escript_config,
1010
deps: deps,

src/javascript/lib/core/functions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function call_property(item, property){
2929
}
3030
}
3131

32+
function is_generator(fun){
33+
return fun.constructor.name === "GeneratorFunction";
34+
}
35+
3236
function* run(fun, args, context = null){
3337
if(fun.constructor.name === "GeneratorFunction"){
3438
return yield* fun.apply(context, args);
@@ -439,5 +443,6 @@ export default {
439443
duplicate,
440444
mapfoldl,
441445
filtermap,
442-
maps_fold
446+
maps_fold,
447+
is_generator
443448
};

0 commit comments

Comments
 (0)