Skip to content

Commit ec2e5ab

Browse files
committed
Reducing javascript functions
1 parent b2b5da7 commit ec2e5ab

File tree

12 files changed

+151
-332
lines changed

12 files changed

+151
-332
lines changed

lib/elixir_script/translator/rewriter.ex

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirScript.Translator.Rewriter do
2-
@moduledoc false
2+
@moduledoc false
33

44
# :erlang, :lists, :maps, :beam_lib, :binary, :calendar, :digraph,
55
# :epp, :erl_lint, :erl_internal, :erl_expand_records, :erl_eval,
@@ -331,35 +331,27 @@ defmodule ElixirScript.Translator.Rewriter do
331331
end
332332

333333
def rewrite({{:., _, [:lists, :foldl]}, _, [fun, acc, list]}) do
334-
quote do: Bootstrap.Core.Functions.foldl(unquote(fun), unquote(acc), unquote(list))
334+
quote do: List.foldl(unquote(fun), unquote(acc), unquote(list))
335335
end
336336

337337
def rewrite({{:., _, [:lists, :foldr]}, _, [fun, acc, list]}) do
338-
quote do: Bootstrap.Core.Functions.foldr(unquote(fun), unquote(acc), unquote(list))
338+
quote do: List.foldr(unquote(fun), unquote(acc), unquote(list))
339339
end
340340

341341
def rewrite({{:., _, [:lists, :keymember]}, _, [key, n, list]}) do
342-
quote do: Bootstrap.Core.Functions.keymember(unquote(key), unquote(n), unquote(list))
342+
quote do: List.keymember?(unquote(list), unquote(key), unquote(n))
343343
end
344344

345345
def rewrite({{:., _, [:lists, :keydelete]}, _, [key, n, list]}) do
346-
quote do: Bootstrap.Core.Functions.keydelete(unquote(key), unquote(n), unquote(list))
347-
end
348-
349-
def rewrite({{:., _, [:lists, :keystore]}, _, [key, n, list, newtuple]}) do
350-
quote do: Bootstrap.Core.Functions.keystore(unquote(key), unquote(n), unquote(list), unquote(newtuple))
351-
end
352-
353-
def rewrite({{:., _, [:lists, :keytake]}, _, [key, n, list]}) do
354-
quote do: Bootstrap.Core.Functions.keystore(unquote(key), unquote(n), unquote(list))
346+
quote do: List.keydelete(unquote(list), unquote(key), unquote(n))
355347
end
356348

357349
def rewrite({{:., _, [:lists, :keyfind]}, _, [key, n, list]}) do
358-
quote do: Bootstrap.Core.Functions.keyfind(unquote(key), unquote(n), unquote(list))
350+
quote do: List.keyfind(unquote(list), unquote(key), unquote(n))
359351
end
360352

361353
def rewrite({{:., _, [:lists, :keyreplace]}, _, [key, n, list, newtuple]}) do
362-
quote do: Bootstrap.Core.Functions.keyreplace(unquote(key), unquote(n), unquote(list), unquote(newtuple))
354+
quote do: List.keyreplace(unquote(list), unquote(key), unquote(n), unquote(newtuple))
363355
end
364356

365357
def rewrite({{:., _, [:lists, :keysort]}, _, [n, tuplelist]}) do
@@ -368,27 +360,27 @@ defmodule ElixirScript.Translator.Rewriter do
368360
end
369361

370362
def rewrite({{:., _, [:lists, :reverse]}, _, [list]}) do
371-
quote do: Bootstrap.Core.Functions.reverse(unquote(list))
363+
quote do: ElixirScript.Bootstrap.Functions.reverse(unquote(list))
372364
end
373365

374366
def rewrite({{:., _, [:lists, :reverse]}, _, [list, tail]}) do
375-
quote do: Bootstrap.Core.Functions.reverse(unquote(list)) ++ unquote(tail)
367+
quote do: ElixirScript.Bootstrap.Functions.reverse(unquote(list)) ++ unquote(tail)
376368
end
377369

