Skip to content

Commit d4877b9

Browse files
committed
Merge pull request #1975 from pguyot/w46/add-sys
Add `sys` implementation for gleam/otp These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 0090e23 + 3c52776 commit d4877b9

File tree

13 files changed

+1073
-191
lines changed

13 files changed

+1073
-191
lines changed

.github/workflows/check-formatting.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ jobs:
5656
5757
erlfmt-check:
5858
runs-on: ubuntu-24.04
59-
container: erlang:27
59+
container: erlang:28
6060
steps:
6161
- uses: actions/checkout@v4
6262

6363
- name: "Check formatting with Erlang fmt"
6464
run: |
6565
cd ..
66-
git clone --depth 1 -b v1.1.0 https://github.com/WhatsApp/erlfmt.git
66+
git clone --depth 1 -b v1.7.0 https://github.com/WhatsApp/erlfmt.git
6767
cd erlfmt
6868
rebar3 as release escriptize
6969
cd ../AtomVM

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6363
- Added support for big integers in `binary_to_term/1` and `term_to_binary/1,2`
6464
- Added `proc_lib`
6565
- Added gen_server support for timeout tuples in callback return actions introduced in OTP-28.
66+
- Added `sys`
6667

6768
### Changed
6869

libs/eavmlib/src/timer_manager.erl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,9 @@ start() ->
3939

4040
-spec maybe_start() -> {ok, Pod :: pid()}.
4141
maybe_start() ->
42-
case erlang:whereis(?SERVER_NAME) of
43-
undefined ->
44-
case start() of
45-
{ok, _Pid} = R -> R;
46-
{error, {already_started, Pid}} -> {ok, Pid}
47-
end;
48-
Pid when is_pid(Pid) ->
49-
{ok, Pid}
42+
case start() of
43+
{ok, _Pid} = R -> R;
44+
{error, {already_started, Pid}} -> {ok, Pid}
5045
end.
5146

5247
%%-----------------------------------------------------------------------------

libs/estdlib/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(ERLANG_MODULES
3636
erts_debug
3737
ets
3838
file
39+
gen
3940
gen_event
4041
gen_server
4142
gen_statem
@@ -58,6 +59,7 @@ set(ERLANG_MODULES
5859
math
5960
net
6061
proc_lib
62+
sys
6163
file
6264
logger
6365
logger_std_h

libs/estdlib/src/gen.erl

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2025 Paul Guyot <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
%%-----------------------------------------------------------------------------
22+
%% This module implements common code in gen_* modules, following what
23+
%% Erlang/OTP does with gen module. However, none of the functions exported
24+
%% here are public interface.
25+
%%-----------------------------------------------------------------------------
26+
27+
-module(gen).
28+
-moduledoc false.
29+
30+
-export([
31+
call/4,
32+
cast/2,
33+
reply/2
34+
]).
35+
36+
-type server_ref() :: atom() | pid().
37+
-type from() :: {pid(), reference()}.
38+
39+
%% @private
40+
-spec call(ServerRef :: server_ref(), Label :: atom(), Request :: term(), Timeout :: timeout()) ->
41+
Reply :: term() | {error, Reason :: term()}.
42+
call(ServerRef, Label, Request, Timeout) ->
43+
MonitorRef = monitor(process, ServerRef),
44+
ok =
45+
try
46+
ServerRef ! {Label, {self(), MonitorRef}, Request},
47+
ok
48+
catch
49+
error:badarg ->
50+
% Process no longer exists, monitor will send a message
51+
ok
52+
end,
53+
receive
54+
{'DOWN', MonitorRef, process, _, {E, []} = _Reason} ->
55+
exit(E);
56+
{'DOWN', MonitorRef, process, _, {_E, _L} = Reason} ->
57+
exit(Reason);
58+
{'DOWN', MonitorRef, process, _, Atom} when is_atom(Atom) ->
59+
exit(Atom);
60+
{MonitorRef, Reply} ->
61+
demonitor(MonitorRef, [flush]),
62+
Reply
63+
after Timeout ->
64+
% If Timeout is small enough (0), the error message might be timeout
65+
% instead of noproc as there could be a race condition with the monitor.
66+
demonitor(MonitorRef, [flush]),
67+
exit(timeout)
68+
end.
69+
70+
%% @private
71+
-spec cast(ServerRef :: server_ref(), Message :: any()) -> ok.
72+
cast(ServerRef, Message) ->
73+
try
74+
ServerRef ! {'$gen_cast', Message},
75+
ok
76+
catch
77+
error:_ ->
78+
% Process does not exist, ignore error
79+
ok
80+
end.
81+
82+
%% @private
83+
-spec reply(From :: from(), Reply :: any()) -> ok.
84+
reply({Pid, Ref}, Reply) ->
85+
try
86+
Pid ! {Ref, Reply},
87+
ok
88+
catch
89+
_:_ ->
90+
ok
91+
end.

0 commit comments

Comments
 (0)