Skip to content

Commit b812fea

Browse files
committed
Preparing for bundled output
1 parent d5eda1c commit b812fea

7 files changed

Lines changed: 96 additions & 175 deletions

File tree

lib/elixir_script/module_systems/common.ex

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ defmodule ElixirScript.ModuleSystems.Common do
55
alias ElixirScript.Translator.State
66
alias ElixirScript.Translator.Utils
77

8-
def build(std_import, imports, js_imports, body, exports, env) do
8+
def build(std_import, imports, js_imports, body, exports) do
99
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, path) end)
1010

1111
imports = js_imports ++ List.wrap(std_import)
1212
|> Enum.map(fn
13-
{module, path} -> import_module(module, path, env)
14-
{module, path, true} -> import_module(module, path, env)
15-
{module, path, false} -> import_namespace_module(module, path, env)
13+
{module, path} -> import_module(module, path)
14+
{module, path, true} -> import_module(module, path)
15+
{module, path, false} -> import_namespace_module(module, path)
1616
end)
1717

1818
imports = Enum.uniq(imports ++ module_imports)
@@ -21,39 +21,12 @@ defmodule ElixirScript.ModuleSystems.Common do
2121
imports ++ body ++ export
2222
end
2323

24-
defp module_imports_to_js_imports(module_refs, env) do
25-
Enum.map(module_refs, fn(x) ->
26-
module_name = Utils.name_to_js_name(x)
27-
app_name = State.get_module(env.state, x).app
28-
path = Utils.make_local_file_path(app_name, Utils.name_to_js_file_name(x), env)
29-
import_module(module_name, path)
30-
end)
31-
end
32-
33-
defp make_std_lib_import(env) do
34-
compiler_opts = State.get(env.state).compiler_opts
35-
case compiler_opts.import_standard_libs do
36-
true ->
37-
[{:Elixir, Utils.make_local_file_path(:elixir, compiler_opts.core_path, env), true }]
38-
false ->
39-
[]
40-
end
41-
end
42-
43-
def import_namespace_module(module_name, from, env) do
44-
do_import_module(Translator.translate!(module_name, env), from)
45-
end
46-
47-
def import_module(:Elixir, from, env) do
48-
do_import_module(JS.identifier("Elixir"), from)
49-
end
50-
51-
def import_module(module_name, from, env) do
52-
do_import_module(Translator.translate!(module_name, env), from)
24+
def import_namespace_module(module_name, from) do
25+
do_import_module(JS.identifier(module_name), from)
5326
end
5427

55-
def import_module(import_name, from) do
56-
do_import_module(JS.identifier(import_name), from)
28+
def import_module(module_name, from) do
29+
do_import_module(JS.identifier(module_name), from)
5730
end
5831

5932
defp do_import_module(ref, file_path) do

lib/elixir_script/module_systems/es.ex

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ defmodule ElixirScript.ModuleSystems.ES do
55
alias ElixirScript.Translator.State
66
alias ElixirScript.Translator.Utils
77

8-
def build(std_import, imports, js_imports, body, exports, env) do
8+
def build(std_import, imports, js_imports, body, exports) do
99
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, path) end)
1010

1111
imports = js_imports ++ List.wrap(std_import)
1212
|> Enum.map(fn
13-
{module, path} -> import_module(module, path, env)
14-
{module, path, true} -> import_module(module, path, env)
15-
{module, path, false} -> import_namespace_module(module, path, env)
13+
{module, path} -> import_module(module, path)
14+
{module, path, true} -> import_module(module, path)
15+
{module, path, false} -> import_namespace_module(module, path)
1616
end)
1717

1818
imports = Enum.uniq(imports ++ module_imports)
@@ -21,47 +21,10 @@ defmodule ElixirScript.ModuleSystems.ES do
2121
imports ++ body ++ export
2222
end
2323

24-
defp module_imports_to_js_imports(module_refs, env) do
25-
Enum.map(module_refs, fn(x) ->
26-
module_name = Utils.name_to_js_name(x)
27-
app_name = State.get_module(env.state, x).app
28-
path = Utils.make_local_file_path(app_name, Utils.name_to_js_file_name(x), env)
29-
import_module(module_name, path)
30-
end)
31-
end
32-
33-
defp make_std_lib_import(env) do
34-
compiler_opts = State.get(env.state).compiler_opts
35-
case compiler_opts.import_standard_libs do
36-
true ->
37-
[{:Elixir, Utils.make_local_file_path(:elixir, compiler_opts.core_path, env), true }]
38-
false ->
39-
[]
40-
end
41-
end
42-
43-
def import_namespace_module(module_name, from, env) do
24+
def import_namespace_module(module_name, from) do
4425
import_specifier = JS.import_namespace_specifier(
45-
Translator.translate!(module_name, env),
46-
Translator.translate!(module_name, env)
47-
)
48-
49-
do_import_module([import_specifier], from)
50-
end
51-
52-
def import_module(:Elixir, from, env) do
53-
import_specifier = JS.import_default_specifier(
54-
JS.identifier("Elixir"),
55-
JS.identifier("Elixir")
56-
)
57-
58-
do_import_module([import_specifier], from)
59-
end
60-
61-
def import_module(module_name, from, env) do
62-
import_specifier = JS.import_default_specifier(
63-
Translator.translate!(module_name, env),
64-
Translator.translate!(module_name, env)
26+
JS.identifier(module_name),
27+
JS.identifier(module_name)
6528
)
6629

