Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/elixir_script/module_systems/es.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ defmodule ElixirScript.ModuleSystems.ES do
imports ++ body ++ export
end

def build_imports(js_imports) do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions should have a @SPEC type specification.

js_imports
|> Enum.map(fn
{_module, name, _path, import_path} -> import_module(name, import_path)
end)
end

def build_export(exports) do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions should have a @SPEC type specification.

if is_nil(exports), do: [], else: [export_module(exports)]
end

defp import_module(import_name, from) do
js_module_name = JS.identifier(import_name)

Expand Down
11 changes: 8 additions & 3 deletions lib/elixir_script/passes/find_used_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ defmodule ElixirScript.FindUsedFunctions do
"""
@spec execute([atom], pid) :: nil
def execute(entry_modules, pid) do
Enum.each(entry_modules, fn
entry_modules
|> List.wrap
|> Task.async_stream(fn
module ->
walk_module(module, pid)
end)
|> Stream.run()

modules = ElixirScript.State.list_modules(pid)
Enum.each(modules, fn
pid
|> ElixirScript.State.list_modules
|> Task.async_stream(fn
{module, info} ->
if get_in(info, [:attributes, :protocol_impl]) do
walk_module(module, pid)
end
end)
|> Stream.run()
end

defp walk_module(module, pid) do
Expand Down
10 changes: 6 additions & 4 deletions lib/elixir_script/passes/find_used_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ defmodule ElixirScript.FindUsedModules do
"""
@spec execute([atom], pid) :: nil
def execute(modules, pid) do
Enum.each(List.wrap(modules), fn(module) ->
modules
|> List.wrap
|> Task.async_stream(fn(module) ->
if ElixirScript.State.get_module(pid, module) == nil do
do_execute(module, pid)
end
end)
|> Stream.run()
end

defp do_execute(module, pid) do
Expand Down Expand Up @@ -72,7 +75,6 @@ defmodule ElixirScript.FindUsedModules do
}

Enum.each(reachable_defs, &walk(&1, state))

end

defp walk_protocol(module, implementations, pid) do
Expand Down Expand Up @@ -128,7 +130,7 @@ defmodule ElixirScript.FindUsedModules do
defp walk(form, state) when is_atom(form) and form not in [BitString, Function, PID, Port, Reference, Any, Elixir] do
if ElixirScript.Translate.Module.is_elixir_module(form) and !ElixirScript.Translate.Module.is_js_module(form, state) do
if ModuleState.get_module(state.pid, form) == nil do
execute(form, state.pid)
do_execute(form, state.pid)
end
end
end
Expand Down Expand Up @@ -292,7 +294,7 @@ defmodule ElixirScript.FindUsedModules do
cond do
ElixirScript.Translate.Module.is_elixir_module(module) ->
if ModuleState.get_module(state.pid, module) == nil do
execute(module, state.pid)
do_execute(module, state.pid)
end
true ->
walk(module, state)
Expand Down
14 changes: 10 additions & 4 deletions lib/elixir_script/passes/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ defmodule ElixirScript.Output do
defp bundle(modules, opts, js_modules) do
modules
|> ElixirScript.Output.JSModule.compile(opts, js_modules)
|> List.wrap
|> Builder.program
|> prepare_js_ast
|> Generator.generate
|> Task.async_stream(fn(js_part) ->
js_part
|> List.wrap
|> Builder.program
|> prepare_js_ast
|> Generator.generate
end)
|> Stream.map(fn {:ok, js_code} -> js_code end)
|> Enum.to_list
|> Enum.join("\n")
|> concat
end

Expand Down
9 changes: 3 additions & 6 deletions lib/elixir_script/passes/output/js_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ defmodule ElixirScript.Output.JSModule do
def compile(body, opts, js_modules) do
elixir = Helpers.declare("Elixir", J.object_expression([]))

ast = opts.module_formatter.build(
js_modules,
[elixir, create_atom_table(), start(), load()] ++ body,
J.identifier("Elixir")
)
imports = opts.module_formatter.build_imports(js_modules)
exports = opts.module_formatter.build_export(J.identifier("Elixir"))

ast
[elixir, create_atom_table(), start(), load(), imports, body, exports]
end

def start do
Expand Down
5 changes: 4 additions & 1 deletion lib/elixir_script/passes/translate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ defmodule ElixirScript.Translate do
"""
@spec execute([atom], pid) :: nil
def execute(modules, pid) do
Enum.each(modules, fn
modules
|> List.wrap()
|> Task.async_stream(fn
{module, info} ->
ElixirScript.Translate.Module.compile(module, info, pid)
end)
|> Stream.run()
end
end
12 changes: 9 additions & 3 deletions lib/elixir_script/passes/translate/identifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ defmodule ElixirScript.Translate.Identifier do
end

defp filter_name(name) do
to_string(name)
|> String.replace("?", "__qmark__")
|> String.replace("!", "__emark__")
name = to_string(name)

if String.contains?(name, ["?", "!"]) do
name
|> String.replace("?", "__qmark__")
|> String.replace("!", "__emark__")
else
name
end
end

defp make_alias([x]) do
Expand Down
7 changes: 4 additions & 3 deletions lib/elixir_script/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule ElixirScript.State do
def put_module(pid, module, value) do
Agent.update(pid, fn(state) ->
value = Map.put(value, :used, [])
%{ state | modules: Keyword.put(state.modules, module, value) }
modules = Keyword.put(state.modules, module, value)
%{ state | modules: modules }
end)
end

Expand All @@ -41,7 +42,7 @@ defmodule ElixirScript.State do
module_info = Keyword.get(state.modules, module)

used = Map.get(module_info, :used, [])
used = used ++ [func]
used = [func | used]

module_info = Map.put(module_info, :used, used)
modules = Keyword.put(state.modules, module, module_info)
Expand All @@ -53,7 +54,7 @@ defmodule ElixirScript.State do
def put_javascript_module(pid, module, name, path) do
Agent.update(pid, fn(state) ->
js_modules = Map.get(state, :js_modules, [])
js_modules = js_modules ++ [{module, name, path}]
js_modules = [{module, name, path} | js_modules]
%{ state | js_modules: js_modules }
end)
end
Expand Down