Skip to content

Commit b68db3e

Browse files
committed
Fixed issues with protocols and agents and defgen defined functions
1 parent c62693f commit b68db3e

File tree

11 files changed

+77
-31
lines changed

11 files changed

+77
-31
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [0.23.2] - Unreleased
8+
9+
### Fixed
10+
- Agent not functioning properly. Now uses internal store instead of making a process and using that to put data in store
11+
- Protocol incorrectly handling strings
12+
- `defgen` and `defgenp` functions not being recognized by Elixir compiler.
13+
14+
715
## [0.23.1] - 2016-11-16
816

917
### Fixed

lib/elixir_script/passes/consolidate_protocols.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirScript.Passes.ConsolidateProtocols do
2-
@moduledoc false
2+
@moduledoc false
33
alias ESTree.Tools.Builder, as: JS
44
alias ElixirScript.Translator.Utils
55
alias ElixirScript.ModuleSystems

lib/elixir_script/passes/find_modules.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirScript.Passes.FindModules do
2-
@moduledoc false
2+
@moduledoc false
33
alias ElixirScript.Translator.Utils
44
alias ElixirScript.Translator.State
55

@@ -27,14 +27,14 @@ defmodule ElixirScript.Passes.FindModules do
2727

2828
defp get_defmodules({:defimpl, _, [ the_alias, [for: {:__aliases__, _, type_name} = type], [do: {:__block__, context, spec}] ]} = ast, state, _) do
2929
{:__aliases__, _, original_name} = Utils.name_to_quoted(State.get_module_name(the_alias))
30-
name = original_name ++ [DefImpl] ++ type_name
30+
name = original_name ++ [DefImpl] ++ [Elixir] ++ type_name
3131
s = %{name: Utils.quoted_to_name({:__aliases__, [], name}), type: :impl, for: type, ast: {:__block__, context, spec}, implements: Utils.quoted_to_name({:__aliases__, [], original_name}) }
3232
{ ast, state ++ [s] }
3333
end
3434

3535
defp get_defmodules({:defimpl, _, [ the_alias, [for: {:__aliases__, _, type_name} = type], [do: spec] ]} = ast, state, _) do
3636
{:__aliases__, _, original_name} = Utils.name_to_quoted(State.get_module_name(the_alias))
37-
name = original_name ++ [DefImpl] ++ type_name
37+
name = original_name ++ [DefImpl] ++ [Elixir] ++ type_name
3838
s = %{name: Utils.quoted_to_name({:__aliases__, [], name}), type: :impl, for: type, ast: {:__block__, [], [spec]}, implements: Utils.quoted_to_name({:__aliases__, [], original_name}) }
3939
{ ast, state ++ [s] }
4040
end

lib/elixir_script/passes/java_script_name.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirScript.Passes.JavaScriptName do
2-
@moduledoc false
2+
@moduledoc false
33
alias ElixirScript.Translator.Utils
44

55
def execute(compiler_data, _) do

lib/elixir_script/prelude/agent.ex

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
defmodule ElixirScript.Agent do
2-
@moduledoc false
2+
@moduledoc false
3+
require JS
34

45
def start(fun, options \\ []) do
5-
pid = spawn(fn() -> Process.sleep(:infinity) end)
6+
pid = JS.new Elixir.Core.PID, []
67

7-
if Elixir.Keyword.has_key?(options, :name) do
8-
Process.register(pid, Elixir.Keyword.get(options, :name))
8+
name = if Elixir.Keyword.has_key?(options, :name) do
9+
Elixir.Keyword.get(options, :name)
10+
else
11+
nil
912
end
1013

11-
Elixir.Core.Store.create(pid, fun.())
14+
Elixir.Core.Store.create(pid, fun.(), name)
1215
{ :ok, pid }
1316
end
1417

1518
def start_link(fun, options \\ []) do
16-
pid = spawn_link(fn() -> Process.sleep(:infinity) end)
19+
pid = JS.new Elixir.Core.PID, []
1720

18-
if Elixir.Keyword.has_key?(options, :name) do
19-
Process.register(pid, Elixir.Keyword.get(options, :name))
21+
name = if Elixir.Keyword.has_key?(options, :name) do
22+
Elixir.Keyword.get(options, :name)
23+
else
24+
nil
2025
end
2126