378370
def rewrite({{:., _, [:lists, :flatten]}, _, [list]}) do
379-
quote do: Bootstrap.Core.Functions.flatten(unquote(list))
371+
quote do: List.flatten(unquote(list))
380372
end
381373

382374
def rewrite({{:., _, [:lists, :flatten]}, _, [list, tail]}) do
383-
quote do: Bootstrap.Core.Functions.flatten(unquote(list), unquote(tail))
375+
quote do: List.flatten(unquote(list), unquote(tail))
384376
end
385377

386378
def rewrite({{:., _, [:lists, :delete]}, _, [elem, list]}) do
387-
quote do: Bootstrap.Core.Functions.remove_from_list(unquote(list), unquote(elem))
379+
quote do: List.delete(unquote(list), unquote(elem))
388380
end
389381

390382
def rewrite({{:., _, [:lists, :duplicate]}, _, [n, elem]}) do
391-
quote do: Bootstrap.Core.Functions.duplicate(unquote(n), unquote(elem))
383+
quote do: List.duplicate(unquote(n), unquote(elem))
392384
end
393385

394386
def rewrite({{:., _, [:lists, :mapfoldl]}, _, [fun, acc, list]}) do
@@ -437,15 +429,15 @@ defmodule ElixirScript.Translator.Rewriter do
437429
end
438430

439431
def rewrite({{:., _, [:erlang, :list_to_float]}, _, [list]}) do
440-
quote do: parseFloat(unquote(list))
432+
quote do: JS.parseFloat(unquote(list))
441433
end
442434

443435
def rewrite({{:., _, [:erlang, :list_to_integer]}, _, [list]}) do
444-
quote do: parseInt(unquote(list))
436+
quote do: JS.parseInt(unquote(list))
445437
end
446438

447439
def rewrite({{:., _, [:erlang, :list_to_integer]}, _, [list, base]}) do
448-
quote do: parseInt(unquote(list), unquote(base))
440+
quote do: JS.parseInt(unquote(list), unquote(base))
449441
end
450442

451443
def rewrite({{:., _, [:erlang, :integer_to_binary]}, _, [integer]}) do
@@ -481,43 +473,31 @@ defmodule ElixirScript.Translator.Rewriter do
481473
end
482474

483475
def rewrite({{:., _, [:erlang, :binary_to_float]}, _, [binary]}) do
484-
quote do: parseFloat(unquote(binary))
476+
quote do: JS.parseFloat(unquote(binary))
485477
end
486478

487479
def rewrite({{:., _, [:erlang, :binary_to_integer]}, _, [binary]}) do
488-
quote do: parseInt(unquote(binary))
480+
quote do: JS.parseInt(unquote(binary))
489481
end
490482

491483
def rewrite({{:., _, [:erlang, :binary_to_integer]}, _, [binary, base]}) do
492-
quote do: parseInt(unquote(binary), unquote(base))
484+
quote do: JS.parseInt(unquote(binary), unquote(base))
493485
end
494486

495487
def rewrite({{:., _, [:maps, :is_key]}, _, [key, map]}) do
496-
quote do: unquote(key) in Bootstrap.Core.Functions.get_object_keys(unquote(map))
488+
quote do: unquote(key) in ElixirScript.Bootstrap.Functions.get_object_keys(unquote(map))
497489
end
498490

499491
def rewrite({{:., _, [:maps, :put]}, _, [key, value, map]}) do
500492
quote do: Bootstrap.Core.Functions.add_property_to_map(unquote(map), unquote(key), unquote(value))
501493
end
502494

503495
def rewrite({{:., _, [:maps, :update]}, _, [key, value, map]}) do
504-
quote do: Bootstrap.Core.Functions.update_map(unquote(map), unquote(key), unquote(value))
505-
end
506-
507-
def rewrite({{:., _, [:maps, :find]}, _, [key, map]}) do
508-
quote do: Bootstrap.Core.Functions.maps_find(unquote(key), unquote(map))
496+
quote do: Map.update!(unquote(map), unquote(key), fn(_) -> unquote(value) end)
509497
end
510498

