Skip to content

Commit

Permalink
Fix and factorize spawn functions
Browse files Browse the repository at this point in the history
Implement `erlang:spawn_link/1,3`
Fix options of `spawn_opt/2` by factorizing it with `spawn_opt/4`
Also rewrite `spawn/1` and `spawn/3` in Erlang and update tests accordingly
Also isolate libs tests
Also move types declaration in erlang module to the top

Signed-off-by: Paul Guyot <[email protected]>
  • Loading branch information
pguyot committed Sep 3, 2023
1 parent 99ab615 commit 261a031
Show file tree
Hide file tree
Showing 66 changed files with 482 additions and 337 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ functions that default to `?ATOMVM_NVS_NS` are deprecated now).
- Added most format possibilities to `io:format/2` and `io_lib:format/2`
- Added `unicode` module with `characters_to_list/1,2` and `characters_to_binary/1,2,3` functions
- Added support for `crypto:hash/2` (ESP32 and generic_unix with openssl)
- Added erlang:spawn_link/1,3
- Added links to process_info/2

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion examples/emscripten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ project(examples_emscripten)

include(BuildErlang)

pack_runnable(run_script run_script eavmlib)
pack_runnable(run_script run_script estdlib eavmlib)
pack_runnable(call_cast call_cast eavmlib)
pack_runnable(html5_events html5_events estdlib eavmlib)
pack_runnable(wasm_webserver wasm_webserver estdlib eavmlib)
59 changes: 42 additions & 17 deletions libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
whereis/1,
spawn/1,
spawn/3,
spawn_link/1,
spawn_link/3,
spawn_opt/2,
spawn_opt/4,
link/1,
Expand Down Expand Up @@ -119,6 +121,19 @@
MegaSecs :: non_neg_integer(), Secs :: non_neg_integer(), MicroSecs :: non_neg_integer
}.

-type float_format_option() ::
{decimals, Decimals :: 0..57}
| {scientific, Decimals :: 0..57}
| compact.

-type demonitor_option() :: flush | {flush, boolean()} | info | {info, boolean()}.

-type spawn_option() ::
{min_heap_size, pos_integer()}
| {max_heap_size, pos_integer()}
| link
| monitor.

%%-----------------------------------------------------------------------------
%% @param Time time in milliseconds after which to send the timeout message.
%% @param Dest Pid or server name to which to send the timeout message.
Expand Down Expand Up @@ -612,11 +627,6 @@ atom_to_binary(_Atom, _Encoding) ->
atom_to_list(_Atom) ->
erlang:nif_error(undefined).

-type float_format_option() ::
{decimals, Decimals :: 0..57}
| {scientific, Decimals :: 0..57}
| compact.

%%-----------------------------------------------------------------------------
%% @param Float Float to convert
%% @returns a binary with a text representation of the float
Expand Down Expand Up @@ -774,8 +784,8 @@ whereis(_Name) ->
%% @end
%%-----------------------------------------------------------------------------
-spec spawn(Function :: function()) -> pid().
spawn(_Name) ->
erlang:nif_error(undefined).
spawn(Function) ->
erlang:spawn_opt(Function, []).

%%-----------------------------------------------------------------------------
%% @param Module module of the function to create a process from
Expand All @@ -786,14 +796,31 @@ spawn(_Name) ->
%% @end
%%-----------------------------------------------------------------------------
-spec spawn(Module :: module(), Function :: atom(), Args :: [any()]) -> pid().
spawn(_Module, _Function, _Args) ->
erlang:nif_error(undefined).
spawn(Module, Function, Args) ->
erlang:spawn_opt(Module, Function, Args, []).

-type spawn_option() ::
{min_heap_size, pos_integer()}
| {max_heap_size, pos_integer()}
| link
| monitor.
%%-----------------------------------------------------------------------------
%% @param Function function to create a process from
%% @returns pid of the new process
%% @doc Create a new process and link it.
%% @end
%%-----------------------------------------------------------------------------
-spec spawn_link(Function :: function()) -> pid().
spawn_link(Function) ->
erlang:spawn_opt(Function, [link]).

%%-----------------------------------------------------------------------------
%% @param Module module of the function to create a process from
%% @param Function name of the function to create a process from
%% @param Args arguments to pass to the function to create a process from
%% @returns pid of the new process
%% @doc Create a new process by calling exported Function from Module with Args
%% and link it.
%% @end
%%-----------------------------------------------------------------------------
-spec spawn_link(Module :: module(), Function :: atom(), Args :: [any()]) -> pid().
spawn_link(Module, Function, Args) ->
erlang:spawn_opt(Module, Function, Args, [link]).

%%-----------------------------------------------------------------------------
%% @param Function function to create a process from
Expand All @@ -802,7 +829,7 @@ spawn(_Module, _Function, _Args) ->
%% @doc Create a new process.
%% @end
%%-----------------------------------------------------------------------------
-spec spawn_opt(Function :: function(), Options :: [{max_heap_size, integer()}]) ->
-spec spawn_opt(Function :: function(), Options :: [spawn_option()]) ->
pid() | {pid(), reference()}.
spawn_opt(_Name, _Options) ->
erlang:nif_error(undefined).
Expand Down Expand Up @@ -889,8 +916,6 @@ monitor(_Type, _Pid) ->
demonitor(_Monitor) ->
erlang:nif_error(undefined).

-type demonitor_option() :: flush | {flush, boolean()} | info | {info, boolean()}.

%%-----------------------------------------------------------------------------
%% @param Monitor reference of monitor to remove
%% @returns `true'
Expand Down
27 changes: 27 additions & 0 deletions libs/etest/src/etest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,35 @@ assert_failure(F, E) ->

%% @private
run_test(Test) ->
Parent = self(),
{Pid, Ref} = spawn_opt(
fun() ->
Result = do_run_test(Test),
Parent ! {self(), Result}
end,
[monitor]
),
receive
{Pid, Result} ->
receive
{'DOWN', Ref, process, Pid, normal} -> ok
after 0 -> ok
end,
Result;
{'DOWN', Ref, process, Pid, Reason} ->
{error, Reason}
end.

do_run_test(Test) ->
try
Result = Test:test(),
Value = process_flag(trap_exit, false),
case Value of
true ->
erlang:display({test, Test, unexpected_trap_exit});
false ->
ok
end,
case erlang:system_info(machine) of
"BEAM" ->
io:format("+");
Expand Down
Loading

0 comments on commit 261a031

Please sign in to comment.