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
35 changes: 24 additions & 11 deletions lib/elixir_script.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ defmodule ElixirScript do
end

# This is the serialized state of the ElixirScript.State module containing references to the standard library
@external_resource stdlib_state_path = Path.join([__DIR__, "elixir_script", "translator", "stdlib_state.bin"])
@stdlib_state File.read(stdlib_state_path)
@lib_path Application.get_env(:elixir_script, :lib_path)
@version Mix.Project.config[:version]

Expand Down Expand Up @@ -73,30 +71,45 @@ defmodule ElixirScript do
|> get_modules_from_quoted
|> Enum.map(fn(x) -> %{ast: x, app: :app} end)

data = get_quoted_std_lib() ++ data
{loaded_modules, std_lib_quoted} = get_quoted_std_lib()

result = %{data: data}
%{data: std_lib_quoted ++ data, loaded_modules: loaded_modules}
|> ElixirScript.Passes.Init.execute(opts)
|> ElixirScript.Passes.LoadModulesForQuoted.execute(opts)
|> ElixirScript.Passes.FindModules.execute(opts)
|> ElixirScript.Passes.FindLoadOnly.execute(opts)
|> ElixirScript.Passes.FindFunctions.execute(opts)
|> ElixirScript.Passes.JavaScriptAST.execute(opts)
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
|> ElixirScript.Passes.CreateJSModules.execute(opts)
|> ElixirScript.Passes.CreateJSModules.execute(opts)
|> ElixirScript.Passes.JavaScriptCode.execute(opts)
|> ElixirScript.Passes.JavaScriptName.execute(opts)
|> ElixirScript.Passes.HandleOutput.execute(opts)

result
end

defp get_quoted_std_lib() do
Path.join([get_std_lib_path(), "**", "*.ex"])
files = [get_std_lib_path(), "**", "*.ex"]
|> Path.join
|> Path.wildcard

loaded_modules = if Code.ensure_loaded?(ElixirScript.Kernel) do
[]
else
case files do
[] ->
[]
files ->
Kernel.ParallelCompiler.files(files)
end
end

std_lib_quoted = files
|> Enum.map(fn path -> File.read!(path) end)
|> Enum.map(&Code.string_to_quoted!(&1))
|> Enum.flat_map(&get_modules_from_quoted(&1))
|> Enum.map(fn(x) -> %{ast: x, app: :elixir} end)

{loaded_modules, std_lib_quoted}
end

defp get_modules_from_quoted(quoted) do
Expand Down Expand Up @@ -138,7 +151,7 @@ defmodule ElixirScript do
compile_path([path], opts)
end

def compile_path(path, opts) when is_list(path) do
def compile_path(path, opts) when is_list(path) do
built_opts = build_compiler_options(opts)

app_name = cond do
Expand Down Expand Up @@ -171,7 +184,7 @@ defmodule ElixirScript do
|> ElixirScript.Passes.FindFunctions.execute(opts)
|> ElixirScript.Passes.JavaScriptAST.execute(opts)
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
|> ElixirScript.Passes.CreateJSModules.execute(opts)
|> ElixirScript.Passes.CreateJSModules.execute(opts)
|> ElixirScript.Passes.JavaScriptCode.execute(opts)
|> ElixirScript.Passes.JavaScriptName.execute(opts)
|> ElixirScript.Passes.HandleOutput.execute(opts)
Expand Down Expand Up @@ -241,7 +254,7 @@ defmodule ElixirScript do
end

defp get_std_lib_path() do
Path.join([operating_path(), "std_lib"])
Path.join([operating_path(), "std_lib"])
end

end
6 changes: 3 additions & 3 deletions lib/elixir_script/passes/ast_from_file.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
defmodule ElixirScript.Passes.ASTFromFile do
@moduledoc false
@moduledoc false