22-
Elixir.Core.Store.create(pid, fun.())
27+
Elixir.Core.Store.create(pid, fun.(), name)
2328
{ :ok, pid }
2429
end
2530

2631
def stop(agent) do
27-
Process.exit(agent)
32+
Elixir.Core.Store.remove(agent)
2833
:ok
2934
end
3035

3136
def update(agent, fun) do
32-
pid = Elixir.Core.processes.pidof(agent)
33-
current_state = Elixir.Core.Store.read(pid)
34-
Elixir.Core.Store.update(pid, fun.(current_state))
37+
current_state = Elixir.Core.Store.read(agent)
38+
Elixir.Core.Store.update(agent, fun.(current_state))
3539
:ok
3640
end
3741

3842
def get(agent, fun) do
39-
pid = Elixir.Core.processes.pidof(agent)
40-
current_state = Elixir.Core.Store.read(pid)
43+
current_state = Elixir.Core.Store.read(agent)
4144
fun.(current_state)
4245
end
4346

4447
def get_and_update(agent, fun) do
45-
pid = Elixir.Core.processes.pidof(agent)
46-
current_state = Elixir.Core.Store.read(pid)
48+
current_state = Elixir.Core.Store.read(agent)
4749
{val, new_state} = fun.(current_state)
48-
Elixir.Core.Store.update(pid, new_state)
50+
Elixir.Core.Store.update(agent, new_state)
4951
val
5052
end
5153

lib/elixir_script/prelude/js.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,18 @@ defmodule JS do
7878
defgen and defgenp are currently the only ways to use process in Elixirscript right now.
7979
"""
8080
defmacro defgen(call, expr \\ nil) do
81+
quote do
82+
def unquote(call), unquote(expr)
83+
end
8184
end
8285

8386
@doc """
8487
Defines a private generator. This is compiled into a generator function in JavaScript.
8588
"""
8689
defmacro defgenp(call, expr \\ nil) do
90+
quote do
91+
defp unquote(call), unquote(expr)
92+
end
8793
end
8894

8995
@doc """

lib/elixir_script/prelude/process.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule ElixirScript.Process do
5757
end
5858

5959
def register(pid, name) when is_atom(name) do
60-
Elixir.Core.processes.register(pid, name)
60+
Elixir.Core.processes.register(name, pid)
6161
end
6262

6363
def registered() do

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ElixirScript.Mixfile do
44
def project do
55
[
66
app: :elixir_script,
7-
version: "0.23.1",
7+
version: "0.23.2-dev",
88
elixir: "~> 1.0",
99
escript: escript_config,
1010
deps: deps,

src/javascript/lib/core/functions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ function maps_from_list(list){
392392
return Object.freeze(m);
393393
}
394394

395+
function* sleep_forever(){
396+
yield* Core.processes.sleep(Symbol("infinity"));
397+
}
398+
395399
export default {
396400
call_property,
397401
apply,
@@ -430,5 +434,6 @@ export default {
430434
duplicate,
431435
mapfoldl,
432436
filtermap,
433-
maps_fold
437+
maps_fold,
438+
sleep_forever
434439
};

src/javascript/lib/core/protocol.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Protocol{
2020
fun = this.registry.get(Core.Integer)[funName];
2121
}else if(typeof thing === "number" && !Number.isInteger(thing) && this.hasImplementation(Core.Float)){
2222
fun = this.registry.get(Core.Float)[funName];
23+
}else if(typeof thing === "string" && this.hasImplementation(Core.BitString)){
24+
fun = this.registry.get(Core.BitString)[funName];
2325
}else if(this.hasImplementation(thing)){
2426
fun = this.registry.get(thing.constructor)[funName];
2527
}else if(this.fallback){
@@ -45,7 +47,7 @@ class Protocol{
4547
}
4648

4749
hasImplementation(thing) {
48-
if (thing === Core.Integer || thing === Core.Float){
50+
if (thing === Core.Integer || thing === Core.Float || Core.BitString){
4951
return this.registry.has(thing);
5052
}
5153

0 commit comments

Comments
 (0)