Skip to content

Commit beff4a7

Browse files
committed
Move Elixir app code into core
1 parent e987c47 commit beff4a7

File tree

3 files changed

+20
-67
lines changed

3 files changed

+20
-67
lines changed

lib/elixir_script/passes/output.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ defmodule ElixirScript.Output do
4949
end
5050

5151
defp concat(code) do
52-
"'use strict';\nimport ElixirScript from './ElixirScript.Core.js';\n#{code}"
52+
"""
53+
'use strict';
54+
import ElixirScript from './ElixirScript.Core.js';
55+
const Elixir = ElixirScript.Core.initApp();
56+
#{code}
57+
"""
5358
end
5459

5560
defp prepare_js_ast(js_ast) do

lib/elixir_script/passes/output/js_module.ex

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,12 @@ defmodule ElixirScript.Output.JSModule do
22
@moduledoc false
33

44
alias ESTree.Tools.Builder, as: J
5-
alias ElixirScript.Translate.Helpers
65

76
def compile(body, opts, js_modules) do
8-
elixir = Helpers.declare("Elixir", J.object_expression([]))
9-
107
imports = opts.module_formatter.build_imports(js_modules)
118
exports = opts.module_formatter.build_export(J.identifier("Elixir"))
129

13-
[elixir, create_atom_table(), start(), load(), imports] ++ body ++ [exports]
14-
end
15-
16-
def start do
17-
normal = Helpers.symbol("normal")
18-
19-
Helpers.assign(
20-
J.member_expression(
21-
J.identifier("Elixir"),
22-
J.identifier("start")
23-
),
24-
Helpers.function(
25-
[J.identifier(:app), J.identifier(:args)],
26-
J.block_statement([
27-
Helpers.call(
28-
J.member_expression(
29-
Helpers.call(
30-
J.member_expression(
31-
J.identifier(:app),
32-
J.identifier("__load")
33-
),
34-
[J.identifier("Elixir")]
35-
),
36-
J.identifier("start")
37-
),
38-
[normal, J.identifier(:args)]
39-
)
40-
])
41-
)
42-
)
43-
end
44-
45-
def load do
46-
Helpers.assign(
47-
J.member_expression(
48-
J.identifier("Elixir"),
49-
J.identifier("load")
50-
),
51-
Helpers.function(
52-
[J.identifier(:module)],
53-
J.block_statement([
54-
J.return_statement(
55-
Helpers.call(
56-
J.member_expression(
57-
J.identifier(:module),
58-
J.identifier("__load")
59-
),
60-
[J.identifier("Elixir")]
61-
)
62-
)
63-
])
64-
)
65-
)
66-
end
67-
68-
defp create_atom_table() do
69-
Helpers.assign(
70-
J.member_expression(
71-
J.identifier("Elixir"),
72-
J.identifier("__table__")
73-
),
74-
J.object_expression([])
75-
)
10+
[imports] ++ body ++ [exports]
7611
end
7712

7813
end

src/javascript/lib/core.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ function get_global() {
3030
return null;
3131
}
3232

33+
function initApp() {
34+
const Elixir = {};
35+
36+
Elixir.__table__ = {};
37+
Elixir.start = (app, args) => {
38+
app.__load(Elixir).start(Symbol.for('normal'), args);
39+
};
40+
Elixir.load = module => module.__load(Elixir);
41+
42+
return Elixir;
43+
}
44+
3345
const globalState = get_global();
3446

3547
globalState.__elixirscript_store__ = new Map();
@@ -57,4 +69,5 @@ export default {
5769
elixir_config,
5870
math,
5971
proplists,
72+
initApp,
6073
};

0 commit comments

Comments
 (0)