def execute(compiler_data, opts) do
data = Enum.reduce(compiler_data.data, [], fn({dep, paths}, list) ->

file_paths = paths
|> Enum.flat_map(fn(path) -> Path.join(path, "**/*.{ex,exs,exjs}") |> Path.wildcard end)
|> Enum.flat_map(fn(path) -> Path.join([path, "**", "*.{ex,exs,exjs}"]) |> Path.wildcard end)
|> Enum.reduce([], fn(path, list) ->
quoted = path
|> File.read!
|> Code.string_to_quoted!

stat = File.stat!(path)

list ++ [%{ path: path, app: dep, stat: stat, ast: quoted }]
list ++ [%{path: path, app: dep, stat: stat, ast: quoted}]
end)


Expand Down
28 changes: 28 additions & 0 deletions lib/elixir_script/passes/load_modules_for_quoted.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule ElixirScript.Passes.LoadModulesForQuoted do
@moduledoc false
def execute(compiler_data, _) do
quoted = compiler_data.data
|> Enum.filter(fn
{_, %{app: :elixir}} ->
false
%{app: :elixir} ->
false
_ -> true
end)
|> Enum.map(fn
{_, %{ast: ast}} -> ast
%{ast: ast} -> ast
end)

loaded_modules = Map.get(compiler_data, :loaded_modules, [])

loaded_modules_from_quoted = quoted
|> Enum.map(&Code.compile_quoted(&1))
|> List.flatten
|> Enum.map(fn {mod, _} -> mod end)


Map.put(compiler_data, :loaded_modules, loaded_modules ++ loaded_modules_from_quoted)
end

end
6 changes: 5 additions & 1 deletion lib/elixir_script/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ defmodule ElixirScript.Translator do
end
end

defp do_translate({{:., context1, [{:__aliases__, context2, [:Elixir, :Enum]}, function_name]}, context3, params }, env) do
translate({{:., context1, [{:__aliases__, context2, [:Enum]}, function_name]}, context3, params }, env)
end

defp do_translate({{:., context1, [{:__aliases__, context2, [:Enum]}, function_name]}, context3, params }, env) do
translate({{:., context1, [{:__aliases__, context2, [:Bootstrap, :Enum]}, function_name]}, context3, params }, env)
end
Expand Down Expand Up @@ -628,7 +632,7 @@ defmodule ElixirScript.Translator do
def create_module_name(module_name, env) do
case module_name do
{:__aliases__, _, _} ->
candiate_module_name = ElixirScript.Translator.State.get_module_name(env.state,
candiate_module_name = ElixirScript.Translator.State.get_module_name(env.state,
Utils.quoted_to_name(module_name))

if ElixirScript.Translator.LexicalScope.get_module_name(env, candiate_module_name) in ElixirScript.Translator.State.list_module_names(env.state) do
Expand Down
57 changes: 0 additions & 57 deletions test/elixir_script_test.exs

This file was deleted.

18 changes: 15 additions & 3 deletions test/prelude/js_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ defmodule ElixirScript.Lib.JS.Test do

test "translate new" do
ex_ast = quote do
JS.new A.B, [1, 2, 3]
require JS

def execute() do
JS.new A.B, [1, 2, 3]
end
end

js_code = """
Expand All @@ -14,7 +18,11 @@ defmodule ElixirScript.Lib.JS.Test do
assert_translation(ex_ast, js_code)

ex_ast = quote do
JS.new A, [1, 2, 3]
require JS

def execute() do
JS.new A, [1, 2, 3]
end
end

js_code = """
Expand All @@ -26,7 +34,11 @@ defmodule ElixirScript.Lib.JS.Test do

test "translate update" do
ex_ast = quote do
JS.update A, %{"b" => [1, 2, 3]}
require JS

def execute() do
JS.update A, %{"b" => [1, 2, 3]}
end
end

js_code = """
Expand Down
4 changes: 2 additions & 2 deletions test/prelude/kernel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ defmodule ElixirScript.Lib.Elixir.Kernel.Test do

test "translate sigil_r with options" do
ex_ast = quote do
~r/foo/ig
~r/foo/i
end

js_code = """
Elixir$ElixirScript$Regex.compile__emark__('foo', 'ig')
Elixir$ElixirScript$Regex.compile__emark__('foo', 'i')
"""

assert_translation(ex_ast, js_code)
Expand Down
10 changes: 4 additions & 6 deletions test/translator/access_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ defmodule ElixirScript.Translator.Access.Test do
import ElixirScript.TestHelper

test "translate access" do
ex_ast = quote do: a[:b]
ex_ast = quote do
a = []
a[:b]
end
js_code = "a[Symbol.for('b')]"

assert_translation(ex_ast, js_code)

ex_ast = quote do: a["b"]
js_code = "a['b']"

assert_translation(ex_ast, js_code)
end
end
43 changes: 31 additions & 12 deletions test/translator/bitstring_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,45 @@ defmodule ElixirScript.Translator.Bitstring.Test do
ex_ast = quote do: <<1, "foo" :: utf8, "bar" :: utf32>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), Bootstrap.Core.BitString.utf8('foo'), Bootstrap.Core.BitString.utf32('bar'))")

ex_ast = quote do: <<102 :: integer-native, rest :: binary>>
ex_ast = quote do
rest = "oo"
<<102 :: integer-native, rest :: binary>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.native(Bootstrap.Core.BitString.integer(102)), Bootstrap.Core.BitString.binary(rest))")

ex_ast = quote do: <<102 :: unsigned-big-integer, rest :: binary>>
ex_ast = quote do
rest = "oo"
<<102 :: unsigned-big-integer, rest :: binary>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(Bootstrap.Core.BitString.big(Bootstrap.Core.BitString.unsigned(102))), Bootstrap.Core.BitString.binary(rest))")

ex_ast = quote do: <<102, _rest :: size(16)>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(_rest, 16))")
ex_ast = quote do
rest = 100
<<102, rest :: size(16)>>
end

ex_ast = quote do: <<102, _rest :: size(16)-unit(4)>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(_rest, 16), 4))")
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(rest, 16))")

ex_ast = quote do: <<102, _rest :: 16 * 4>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(_rest, 16), 4))")
ex_ast = quote do
rest = 100
<<102, rest :: size(16)-unit(4)>>
end

ex_ast = quote do: <<102, _rest :: _ * 4>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(_rest, undefined), 4))")
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(rest, 16), 4))")

ex_ast = quote do: <<102, _rest :: 16>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(_rest, 16))")
ex_ast = quote do
rest = 100
<<102, rest :: 16 * 4>>
end

assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(rest, 16), 4))")

ex_ast = quote do
rest = 100
<<102, rest :: 16>>
end

assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(rest, 16))")

ex_ast = quote do: << 1, <<2>> >>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(2)))")
Expand Down
Loading