6730
do_import_module([import_specifier], from)

lib/elixir_script/module_systems/namespace.ex

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,11 @@ defmodule ElixirScript.ModuleSystems.Namespace do
66
alias ElixirScript.Translator.Utils
77

88
def build(module_name, imports, body, exports, env) do
9-
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, env) end)
10-
export = export_module(exports)
11-
List.wrap(make_namespace_body(module_name, imports, body, export))
12-
end
13-
14-
defp module_imports_to_js_imports(module_refs, env) do
15-
Enum.map(module_refs, fn(x) ->
16-
module_name = Utils.name_to_js_name(x)
17-
app_name = State.get_module(env.state, x).app
18-
path = Utils.make_local_file_path(app_name, Utils.name_to_js_file_name(x), env)
19-
import_module(module_name, path)
9+
module_imports = Enum.map(imports, fn {module, path} ->
10+
IO.inspect module
11+
import_module(module, env)
2012
end)
13+
List.wrap(make_namespace_body(module_name, imports, body, exports))
2114
end
2215

2316
def import_module(module_name, env) do
@@ -38,7 +31,7 @@ defmodule ElixirScript.ModuleSystems.Namespace do
3831
),
3932
[JS.identifier("Elixir"), Utils.name_to_js_file_name(module_name)]
4033
),
41-
JS.identifier("make")
34+
JS.identifier("__make")
4235
),
4336
[]
4437
)
@@ -47,10 +40,6 @@ defmodule ElixirScript.ModuleSystems.Namespace do
4740
JS.variable_declaration([declarator], :const)
4841
end
4942

50-
def export_module(exported_object) do
51-
exported_object
52-
end
53-
5443
def make_namespace_body(module_name, imports, body, exports) do
5544
exports = if is_nil(exports), do: [], else: [JS.return_statement(exports)]
5645

@@ -68,10 +57,12 @@ defmodule ElixirScript.ModuleSystems.Namespace do
6857
),
6958
[JS.identifier("Elixir"), Utils.name_to_js_file_name(module_name)]
7059
),
71-
JS.identifier("make")
60+
JS.identifier("__make")
7261
)
7362

74-
func = JS.function_expression([JS.identifier("Elixir")], [], JS.block_statement(imports ++ body ++ exports))
63+
func_body = JS.block_statement(imports ++ body ++ exports)
64+
65+
func = JS.function_expression([JS.identifier("Elixir")], [], func_body)
7566
JS.assignment_expression(
7667
:=,
7768
make,

lib/elixir_script/module_systems/umd.ex

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ defmodule ElixirScript.ModuleSystems.UMD do
55
alias ElixirScript.Translator.State
66
alias ElixirScript.Translator.Utils
77

8-
def build(std_import, imports, js_imports, body, exports, env) do
8+
def build(std_import, imports, js_imports, body, exports) do
99
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, path) end)
1010

1111
imports = js_imports ++ List.wrap(std_import)
1212
|> Enum.map(fn
13-
{module, path} -> import_module(module, path, env)
14-
{module, path, true} -> import_module(module, path, env)
15-
{module, path, false} -> import_namespace_module(module, path, env)
13+
{module, path} -> import_module(module, path)
14+
{module, path, true} -> import_module(module, path)
15+
{module, path, false} -> import_namespace_module(module, path)
1616
end)
1717

1818
imports = Enum.uniq(imports ++ module_imports)
@@ -21,35 +21,12 @@ defmodule ElixirScript.ModuleSystems.UMD do
2121
List.wrap(make_umd(imports, body, export))
2222
end
2323

24-
defp module_imports_to_js_imports(module_refs, env) do
25-
Enum.map(module_refs, fn(x) ->
26-
module_name = Utils.name_to_js_name(x)
27-
app_name = State.get_module(env.state, x).app
28-
path = Utils.make_local_file_path(app_name, Utils.name_to_js_file_name(x), env)
29-
import_module(module_name, path)
30-
end)
31-
end
32-
33-
defp make_std_lib_import(env) do
34-
compiler_opts = State.get(env.state).compiler_opts
35-
case compiler_opts.import_standard_libs do
36-
true ->
37-
[{:Elixir, Utils.make_local_file_path(:elixir, compiler_opts.core_path, env), true }]
38-
false ->
39-
[]
40-
end
41-
end
42-
43-
def import_namespace_module(module_name, from, env) do
44-
{Translator.translate!(module_name, env), JS.literal(from)}
45-
end
46-
47-
def import_module(:Elixir, from, env) do
48-
{JS.identifier("Elixir"), JS.literal(from)}
24+
def import_namespace_module(module_name, from) do
25+
{JS.identifier(module_name), JS.literal(from)}
4926
end
5027

