Skip to content

Commit f5abddd

Browse files
committed
Added more erlang functions. Fixed more translations found from compiling stdlib
1 parent 165ece6 commit f5abddd

8 files changed

Lines changed: 122 additions & 11 deletions

File tree

lib/elixir_script/translator.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,17 @@ defmodule ElixirScript.Translator do
469469
translate(quoted, env)
470470
end
471471

472-
defp do_translate({:raise, _, [alias_info, {:<<>>, _, _} = message]}, env) do
472+
defp do_translate({:raise, _, [alias_info, attributes]}, env) when is_list(attributes) do
473473
js_ast = JS.throw_statement(
474-
Struct.new_struct(alias_info, {:%{}, [], [message: message] }, env)
474+
Struct.new_struct(alias_info, {:%{}, [], attributes }, env)
475475
)
476476

477477
{ js_ast, env }
478478
end
479479

480-
defp do_translate({:raise, _, [alias_info, attributes]}, env) do
480+
defp do_translate({:raise, _, [alias_info, message]}, env) do
481481
js_ast = JS.throw_statement(
482-
Struct.new_struct(alias_info, {:%{}, [], attributes }, env)
482+
Struct.new_struct(alias_info, {:%{}, [], [message: message] }, env)
483483
)
484484

485485
{ js_ast, env }

lib/elixir_script/translator/bitstring.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ defmodule ElixirScript.Translator.Bitstring do
5353
do_make_bitstring_element({:size, Translator.translate!(element, env), [Translator.translate!(size, env)]})
5454
end
5555

