Skip to content

Commit b57c2b1

Browse files
committed
Updated from testing
1 parent cbdb1f3 commit b57c2b1

File tree

16 files changed

+172
-25
lines changed

16 files changed

+172
-25
lines changed

lib/elixir_script/beam.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ defmodule ElixirScript.Beam do
2020
end
2121
end
2222

23+
#Replacing Agent module with our ElixirScript's version
24+
def debug_info(Agent) do
25+
case debug_info(ElixirScript.Agent) do
26+
{:ok, info} ->
27+
{:ok, Map.put(info, :module, Agent)}
28+
e ->
29+
e
30+
end
31+
end
32+
2333
def debug_info(module) when is_atom(module) do
2434
#TODO: Get modified date from _beam_path to check for cached version?
2535
with {_, beam, _beam_path} <- :code.get_object_code(module),

lib/elixir_script/lib/agent.ex

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule ElixirScript.Agent do
2+
@moduledoc false
3+
4+
def start(fun, options \\ []) do
5+
pid = JS.new JS.Bootstrap.Core.PID, []
6+
7+
name = if Keyword.has_key?(options, :name) do
8+
Keyword.get(options, :name)
9+
else
10+
nil
11+
end
12+
13+
ElixirScript.Store.create(pid, fun.(), name)
14+
{ :ok, pid }
15+
end
16+
17+
def start_link(fun, options \\ []) do
18+
pid = JS.new JS.Bootstrap.Core.PID, []
19+
20+
name = if Keyword.has_key?(options, :name) do
21+
Keyword.get(options, :name)
22+
else
23+
nil
24+
end
25+
26+
ElixirScript.Store.create(pid, fun.(), name)
27+
{ :ok, pid }
28+
end
29+
30+
def stop(agent) do
31+
ElixirScript.Store.remove(agent)
32+
:ok
33+
end
34+
35+
def update(agent, fun) do
36+
current_state = ElixirScript.Store.read(agent)
37+
ElixirScript.Store.update(agent, fun.(current_state))
38+
:ok
39+
end
40+
41+
def get(agent, fun) do
42+
current_state = ElixirScript.Store.read(agent)
43+
fun.(current_state)
44+
end
45+
46+
def get_and_update(agent, fun) do
47+
current_state = ElixirScript.Store.read(agent)
48+
{val, new_state} = fun.(current_state)
49+
ElixirScript.Store.update(agent, new_state)
50+
val
51+
end
52+
53+
end

lib/elixir_script/lib/store.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule ElixirScript.Store do
2+
3+
defp get_key(key) do
4+
real_key = case JS.__elixirscript_names__.has(key) do
5+
true ->
6+
JS.__elixirscript_names__.get(key)
7+
false ->
8+
key
9+
end
10+
11+
case JS.__elixirscript_store__.has(real_key) do
12+
true ->
13+
real_key
14+
false ->
15+
JS.throw JS.new(JS.Error, ["Key Not Found"])
16+
end
17+
end
18+
19+
def create(key, value, name \\ nil) do
20+
if name != nil do
21+
JS.__elixirscript_names__.set(name, key)
22+
end
23+
24+
JS.__elixirscript_store__.set(key, value)
25+
end
26+
27+
def update(key, value) do
28+
real_key = get_key(key)
29+
JS.__elixirscript_store__.set(real_key, value)
30+
end
31+
32+
def read(key) do
33+
real_key = get_key(key)
34+
JS.__elixirscript_store__.get(real_key)
35+
end
36+
37+
def remove(key) do
38+
real_key = get_key(key)
39+
JS.__elixirscript_store__.delete(real_key)
40+
end
41+
42+
43+
end

lib/elixir_script/passes/find_used_functions.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ defmodule ElixirScript.FindUsedFunctions do
3636

3737
reachable_defs = Enum.filter(defs, fn
3838
{ _, type, _, _} when type in [:defmacro, :defmacrop] -> false
39-
{ name, _, _, _} -> not(name in unreachable)
39+
{ name, _, _, _} ->
40+
not(name in unreachable)
4041
_ -> true
4142
end)
4243

