Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2fdc7a

Browse files
committed
Update module refs to reference module directly. Remove processing of standard lib in tests.
1 parent 56cb2c5 commit d2fdc7a

File tree

20 files changed

+319
-163
lines changed

20 files changed

+319
-163
lines changed

lib/elixir_script/module_systems/namespace.ex

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,8 @@ defmodule ElixirScript.ModuleSystems.Namespace do
66
alias ElixirScript.Translator.Utils
77
alias ElixirScript.Translator.Identifier
88

9-
def build(module_name, imports, body, exports, env) do
10-
module_imports = imports
11-
|> Enum.filter(fn
12-
{mod, _} ->
13-
case Module.split(mod) do
14-
["JS"] -> false
15-
_ -> true
16-
end
17-
end)
18-
|> Enum.map(fn {module, path} ->
19-
import_module(module)
20-
end)
21-
22-
List.wrap(make_namespace_body(module_name, module_imports, body, exports))
9+
def build(module_name, body, exports, env) do
10+
List.wrap(make_namespace_body(module_name, body, exports))
2311
end
2412

2513
defp module_name_function_call(module_name, function) do
@@ -54,7 +42,7 @@ defmodule ElixirScript.ModuleSystems.Namespace do
5442
)
5543
end
5644

57-
defp make_namespace_body(module_name, imports, body, exports) do
45+
defp make_namespace_body(module_name, body, exports) do
5846
values = module_name_function_call(module_name, "__exports")
5947

6048
_if = JS.if_statement(
@@ -91,7 +79,7 @@ defmodule ElixirScript.ModuleSystems.Namespace do
9179
JS.identifier("__load")
9280
)
9381

94-
func_body = JS.block_statement([_if] ++ body ++ [declaration, assign] ++ imports ++ exports)
82+
func_body = JS.block_statement([_if] ++ body ++ [declaration, assign] ++ exports)
9583

9684
func = JS.function_expression([JS.identifier("Elixir")], [], func_body)
9785
JS.assignment_expression(

lib/elixir_script/passes/consolidate_protocols.ex

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
33
alias ESTree.Tools.Builder, as: JS
44
alias ElixirScript.Translator.Utils
55
alias ElixirScript.Translator.State
6+
alias ElixirScript.Translator.Identifier
67
require Logger
78

89
def execute(compiler_data, opts) do
@@ -67,23 +68,21 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
6768

6869
app_name = protocol.app
6970

70-
defimpl_imports = Enum.map(implementations, fn({_, impl_data}) ->
71-
x = Atom.to_string(Utils.quoted_to_name(impl_data.for))
72-
x = String.to_atom(protocol_name <> ".DefImpl." <> x)
73-
name = Utils.name_to_js_name(x)
74-
ElixirScript.ModuleSystems.Namespace.import_module(x)
75-
end)
76-
7771
body = Enum.map(implementations, fn({_, impl_data}) ->
78-
x = Atom.to_string(Utils.quoted_to_name(impl_data.for))
79-
x = String.to_atom(protocol_name <> ".DefImpl." <> x)
80-
name = Utils.name_to_js_name(x)
72+
x = Utils.quoted_to_name(impl_data.for)
73+
members = ["Elixir"] ++ Module.split(name) ++ ["DefImpl"] ++ Module.split(x) ++ ["__load"]
74+
ast = JS.call_expression(
75+
Identifier.make_namespace_members(members),
76+
[JS.identifier("Elixir")]
77+
)
78+
79+
8180
JS.call_expression(
8281
JS.member_expression(
8382
JS.identifier("impls"),
8483
JS.identifier("push")
8584
),
86-
[JS.identifier(name)]
85+
[ast]
8786
)
8887
end)
8988

@@ -92,8 +91,7 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
9291
name: name,
9392
module: String.to_atom(protocol_name <> ".DefImpl"),
9493
std_lib: {:Elixir, Utils.make_local_file_path(:elixir, compiler_opts.core_path, compiler_opts.root, nil)},
95-
imports: [],
96-
body: defimpl_imports ++ [declaration] ++ body,
94+
body: [declaration] ++ body,
9795
exports: JS.identifier("impls"),
9896
app: app_name,
9997
type: :consolidated,

lib/elixir_script/passes/create_js_modules.ex

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ defmodule ElixirScript.Passes.CreateJSModules do
99
acc
1010

1111
({module_name, module_data}, acc) ->
12-
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-
)
12+
if module_data.app == :elixir && opts.import_standard_libs == false do
13+
acc
14+
else
15+
body = generate_namespace_module(
16+
module_data.type,
17+
module_name,
18+
Map.get(module_data, :javascript_module, module_data),
19+
opts,
20+
compiler_data.state
21+
)
1922