56+
defp make_bitstring_element(element, env) do
57+
do_make_bitstring_element({:binary, Translator.translate!(element, env)})
58+
end
59+
60+
5661
defp handle_type_adjectives({:-, _, types}, ast, env) do
5762
Enum.reduce(types, ast, fn(type, current_ast) ->
5863
case type do

lib/elixir_script/translator/module_collector.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ defmodule ElixirScript.Translator.ModuleCollector do
193193

194194
add_function_to_map(state, type, name, arity)
195195

196+
({type, _, [{name, _, nil}, [do: _body]]}, state) when type in [:defmacro, :defmacrop] ->
197+
add_function_to_map(state, type, name, 0)
198+
199+
196200
({type, _, [{name, _, params}, [do: _body]]}, state) when type in [:defmacro, :defmacrop] ->
197201
arity = length(params)
198202

lib/elixir_script/translator/rewriter.ex

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ defmodule ElixirScript.Translator.Rewriter do
407407
quote do: Elixir.Core.Functions.filtermap(unquote(fun), unquote(list))
408408
end
409409

410+
def rewrite({{:., _, [:lists, :concat]}, _, [things]}) do
411+
quote do: unquote(things).join("")
412+
end
413+
414+
def rewrite({{:., _, [:erlang, :binary_to_atom]}, _, [binary, _]}) do
415+
quote do: Symbol.for(unquote(binary))
416+
end
417+
418+
def rewrite({{:., _, [:erlang, :binary_to_existing_atom]}, _, [binary, _]}) do
419+
quote do: Symbol.for(unquote(binary))
420+
end
421+
410422
def rewrite({{:., _, [:erlang, :list_to_atom]}, _, [char_list]}) do
411423
quote do: Symbol.for(unquote(char_list))
412424
end
@@ -431,6 +443,50 @@ defmodule ElixirScript.Translator.Rewriter do
431443
quote do: parseInt(unquote(list), unquote(base))
432444
end
433445

446+
def rewrite({{:., _, [:erlang, :integer_to_binary]}, _, [integer]}) do
447+
quote do: unquote(integer).toString()
448+
end
449+
450+
def rewrite({{:., _, [:erlang, :integer_to_binary]}, _, [integer, base]}) do
451+
quote do: unquote(integer).toString(unquote(base))
452+
end
453+
454+
def rewrite({{:., _, [:erlang, :integer_to_list]}, _, [integer]}) do
455+
quote do: unquote(integer).toString()
456+
end
457+
458+
def rewrite({{:., _, [:erlang, :integer_to_list]}, _, [integer, base]}) do
459+
quote do: unquote(integer).toString(unquote(base))
460+
end
461+
462+
def rewrite({{:., _, [:erlang, :float_to_binary]}, _, [float]}) do
463+
quote do: unquote(float).toString()
464+
end
465+
466+
def rewrite({{:., _, [:erlang, :float_to_binary]}, _, [float, base]}) do
467+
quote do: unquote(float).toString(unquote(base))
468+
end
469+
470+
def rewrite({{:., _, [:erlang, :float_to_list]}, _, [float]}) do
471+
quote do: unquote(float).toString()
472+
end
473+
474+
def rewrite({{:., _, [:erlang, :float_to_list]}, _, [float, base]}) do
475+
quote do: unquote(float).toString(unquote(base))
476+
end
477+
478+
def rewrite({{:., _, [:erlang, :binary_to_float]}, _, [binary]}) do
479+
quote do: parseFloat(unquote(binary))
480+
end
481+
482+
def rewrite({{:., _, [:erlang, :binary_to_integer]}, _, [binary]}) do
483+
quote do: parseInt(unquote(binary))
484+
end
485+
486+
def rewrite({{:., _, [:erlang, :binary_to_integer]}, _, [binary, base]}) do
487+
quote do: parseInt(unquote(binary), unquote(base))
488+
end
489+
434490
def rewrite({{:., _, [:maps, :is_key]}, _, [key, map]}) do
435491
quote do: unquote(key) in Elixir.Core.Functions.get_object_keys(unquote(map))
436492
end
@@ -447,5 +503,16 @@ defmodule ElixirScript.Translator.Rewriter do
447503
quote do: Elixir.Core.Functions.maps_find(unquote(key), unquote(map))
448504
end
449505

506+
def rewrite({{:., _, [:maps, :remove]}, _, [key, map]}) do
507+
quote do: Elixir.Core.Functions.delete_property_from_map(unquote(map), unquote(key))
508+
end
509+
510+
def rewrite({{:., _, [:maps, :fold]}, _, [fun, init, map]}) do
511+
quote do: Elixir.Core.Functions.maps_fold(unquote(fun), unquote(init), unquote(map))
512+
end
513+
514+
def rewrite({{:., _, [:maps, :from_list]}, _, [list]}) do
515+
quote do: Elixir.Core.Functions.maps_fold(unquote(list))
516+
end
450517

451518
end

src/javascript/lib/core/functions.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,26 @@ function filtermap(fun, list){
364364
return Object.freeze(newlist);
365365
}
366366

367+
function maps_fold(fun, acc, map){
368+
let acc1 = acc;
369+
370+
for(let k of get_object_keys(map)){
371+
acc1 = fun(k, map[k], acc1);
372+
}
373+
374+
return acc1;
375+
}
376+
377+
function maps_from_list(list){
378+
let m = {};
379+
380+
for(x of list){
381+
m[x.get(0)] = x.get(1);
382+
}
383+
384+
return Object.freeze(m);
385+
}
386+
367387
export default {
368388
call_property,
369389
apply,
@@ -401,5 +421,6 @@ export default {
401421
flatten,
402422
duplicate,
403423
mapfoldl,
404-
filtermap
424+
filtermap,
425+
maps_fold
405426
};

test/translator/assignment_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ defmodule ElixirScript.Translator.Assignment.Test do
7171
test "translate head/tail assignment" do
7272
ex_ast = quote do: [a | b] = [1, 2, 3, 4]
7373
js_code = """
74-
let [a,b] = Elixir.Core.Patterns.match(Elixir.Core.Patterns.headTail(),Object.freeze([1, 2, 3, 4]));
75-
let _ref = Object.freeze([a, b]);
74+
let [a,b] = Elixir.Core.Patterns.match(Elixir.Core.Patterns.headTail(Elixir.Core.Patterns.variable(),Elixir.Core.Patterns.variable()),Object.freeze([1, 2, 3, 4]));
75+
let _ref = Object.freeze([a, b]);
7676
"""
7777

7878
assert_translation(ex_ast, js_code)

test/translator/function_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,9 @@ defmodule ElixirScript.Translator.Function.Test do
463463

464464

465465
js_code = """
466-
const something = Elixir.Core.Patterns.defmatch(Elixir.Core.Patterns.make_case([Elixir.Core.Patterns.headTail()],function(apple,fruits) {
467-
return null;
468-
}));
466+
const something = Elixir.Core.Patterns.defmatch(Elixir.Core.Patterns.make_case([Elixir.Core.Patterns.headTail(Elixir.Core.Patterns.variable(),Elixir.Core.Patterns.variable())],function(apple,fruits) {
467+
return null;
468+
}));
469469
"""
470470

471471
assert_translation(ex_ast, js_code)

test/translator/kernel_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ElixirScript.Translator.Kernel.Test do
22
use ExUnit.Case
33
import ElixirScript.TestHelper
44

5-
test "raise" do
5+
test "raise with bitstring" do
66
ex_ast = quote do
77
raise ArgumentError, "cannot convert list to string. The list must contain only integers, strings or nested such lists; got: #{inspect list}"
88
end
@@ -16,6 +16,20 @@ defmodule ElixirScript.Translator.Kernel.Test do
1616
assert_translation(ex_ast, js_code)
1717
end
1818

19+
test "raise with string" do
20+
ex_ast = quote do
21+
raise ArgumentError, "cannot convert list to string. The list must contain only integers, strings or nested such lists; got"
22+
end
23+
24+
js_code = """
25+
throw ArgumentError.create(Object.freeze({
26+
[Symbol.for('message')]: 'cannot convert list to string. The list must contain only integers, strings or nested such lists; got'
27+
}));
28+
"""
29+
30+
assert_translation(ex_ast, js_code)
31+
end
32+
1933
test "max" do
2034
ex_ast = quote do
2135
max(1, 2)

0 commit comments

Comments
 (0)