@@ -128,8 +129,8 @@ defmodule ElixirScript.FindUsedFunctions do
128129
end
129130

130131
defp walk({:%, _, [module, params]}, state) do
131-
ModuleState.add_used(state.pid, module, {:__struct__, 0})
132-
ModuleState.add_used(state.pid, module, {:__struct__, 1})
132+
walk_module(module, :__struct__, 0, state.pid)
133+
walk_module(module, :__struct__, 1, state.pid)
133134
walk(params, state)
134135
end
135136

lib/elixir_script/passes/find_used_modules.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ defmodule ElixirScript.FindUsedModules do
232232
walk({function, [], params}, state)
233233
end
234234

235+
defp walk({{:., _, [module, function]} = ast, _, params}, state) do
236+
walk(ast, state)
237+
walk(params, state)
238+
end
239+
235240
defp walk({:., _, [JS, _]}, _) do
236241
nil
237242
end

lib/elixir_script/passes/translate/form.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ defmodule ElixirScript.Translate.Form do
207207
ElixirScript.Translate.Function.compile(ast, state)
208208
end
209209

210+
def compile({{:., _, [{_, _, nil} = var, func_or_prop]}, _, []}, state) do
211+
ast = J.call_expression(
212+
ElixirScript.Translate.Forms.JS.call_property(),
213+
[compile!(var, state), J.literal(to_string(func_or_prop))]
214+
)
215+
216+
{ast, state}
217+
end
218+
210219
def compile({{:., _, [JS, _]}, _, _} = ast, state) do
211220
ElixirScript.Translate.Forms.JS.compile(ast, state)
212221
end

lib/elixir_script/passes/translate/forms/js.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ defmodule ElixirScript.Translate.Forms.JS do
3939
def compile({{:., _, [JS, :new]}, _, [module, params]}, state) do
4040
members = Module.split(module)
4141

42+
members = case members do
43+
["JS" | rest] ->
44+
rest
45+
x ->
46+
x
47+
end
48+
4249
params = case params do
4350
p when is_list(p) ->
4451
Enum.map(params, &Form.compile!(&1, state))

lib/elixir_script/passes/translate/forms/pattern.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ defmodule ElixirScript.Translate.Forms.Pattern do
118118

119119
defp process_pattern({:%{}, _, props}, state) do
120120
properties = Enum.map(props, fn
121+
{:__module__struct__, {_, _, nil} = var } ->
122+
{pattern, params} = process_pattern(var, state)
123+
124+
a = J.object_expression([%ESTree.Property{
125+
key: J.identifier("__MODULE__"),
126+
value: hd(List.wrap(pattern))
127+
}])
128+
129+
property = ElixirScript.Translate.Forms.Map.make_property(
130+
Form.compile!(:__struct__, state),
131+
a
132+
)
133+
134+
{ property, params }
135+
121136
{:__module__struct__, module} ->
122137
a = J.object_expression([%ESTree.Property{
123138
key: J.identifier("__MODULE__"),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"license": "MIT",
2424
"dependencies": {
2525
"erlang-types": "^1.0.1",
26-
"tailored": "^2.4.7"
26+
"tailored": "^2.4.8"
2727
},
2828
"devDependencies": {
2929
"ava": "^0.19.1",

rollup.config.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export default {
88
plugins: [
99
nodeResolve({ jsnext: true }),
1010
babel({
11-
babelrc: false,
12-
}),
13-
babili({
14-
keepFnName: true,
15-
keepClassName: true,
16-
}),
11+
babelrc: false
12+
})
13+
//babili({
14+
// keepFnName: true,
15+
// keepClassName: true,
16+
//}),
1717
],
18-
targets: [{ dest: 'priv/build/iife/Elixir.Bootstrap.js', format: 'iife' }],
18+
targets: [{ dest: 'priv/build/iife/Elixir.Bootstrap.js', format: 'iife' }]
1919
};

0 commit comments

Comments
 (0)