20-
acc ++ List.wrap(body)
23+
acc ++ List.wrap(body)
24+
end
2125
end)
2226

2327
compiled = compile(namespace_modules, opts)
@@ -34,7 +38,6 @@ defmodule ElixirScript.Passes.CreateJSModules do
3438

3539
body = ElixirScript.ModuleSystems.Namespace.build(
3640
module_name,
37-
js_module.imports,
3841
js_module.body,
3942
js_module.exports,
4043
env
@@ -46,7 +49,6 @@ defmodule ElixirScript.Passes.CreateJSModules do
4649
defp generate_namespace_module(_, module_name, js_module, _, _) do
4750
body = ElixirScript.ModuleSystems.Namespace.build(
4851
module_name,
49-
js_module.imports,
5052
js_module.body,
5153
js_module.exports,
5254
js_module.env

lib/elixir_script/passes/java_script_ast.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ defmodule ElixirScript.Passes.JavaScriptAST do
88
State.set_module_data(compiler_data.state, compiler_data.data)
99
State.set_loaded_modules(compiler_data.state, Map.get(compiler_data, :loaded_modules, []))
1010

11-
data = Enum.map(State.get_module_data(compiler_data.state), fn({module_name, module_data}) ->
11+
data = compiler_data.state
12+
|> State.get_module_data
13+
|> Enum.reject(fn {_,module_data} ->
14+
module_data.app == :elixir && opts.import_standard_libs == false
15+
end)
16+
|> Enum.map(fn({module_name, module_data}) ->
1217
module_data = compile(module_data, opts, compiler_data.state)
1318
{module_name, module_data}
1419
end)

lib/elixir_script/translator.ex

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,15 @@ defmodule ElixirScript.Translator do
169169
end
170170

171171
defp do_translate({:&, _, [{:/, _, [{{:., _, [module_name, function_name]}, _, []}, arity]}]}, env) do
172-
{ Capture.make_capture(module_name, function_name, arity, env), env }
172+
module_name = create_module_name2(module_name, env)
173+
js_ast = case module_name do
174+
{mod, :local} ->
175+
Capture.make_capture(mod, function_name, arity, env)
176+
mod ->
177+
Capture.make_extern_capture(mod, function_name, arity, env)
178+
end
179+
180+
{ js_ast, env }
173181
end
174182

175183
defp do_translate({:&, _, [{:/, _, [{function_name, _, _}, arity]}]}, env) do
@@ -236,37 +244,34 @@ defmodule ElixirScript.Translator do
236244
end
237245

238246
defp do_translate({{:., _, [function_name]}, _, params}, env) do
239-
Call.make_function_call(function_name, params, env)
247+
Call.make_local_function_call(function_name, params, env)
240248
end
241249

242-
defp do_translate({:., _, [module_name, function_name]} = ast, env) do
243-
expanded_ast = Macro.expand(ast, env.env)
244-
245-
if expanded_ast == ast do
246-
module_name = create_module_name(module_name, env)
247-
Call.make_function_or_property_call(module_name, function_name, env)
248-
else
249-
translate(expanded_ast, env)
250-
end
250+
defp do_translate({:., _, [{:__aliases__, _, _} = module_name, function_name]} = ast, env) do
251+
do_translate({{:., [], [module_name, function_name]}, [], []}, env)
251252
end
252253

253-
defp do_translate({{:., _, [module_name, function_name]}, _, [] } = ast, env) do
254+
defp do_translate({{:., _, [{:__aliases__, _, _} = module_name, function_name]}, _, [] } = ast, env) do
254255
expanded_ast = Macro.expand(ast, env.env)
255256

256257
if expanded_ast == ast do
257-
module_name = create_module_name(module_name, env)
258-
Call.make_function_or_property_call(module_name, function_name, env)
258+
module_name = create_module_name2(module_name, env)
259+
case module_name do
260+
{mod, :local} ->
261+
Call.make_module_function_call(mod, function_name, env)
262+
mod ->
263+
Call.make_extern_function_or_property_call(mod, function_name, env)
264+
end
259265
else
260266
translate(expanded_ast, env)
261267
end
262268
end
263269

264-
defp do_translate({{:., _, [{:__aliases__, _, _} = module_name]}, _, params} = ast, env) do
270+
defp do_translate({:., _, [module_name, function_name]} = ast, env) do
265271
expanded_ast = Macro.expand(ast, env.env)
266272

267273
if expanded_ast == ast do
268-
module_name = create_module_name(module_name, env)
269-
Call.make_function_call(module_name, params, env)
274+
Call.make_function_or_property_call(module_name, function_name, env)
270275
else
271276
translate(expanded_ast, env)
272277
end
@@ -288,11 +293,36 @@ defmodule ElixirScript.Translator do
288293
JSLib.translate_js_function(function_name, params, env)
289294
end
290295

296+
defp do_translate({{:., _, [{:__aliases__, _, _} = module_name, function_name]}, _, params } = ast, env) do
297+
expanded_ast = Macro.expand(ast, env.env)
298+
299+
if expanded_ast == ast do
300+
module_name = create_module_name2(module_name, env)
301+
case module_name do
302+
{mod, :local} ->
303+
Call.make_module_function_call(mod, function_name, params, env)
304+
mod ->
305+
Call.make_extern_function_call(mod, function_name, params, env)
306+
end
307+
else
308+
translate(expanded_ast, env)
309+
end
310+
end
311+
312+
defp do_translate({{:., _, [module_name, function_name]}, _, params} = ast, env) when is_atom(module_name) and is_atom(function_name) do
313+
expanded_ast = Macro.expand(ast, env.env)
314+
315+
if expanded_ast == ast do
316+
Call.make_function_call(module_name, function_name, params, env)
317+
else
318+
translate(expanded_ast, env)
319+
end
320+
end
321+
291322
defp do_translate({{:., _, [module_name, function_name]}, _, params } = ast, env) do
292323
expanded_ast = Macro.expand(ast, env.env)
293324

294325
if expanded_ast == ast do
295-
module_name = create_module_name(module_name, env)
296326
Call.make_function_call(module_name, function_name, params, env)
297327
else
298328
translate(expanded_ast, env)
@@ -304,8 +334,13 @@ defmodule ElixirScript.Translator do
304334
end
305335

306336
defp do_translate({:__aliases__, _, aliases} = ast, env) do
307-
module_name = create_module_name(ast, env)
308-
Call.make_module_name(module_name, env)
337+
module_name = create_module_name2(ast, env)
338+
case module_name do
339+
{mod, :local} ->
340+
Call.make_module_name(mod, env)
341+
mod ->
342+
Call.make_extern_module_name(mod, env)
343+
end
309344
end
310345

311346
defp do_translate({:__MODULE__, _, _ }, env) do
@@ -580,12 +615,12 @@ defmodule ElixirScript.Translator do
580615

581616
cond do
582617
name_arity in module.functions or name_arity in module.private_functions ->
583-
Call.make_function_call(name, params, env)
618+
Call.make_local_function_call(name, params, env)
584619
ElixirScript.Translator.LexicalScope.find_module(env, name_arity) ->
585620
imported_module_name = ElixirScript.Translator.LexicalScope.find_module(env, name_arity)
586-
Call.make_function_call(imported_module_name, name, params, env)
621+
Call.make_module_function_call(imported_module_name, name, params, env)
587622
true ->
588-
Call.make_function_call(name, params, env)
623+
Call.make_local_function_call(name, params, env)
589624
end
590625

591626
else
@@ -604,7 +639,7 @@ defmodule ElixirScript.Translator do
604639
Call.make_function_call(name, [], env)
605640
ElixirScript.Translator.LexicalScope.find_module(env, {name, 0}) ->
606641
imported_module_name = ElixirScript.Translator.LexicalScope.find_module(env, {name, 0})
607-
Call.make_function_call(imported_module_name, name, params, env)
642+
Call.make_module_function_call(imported_module_name, name, params, env)
608643
true ->
609644
{ Identifier.make_identifier(name), env }
610645
end
@@ -645,6 +680,17 @@ defmodule ElixirScript.Translator do
645680
end
646681
end
647682

683+
def create_module_name2(module_name, env) do
684+
module_name = Utils.quoted_to_name(module_name)
685+
candiate_module_name = ElixirScript.Translator.State.get_module_name(env.state, module_name)
686+
687+
if ElixirScript.Translator.LexicalScope.get_module_name(env, candiate_module_name) in ElixirScript.Translator.State.list_module_names(env.state) do
688+
{ ElixirScript.Translator.LexicalScope.get_module_name(env, candiate_module_name), :local }
689+
else
690+
module_name
691+
end
692+
end
693+
648694
def has_function?(module_name, name_arity, env) do
649695
case ElixirScript.Translator.State.get_module(env.state, module_name) do
650696
nil ->

lib/elixir_script/translator/kernel/defimpl.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule ElixirScript.Translator.Defimpl do
88
def make(name, type, body, env) do
99

1010
type = map_to_js(type, env)
11-
{imports, body, export} = Defmodule.process_module(name, body, env)
11+
{body, export} = Defmodule.process_module(name, body, env)
1212

1313
protocol_name = Atom.to_string(name) |> String.split(".DefImpl.") |> hd |> String.to_atom
1414

@@ -24,8 +24,7 @@ defmodule ElixirScript.Translator.Defimpl do
2424
])
2525

2626
%{
27-
name: Utils.quoted_to_name({:__aliases__, [], name }),
28-
imports: imports,
27+
name: Utils.quoted_to_name({:__aliases__, [], name }),
2928
exports: export,
3029
body: body,
3130
app_name: State.get_module(env.state, name).app,

lib/elixir_script/translator/kernel/defmodule.ex

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule ElixirScript.Translator.Defmodule do
1212
{ body, _ } = translate_body(body, env)
1313
%{
1414
name: ElixirScript.Temp,
15-
imports: [],
1615
body: body |> Group.inflate_groups,
1716
exports: nil,
1817
app_name: ElixirScript.Translator.State.get(env.state).compiler_opts.app,
@@ -23,7 +22,6 @@ defmodule ElixirScript.Translator.Defmodule do
2322
def make_module(module, nil, env) do
2423
%{
2524
name: module,
26-
imports: [],
2725
body: [],
2826
exports: nil,
2927
app_name: ElixirScript.Translator.State.get(env.state).compiler_opts.app,
@@ -32,12 +30,11 @@ defmodule ElixirScript.Translator.Defmodule do
3230
end
3331

3432
def make_module(module, body, env) do
35-
{imports, body, exported_object} = process_module(module, body, env)
33+
{body, exported_object} = process_module(module, body, env)
3634
app_name = State.get_module(env.state, module).app
3735

3836
result = %{
3937
name: Utils.quoted_to_name({:__aliases__, [], module }),
40-
imports: imports,
4138
exports: exported_object,
4239
body: body,
4340
app_name: app_name,
@@ -77,11 +74,8 @@ defmodule ElixirScript.Translator.Defmodule do
7774
exported_functions = Enum.map(exported_functions, fn({_key, value}) -> value end)
7875
private_functions = Enum.map(private_functions, fn({_key, value}) -> value end)
7976

80-
module_refs = State.get_module_references(env.state, env.module) -- [env.module]
81-
imports = process_module_refs(module_refs, env)
82-
8377
body = structs ++ private_functions ++ exported_functions ++ body
84-
{imports, body, exported_object}
78+
{body, exported_object}
8579
end
8680

8781
def process_module_refs(module_refs, env) do

lib/elixir_script/translator/kernel/defprotocol.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,10 @@ defmodule ElixirScript.Translator.Defprotocol do
7474
)
7575

7676
body = body ++ [declaration] ++ [implementations]
77-
78-
module_refs = State.get_module_references(env.state, env.module) -- [env.module]
79-
imports = Defmodule.process_module_refs(module_refs, env)
8077
defimpl_import = ElixirScript.ModuleSystems.Namespace.import_module(implementation_name_module)
8178

8279
%{
8380
name: name,
84-
imports: imports,
8581
body: [defimpl_import] ++ body,
8682
exports: JS.identifier(Utils.name_to_js_name(name)),
8783
app_name: app_name,

0 commit comments

Comments
 (0)