Skip to content

Support GenServer.ex & Supervisor.ex #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `erlang:module_loaded/1`
- Added `binary:replace/3`, `binary:replace/4`
- Added `binary:match/2` and `binary:match/3`
- Added `supervisor:which_children/1`
- Added support for Elixir GenServer and Supervisor.

### Changed

Expand Down
39 changes: 33 additions & 6 deletions libs/estdlib/src/supervisor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
start_child/2,
terminate_child/2,
restart_child/2,
delete_child/2
delete_child/2,
which_children/1
]).

-export([
Expand Down Expand Up @@ -86,7 +87,8 @@
start :: {module(), atom(), [any()] | undefined},
restart :: restart(),
shutdown :: shutdown(),
type :: child_type
type :: child_type,
modules = [] :: [module()] | dynamic
}).
-record(state, {restart_strategy :: strategy(), children = [] :: [#child{}]}).

Expand All @@ -107,6 +109,9 @@ restart_child(Supervisor, ChildId) ->
delete_child(Supervisor, ChildId) ->
gen_server:call(Supervisor, {delete_child, ChildId}).

which_children(Supervisor) ->
gen_server:call(Supervisor, which_children).

init({Mod, Args}) ->
erlang:process_flag(trap_exit, true),
case Mod:init(Args) of
Expand All @@ -123,13 +128,14 @@ init({Mod, Args}) ->
end.

-spec child_spec_to_record(child_spec()) -> #child{}.
child_spec_to_record({ChildId, MFA, Restart, Shutdown, Type, _Modules}) ->
child_spec_to_record({ChildId, MFA, Restart, Shutdown, Type, Modules}) ->
#child{
id = ChildId,
start = MFA,
restart = Restart,
shutdown = Shutdown,
type = Type
type = Type,
modules = Modules
};
child_spec_to_record(#{id := ChildId, start := MFA} = ChildMap) ->
Restart = maps:get(restart, ChildMap, permanent),
Expand All @@ -142,12 +148,21 @@ child_spec_to_record(#{id := ChildId, start := MFA} = ChildMap) ->
supervisor -> infinity
end
),
Modules = maps:get(
modules,
ChildMap,
case MFA of
{M, _, _} -> [M];
_ -> []
end
),
#child{
id = ChildId,
start = MFA,
restart = Restart,
shutdown = Shutdown,
type = Type
type = Type,
modules = Modules
}.

init_state([ChildSpec | T], State) ->
Expand Down Expand Up @@ -262,7 +277,10 @@ handle_call({delete_child, ID}, _From, #state{children = Children} = State) ->
{reply, {error, running}, State};
false ->
{reply, {error, not_found}, State}
end.
end;
handle_call(which_children, _From, #state{children = Children} = State) ->
ChildrenInfo = lists:map(fun child_to_info/1, Children),
{reply, ChildrenInfo, State}.

handle_cast(_Msg, State) ->
{noreply, State}.
Expand Down Expand Up @@ -337,6 +355,15 @@ try_start(#child{start = {M, F, Args}} = Record) ->
{error, {{'EXIT', Error}, Record}}
end.

child_to_info(#child{id = Id, pid = Pid, type = Type, modules = Modules}) ->
Child =
case Pid of
undefined -> undefined;
{restarting, _} -> restarting;
_ when is_pid(Pid) -> Pid
end,
{Id, Child, Type, Modules}.

do_terminate(#child{pid = Pid, shutdown = brutal_kill}) ->
exit(Pid, kill);
do_terminate(#child{pid = Pid, shutdown = infinity}) ->
Expand Down
4 changes: 4 additions & 0 deletions libs/exavmlib/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ set(ELIXIR_MODULES
Enumerable.Range
Exception
Function
GenServer
IO
List
Map
MapSet
Module
Keyword
Supervisor
Supervisor.Default
Supervisor.Spec
Kernel
Process
Protocol
Expand Down
Loading
Loading