Skip to content

Commit 4da3f46

Browse files
committed
Update String module debug_info code.
Get debug_info from String module, but then replace functions found in ElixirScript.String. This is so we don't have to reimplement the entire String module.
1 parent 0edd518 commit 4da3f46

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
erlang 20.0
2-
elixir ref-v1.5.0-rc.2
2+
elixir 1.5.0-otp-20
33
nodejs 8.2.0

lib/elixir_script/beam.ex

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ defmodule ElixirScript.Beam do
1010
@spec debug_info(atom) :: {:ok | :error, map | binary}
1111
def debug_info(module)
1212

13-
#Replace some modules with ElixirScript versions
14-
def debug_info(module) when module in [String, Agent] do
15-
case debug_info(Module.concat(ElixirScript, module)) do
13+
# We get debug info from String and then replace
14+
# functions in it with equivalents in ElixirScript.String.
15+
# This is so that we don't include the unicode database
16+
# in our output
17+
def debug_info(String) do
18+
{:ok, info} = do_debug_info(String)
19+
{:ok, ex_string_info} = do_debug_info(ElixirScript.String)
20+
21+
definitions = replace_definitions(info.definitions, ex_string_info.definitions)
22+
23+
info = %{info | definitions: definitions}
24+
25+
{:ok, info}
26+
end
27+
28+
# Replace some modules with ElixirScript versions
29+
def debug_info(module) when module in [Agent] do
30+
case do_debug_info(Module.concat(ElixirScript, module)) do
1631
{:ok, info} ->
1732
{:ok, Map.put(info, :module, module)}
1833
e ->
@@ -21,6 +36,10 @@ defmodule ElixirScript.Beam do
2136
end
2237

2338
def debug_info(module) when is_atom(module) do
39+
do_debug_info(module)
40+
end
41+
42+
defp do_debug_info(module) when is_atom(module) do
2443
#TODO: Get modified date from _beam_path to check for cached version?
2544
with {_, beam, _beam_path} <- :code.get_object_code(module),
2645
{:ok, {^module, [debug_info: {:debug_info_v1, backend, data}]}} <- :beam_lib.chunks(beam, [:debug_info]),
@@ -59,4 +78,21 @@ defmodule ElixirScript.Beam do
5978
{:ok, module, implementations}
6079
end
6180

62-
end
81+
defp replace_definitions(original_definitions, replacement_definitions) do
82+
Enum.map(original_definitions, fn
83+
{{function, arity}, type, _, _} = ast ->
84+
ex_ast = Enum.find(replacement_definitions, fn
85+
{{ex_function, ex_arity}, ex_type, _, _} ->
86+
ex_function == function and ex_arity == arity and ex_type == type
87+
end)
88+
89+
case ex_ast do
90+
nil ->
91+
ast
92+
_ ->
93+
ex_ast
94+
end
95+
end)
96+
end
97+
98+
end

lib/elixir_script/passes/translate/forms/remote.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ defmodule ElixirScript.Translate.Forms.Remote do
1717
:elixir_utils,
1818
:file,
1919
:io,
20-
:binary
20+
:binary,
21+
:unicode
2122
]
2223

2324
@doc """

src/javascript/lib/core.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import lists from './core/erlang_compat/lists';
88
import elixir_errors from './core/erlang_compat/elixir_errors';
99
import io from './core/erlang_compat/io';
1010
import binary from './core/erlang_compat/binary';
11+
import unicode from './core/erlang_compat/unicode';
1112
import Store from './core/store';
1213

1314
class Integer {}
@@ -47,5 +48,6 @@ export default {
4748
lists,
4849
elixir_errors,
4950
io,
50-
binary
51+
binary,
52+
unicode
5153
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import erlang from './erlang';
2+
3+
function characters_to_list(characters) {
4+
return characters.split('').map(c => c.codePointAt(0));
5+
}
6+
7+
function characters_to_binary(characters) {
8+
if (erlang.is_binary(characters)) {
9+
return characters;
10+
}
11+
12+
return String.fromCodePoint(...characters);
13+
}
14+
15+
export default {
16+
characters_to_list,
17+
characters_to_binary
18+
};

test/support/main.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule Main do
22
def start(:normal, [callback]) do
33
callback.("started")
4+
5+
String.upcase("d")
46
end
57
end

0 commit comments

Comments
 (0)