511499
def rewrite({{:., _, [:maps, :remove]}, _, [key, map]}) do
512500
quote do: Bootstrap.Core.Functions.delete_property_from_map(unquote(map), unquote(key))
513501
end
514502

515-
def rewrite({{:., _, [:maps, :fold]}, _, [fun, init, map]}) do
516-
quote do: Bootstrap.Core.Functions.maps_fold(unquote(fun), unquote(init), unquote(map))
517-
end
518-
519-
def rewrite({{:., _, [:maps, :from_list]}, _, [list]}) do
520-
quote do: Bootstrap.Core.Functions.maps_fold(unquote(list))
521-
end
522-
523503
end

priv/std_lib/agent.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule ElixirScript.Agent do
1111
nil
1212
end
1313

14-
Bootstrap.Core.Store.create(pid, fun.(), name)
14+
ElixirScript.Store.create(pid, fun.(), name)
1515
{ :ok, pid }
1616
end
1717

@@ -24,30 +24,30 @@ defmodule ElixirScript.Agent do
2424
nil
2525
end
2626

27-
Bootstrap.Core.Store.create(pid, fun.(), name)
27+
ElixirScript.Store.create(pid, fun.(), name)
2828
{ :ok, pid }
2929
end
3030

3131
def stop(agent) do
32-
Bootstrap.Core.Store.remove(agent)
32+
ElixirScript.Store.remove(agent)
3333
:ok
3434
end
3535

3636
def update(agent, fun) do
37-
current_state = Bootstrap.Core.Store.read(agent)
38-
Bootstrap.Core.Store.update(agent, fun.(current_state))
37+
current_state = ElixirScript.Store.read(agent)
38+
ElixirScript.Store.update(agent, fun.(current_state))
3939
:ok
4040
end
4141

4242
def get(agent, fun) do
43-
current_state = Bootstrap.Core.Store.read(agent)
43+
current_state = ElixirScript.Store.read(agent)
4444
fun.(current_state)
4545
end
4646

4747
def get_and_update(agent, fun) do
48-
current_state = Bootstrap.Core.Store.read(agent)
48+
current_state = ElixirScript.Store.read(agent)
4949
{val, new_state} = fun.(current_state)
50-
Bootstrap.Core.Store.update(agent, new_state)
50+
ElixirScript.Store.update(agent, new_state)
5151
val
5252
end
5353

priv/std_lib/base.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ defmodule ElixirScript.Base do
22
@moduledoc false
33

44
def encode64(data) do
5-
Bootstrap.Core.b64EncodeUnicode(data)
5+
ElixirScript.Bootstrap.b64EncodeUnicode(data)
66
end
77

88
def decode64(data) do
9-
if Bootstrap.Core.can_decode64(data) do
9+
if ElixirScript.Bootstrap.can_decode64(data) do
1010
{:ok, decode64!(data) }
1111
else
1212
:error
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
defmodule ElixirScript.Bootstrap.Functions do
2+
3+
def contains(left, []) do
4+
false
5+
end
6+
7+
def contains(left, [right]) do
8+
match?(left, right)
9+
end
10+
11+
def contains(left, [h|t]) do
12+
case match?(left, h) do
13+
true ->
14+
true
15+
false ->
16+
contains(left, t)
17+
end
18+
end
19+
20+
def get_object_keys(obj) do
21+
JS.Object.keys(obj).concat(JS.Object.getOwnPropertySymbols(obj))
22+
end
23+
24+
def is_valid_character(codepoint) do
25+
try do
26+
JS.String.fromCodePoint(codepoint) != nil
27+
rescue
28+
_ ->
29+
false
30+
end
31+
end
32+
33+
def b64EncodeUnicode(str) do
34+
{:ok, regex} = Regex.compile("%([0-9A-F]{2})", "g")
35+
36+
JS.btoa(
37+
JS.encodeURIComponent(str).replace(regex, fn (match, p1) ->
38+
JS.String.fromCharCode("0x#{p1}")
39+
end)
40+
)
41+
end
42+
43+
def can_decode64(data) do
44+
try do
45+
JS.atob(data)
46+
true
47+
rescue
48+
_ ->
49+
false
50+
end
51+
end
52+
53+
def reverse(list) do
54+
list.concat([]).reverse()
55+
end
56+
57+
end