51-
def import_module(module_name, from, env) do
52-
{Translator.translate!(module_name, env), JS.literal(from)}
28+
def import_module(module_name, from) do
29+
{JS.identifier(module_name), JS.literal(from)}
5330
end
5431

5532
def import_module(import_name, from) do

lib/elixir_script/passes/consolidate_protocols.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
7171
x = Atom.to_string(Utils.quoted_to_name(impl_data.for))
7272
x = String.to_atom(protocol_name <> ".DefImpl." <> x)
7373
name = Utils.name_to_js_name(x)
74-
{name, Utils.make_local_file_path(impl_data.app, Utils.name_to_js_file_name(x), compiler_opts.root, nil)}
74+
{x, ""}
7575
end)
7676

7777
body = Enum.map(implementations, fn({_, impl_data}) ->
Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,71 @@
11
defmodule ElixirScript.Passes.CreateJSModules do
2-
@moduledoc false
2+
@moduledoc false
33
alias ElixirScript.Translator.Utils
4+
alias ESTree.Tools.Builder, as: JS
45

56
def execute(compiler_data, opts) do
6-
parent = self
7-
data = Enum.map(compiler_data.data, fn({module_name, module_data}) ->
8-
module_data = compile(module_data, opts, compiler_data.state)
9-
{module_name, module_data}
7+
namespace_modules = Enum.reduce(compiler_data.data, %{ js_imports: [], body: [] }, fn
8+
({_, %{load_only: true} = module_data}, acc) ->
9+
acc
10+
11+
({module_name, module_data}, acc) ->
12+
{js_imports, body} = generate_namespace_module(
13+
module_data.type,
14+
module_name,
15+
Map.get(module_data, :javascript_module, module_data),
16+
opts,
17+
compiler_data.state
18+
)
19+
20+
Map.update!(acc, :js_imports, fn x -> x ++ js_imports end)
21+
|> Map.update!(:body, fn x -> x ++ body end)
1022
end)
1123

12-
%{ compiler_data | data: data }
24+
compiled = compile(namespace_modules.js_imports, namespace_modules.body, opts)
25+
Map.put(compiler_data, :compiled, compiled)
1326
end
1427

15-
defp compile(%{load_only: true} = module_data, opts, state) do
16-
module_data
17-
end
28+
defp generate_namespace_module(:consolidated, module_name, js_module, opts, state) do
29+
env = ElixirScript.Translator.LexicalScope.module_scope(
30+
js_module.name,
31+
Utils.name_to_js_file_name(js_module.name) <> ".js",
32+
opts.env,
33+
state,
34+
opts)
1835

19-
defp compile(%{type: :consolidated} = module_data, opts, state) do
20-
js_module = module_data
21-
env = ElixirScript.Translator.LexicalScope.module_scope(module_data.name, Utils.name_to_js_file_name(module_data.name) <> ".js", opts.env, state, opts)
22-
23-
ast = opts.module_formatter.build(
24-
js_module.std_lib,
25-
js_module.imports,
26-
js_module.js_imports,
36+
body = ElixirScript.ModuleSystems.Namespace.build(
37+
module_name,
38+
js_module.imports,
2739
js_module.body,
2840
js_module.exports,
2941
env
3042
)
3143

32-
Map.put(module_data, :javascript_ast, ast)
33-
end
44+
{js_module.js_imports, body}
45+
end
3446

35-
defp compile(module_data, opts, state) do
36-
js_module = module_data.javascript_module
37-
env = js_module.env
38-
39-
ast = opts.module_formatter.build(
40-
js_module.std_lib,
41-
js_module.imports,
42-
js_module.js_imports,
47+
defp generate_namespace_module(_, module_name, js_module, _, _) do
48+
body = ElixirScript.ModuleSystems.Namespace.build(
49+
module_name,
50+
js_module.imports,
4351
js_module.body,
4452
js_module.exports,
45-
env
53+
js_module.env
4654
)
4755

48-
Map.put(module_data, :javascript_ast, ast)
49-
end
56+
{js_module.js_imports, body}
57+
end
58+
59+
defp compile(js_imports, body, opts) do
60+
ast = opts.module_formatter.build(
61+
{:Elixir, "./Elixir.Bootstrap", true },
62+
[],
63+
js_imports,
64+
body,
65+
JS.identifier("Elixir")
66+
)
67+
68+
ast
69+
end
5070

5171
end

0 commit comments

Comments
 (0)