Skip to content

Commit 9ed8b95

Browse files
committed
Trying out a switch to exos instead
1 parent f41b48b commit 9ed8b95

9 files changed

Lines changed: 71 additions & 34 deletions

File tree

lib/elixir_script.ex

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,10 @@ defmodule ElixirScript do
5252
"""
5353
@spec javascript_ast_to_code(ESTree.Node.t) :: {:ok, binary} | {:error, binary}
5454
def javascript_ast_to_code(js_ast) do
55-
js_ast = case js_ast do
56-
modules when is_list(modules) ->
57-
Enum.reduce(modules, [], fn(x, list) -> list ++ x.body end)
58-
|> ESTree.Builder.program
59-
%ElixirScript.Translator.Group{body: body} ->
60-
ESTree.Builder.program(body)
61-
_ ->
62-
js_ast
63-
end
64-
65-
js_ast = Poison.encode!(js_ast)
66-
67-
path = "#{operating_path}/code_generator.js"
68-
69-
case System.cmd("node", [path, js_ast]) do
70-
{js_code, 0} ->
71-
{:ok, js_code }
72-
{error, _} ->
73-
{:error, error}
74-
end
55+
js_ast = prepare_js_ast(js_ast)
56+
|> Poison.encode!
57+
58+
{:ok, ElixirScript.CodeGenerator.translate(js_ast) }
7559
end
7660

7761
@doc """
@@ -87,6 +71,18 @@ defmodule ElixirScript do
8771
end
8872
end
8973

74+
defp prepare_js_ast(js_ast) do
75+
case js_ast do
76+
modules when is_list(modules) ->
77+
Enum.reduce(modules, [], fn(x, list) -> list ++ x.body end)
78+
|> ESTree.Builder.program
79+
%ElixirScript.Translator.Group{body: body} ->
80+
ESTree.Builder.program(body)
81+
_ ->
82+
js_ast
83+
end
84+
end
85+
9086
@doc """
9187
Writes output to file
9288
"""

lib/elixir_script/app.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule ElixirScript.App do
2+
use Application
3+
4+
def start(_,_) do
5+
ElixirScript.App.Sup.start_link
6+
end
7+
8+
defmodule Sup do
9+
use Supervisor
10+
def start_link, do: Supervisor.start_link(__MODULE__,[])
11+
def init([]), do: supervise([
12+
worker(ElixirScript.CodeGenerator, [0])
13+
], strategy: :one_for_one)
14+
end
15+
16+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule ElixirScript.CodeGenerator do
2+
3+
def start_link(ini) do
4+
Exos.Proc.start_link("node code_generator.js", ini, [cd: ElixirScript.operating_path], name: __MODULE__)
5+
end
6+
7+
def translate(code) do
8+
GenServer.cast(__MODULE__, {:translate, code })
9+
GenServer.call(__MODULE__, :get, :infinity)
10+
end
11+
12+
end

lib/mix/tasks/ex2js.prof.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ defmodule Mix.Tasks.Ex2js.Prof do
55
@shortdoc "Profiles ElixirScript"
66

77
def run(args) do
8+
Mix.Task.run "app.start"
9+
810
records = do_analyze
911
total_percent = Enum.reduce(records, 0.0, &(&1.percent + &2))
1012
IO.inspect "total = #{total_percent}"
1113
end
1214

1315
defp do_analyze do
1416
profile do
15-
ElixirScript.CLI.main(["sample/src/**/*.exjs", "-o", "sample/dest"])
17+
ElixirScript.CLI.main(["/Users/bryanjos/projects/basstype/color_bar_spike/src/exjs/**/*.exjs", "-o", "sample/dest"])
1618
end
1719
end
1820
end

mix.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ defmodule ElixirScript.Mixfile do
1717
end
1818

1919
def application do
20-
[applications: [:logger, :poison]]
20+
[
21+
applications: [:logger, :poison],
22+
mod: { ElixirScript.App, [] }
23+
]
2124
end
2225

2326
defp deps do
@@ -26,6 +29,7 @@ defmodule ElixirScript.Mixfile do
2629
{ :inflex, "~> 1.0" },
2730
{ :estree, github: "bryanjos/elixir-estree"},
2831
{ :exprof, "~> 0.2" },
32+
{ :exos, "~> 1.0.0" },
2933
{ :excoveralls, only: [:dev, :test] },
3034
{ :shouldi, github: "batate/shouldi", only: :test },
3135
{ :mix_test_watch, "~> 0.1.1", only: :test }

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
%{"estree": {:git, "git://github.com/bryanjos/elixir-estree.git", "527fa7005504884359b66da3c9eb53c706dfccb1", []},
22
"excoveralls": {:hex, :excoveralls, "0.3.10"},
33
"exjsx": {:hex, :exjsx, "3.1.0"},
4+
"exos": {:hex, :exos, "1.0.0"},
45
"exprintf": {:hex, :exprintf, "0.1.6"},
56
"exprof": {:hex, :exprof, "0.2.0"},
67
"fs": {:hex, :fs, "0.9.2"},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"author": "",
1717
"license": "MIT",
1818
"dependencies": {
19-
"escodegen": "^1.6.1"
19+
"escodegen": "^1.6.1",
20+
"node_erlastic": "0.0.8"
2021
},
2122
"devDependencies": {
2223
"acorn": "^0.11.0",

priv/javascript/code_generator.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
/**
2-
Takes in SpiderMonkey AST in JSON format and returns JavaScript code
3-
represented by the passed in AST.
4-
5-
Usage: node code_generator.js <ast>
6-
**/
71
var escodegen = require('escodegen');
8-
var args = process.argv.slice(2);
2+
var node_erlastic = require('node_erlastic');
3+
4+
node_erlastic.server(function(term, from, state, done){
5+
if (term == "get"){
6+
return done("reply", state);
7+
}
98

10-
var generated = escodegen.generate(JSON.parse(args[0]));
11-
process.stdout.write(generated);
12-
process.exit(0);
9+
if (term[0] == "translate"){
10+
var ast = JSON.parse(term[1]);
11+
var generated = escodegen.generate(ast);
12+
return done("noreply", generated);
13+
}
14+
15+
throw new Error("unexpected request")
16+
});

priv/javascript/release.package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"author": "",
1717
"license": "MIT",
1818
"dependencies": {
19-
"escodegen": "^1.6.1"
19+
"escodegen": "^1.6.1",
20+
"node_erlastic": "0.0.8"
2021
}
2122
}

0 commit comments

Comments
 (0)