priv/std_lib/kernel.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ defmodule ElixirScript.Kernel do
4444
end
4545

4646
def apply(fun, args) do
47-
Bootstrap.Core.Functions.apply(fun, args)
47+
fun.apply(fun, args)
4848
end
4949

5050
def apply(module, fun, args) do
51-
Bootstrap.Core.Functions.apply(module, Atom.to_string(fun), args)
51+
module[Atom.to_string(fun)].apply(module[Atom.to_string(fun)], args)
5252
end
5353

5454
def binary_part(binary, start, len) do
@@ -80,7 +80,7 @@ defmodule ElixirScript.Kernel do
8080
end
8181

8282
def is_float(term) do
83-
is_number(term) && !Number.isInteger(term)
83+
is_number(term) && !JS.Number.isInteger(term)
8484
end
8585

8686
def is_function(term) do
@@ -92,11 +92,11 @@ defmodule ElixirScript.Kernel do
9292
end
9393

9494
def is_integer(term) do
95-
Number.isInteger(term)
95+
JS.Number.isInteger(term)
9696
end
9797

9898
def is_list(term) do
99-
Array.isArray(term)
99+
JS.Array.isArray(term)
100100
end
101101

102102
def is_number(term) do
@@ -148,11 +148,11 @@ defmodule ElixirScript.Kernel do
148148
end
149149

150150
def tuple_size(tuple) do
151-
Bootstrap.Core.Functions.size(tuple)
151+
tuple.count()
152152
end
153153

154154
def elem(tuple, index) do
155-
Bootstrap.Core.Functions.apply(tuple, "get", [index])
155+
apply(tuple, :get, [index])
156156
end
157157

158158
def is_nil(term) do
@@ -193,7 +193,7 @@ defmodule ElixirScript.Kernel do
193193

194194
defmacro left in right do
195195
quote do
196-
Bootstrap.Core.Functions.contains(unquote(left), unquote(right))
196+
ElixirScript.Bootstrap.Functions.contains(unquote(left), unquote(right))
197197
end
198198
end
199199

priv/std_lib/list.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ defmodule ElixirScript.List do
156156
do_foldl(tl(list), acc, func, new_list ++ [value])
157157
end
158158

159-
def flatten(list) do
160-
do_flatten(list, [])
161-
end
162-
163-
def flatten(list, tail) do
159+
def flatten(list, tail \\ []) do
164160
do_flatten(list, []) ++ tail
165161
end
166162

priv/std_lib/map.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule ElixirScript.Map do
66
end
77

88
def keys(map) do
9-
Bootstrap.Core.Functions.get_object_keys(map)
9+
ElixirScript.Bootstrap.Functions.get_object_keys(map)
1010
end
1111

1212
def size(map) do

priv/std_lib/store.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule ElixirScript.Store do
2+
3+
defp get_key(key) do
4+
5+
real_key = case JS.__elixirscript_names__.has(key) do
6+
true ->
7+
JS.__elixirscript_names__.get(key)
8+
false ->
9+
key
10+
end
11+
12+
13+
case JS.__elixirscript_store__.has(real_key) do
14+
true ->
15+
real_key
16+
false ->
17+
JS.throw JS.new(JS.Error("Key Not Found"))
18+
end
19+
end
20+
21+
def create(key, value, name \\ nil) do
22+
if name != nil do
23+
JS.__elixirscript_names__.set(name, key)
24+
end
25+
26+
JS.__elixirscript_store__.set(key, value)
27+
end
28+
29+
def update(key, value) do
30+
real_key = get_key(key)
31+
JS.__elixirscript_store__.set(real_key, value)
32+
end
33+
34+
def read(key) do
35+
real_key = get_key(key)
36+
JS.__elixirscript_store__.get(real_key)
37+
end
38+
39+
def remove(key) do
40+
real_key = get_key(key)
41+
JS.__elixirscript_store__.delete(real_key)
42+
end
43+
44+
45+
end

0 commit comments

Comments
 (0)