Skip to content

Commit

Permalink
Fix exception classes in estdlib
Browse files Browse the repository at this point in the history
Replace all erroneous uses of throw with calls to `error/1`.
Also update etest to properly test for exception classes, thus asserting the
compatibility with Erlang/OTP
Also remove duplicate etest.hrl header

Signed-off-by: Paul Guyot <[email protected]>
  • Loading branch information
pguyot committed Oct 28, 2023
1 parent 11afecd commit 577da18
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 204 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed support for big endian CPUs (such as some MIPS CPUs).
- Fixed STM32 not aborting when `AVM_ABORT()` is used
- Fixed a bug that would leave the STM32 trapped in a loop on hard faults, rather than aborting
- Fixed classes of exceptions in estdlib.

### Added

Expand Down
13 changes: 5 additions & 8 deletions libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ apply(Module, Function, Args) ->
[Arg1, Arg2, Arg3, Arg4, Arg5, Arg6] ->
Module:Function(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
_ ->
throw(badarg)
error(badarg)
end.

%%-----------------------------------------------------------------------------
Expand All @@ -356,10 +356,9 @@ is_map(_Map) ->
%%-----------------------------------------------------------------------------
%% @param Map the map
%% @returns the size of the map
%% @throws {badmap, Map}
%% @doc Returns the size of (i.e., the number of entries in) the map
%%
%% This function throws a `{badmap, Map}' exception if `Map' is not a map.
%% This function raises a `{badmap, Map}' error if `Map' is not a map.
%%
%% This function may be used in a guard expression.
%% @end
Expand All @@ -372,11 +371,10 @@ map_size(_Map) ->
%% @param Key the key to get
%% @param Map the map from which to get the value
%% @returns the value in `Map' associated with `Key', if it exists.
%% @throws {badkey, Key} | {badmap, Map}
%% @doc Get the value in `Map' associated with `Key', if it exists.
%%
%% This function throws a `{badkey, Key}' exception if 'Key' does not occur in `Map' or
%% a `{badmap, Map}' if `Map' is not a map.
%% This function raises a `{badkey, Key}' error if 'Key' does not occur in
%% `Map' or a `{badmap, Map}' if `Map' is not a map.
%%
%% This function may be used in a guard expression.
%% @end
Expand All @@ -389,10 +387,9 @@ map_get(_Key, _Map) ->
%% @param Key the key
%% @param Map the map
%% @returns `true' if `Key' is associated with a value in `Map'; `false', otherwise.
%% @throws {badmap, Map}
%% @doc Return `true' if `Key' is associated with a value in `Map'; `false', otherwise.
%%
%% This function throws a `{badmap, Map}' exception if `Map' is not a map.
%% This function raises a `{badmap, Map}' error if `Map' is not a map.
%%
%% This function may be used in a guard expression.
%% @end
Expand Down
16 changes: 8 additions & 8 deletions libs/estdlib/src/io_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ format(Format, Args) ->
true ->
interleave(FormatTokens, Instr, Args, []);
false ->
throw(badarg)
error(badarg)
end.

%%
Expand Down Expand Up @@ -145,7 +145,7 @@ parse_format_control([$+ | Rest], Format) -> {Format#format{control = '+'}, Rest
parse_format_control([$e | Rest], Format) -> {Format#format{control = e}, Rest};
parse_format_control([$f | Rest], Format) -> {Format#format{control = f}, Rest};
parse_format_control([$g | Rest], Format) -> {Format#format{control = g}, Rest};
parse_format_control(_String, _Format) -> throw({badarg, _String}).
parse_format_control(_String, _Format) -> error({badarg, _String}).

%% @private
parse_integer([$- | Tail]) ->
Expand Down Expand Up @@ -251,8 +251,8 @@ format_spw(#format{control = Control, mod = undefined}, T) when is_binary(T) ->
format_spw(#format{control = s, mod = Mod}, L) when is_list(L) ->
Flatten = lists:flatten(L),
case {Mod, test_string_class(Flatten)} of
{_, not_a_string} -> throw(badarg);
{undefined, unicode} -> throw(badarg);
{_, not_a_string} -> error(badarg);
{undefined, unicode} -> error(badarg);
{_, _} -> Flatten
end;
format_spw(#format{control = p} = Format, L) when is_list(L) ->
Expand All @@ -263,7 +263,7 @@ format_spw(#format{control = p} = Format, L) when is_list(L) ->
format_spw(#format{control = w} = Format, L) when is_list(L) ->
[$[, lists:join($,, [format_spw(Format, E) || E <- L]), $]];
format_spw(#format{control = s}, _) ->
throw(badarg);
error(badarg);
format_spw(_Format, T) when is_integer(T) ->
erlang:integer_to_list(T);
format_spw(_Format, T) when is_float(T) ->
Expand Down Expand Up @@ -317,7 +317,7 @@ format_integer(#format{control = 'B', precision = Base}, T) when is_integer(T) -
format_integer(#format{control = b, precision = Base}, T) when is_integer(T) ->
string:to_lower(integer_to_list(T, Base));
format_integer(_Format, _) ->
throw(badarg).
error(badarg).

%% @private
format_float(#format{control = f, precision = undefined}, T) when is_float(T) ->
Expand All @@ -341,7 +341,7 @@ format_float(#format{control = C, precision = Precision}, T) when
->
format_scientific(T, Precision, 0);
format_float(_Format, _) ->
throw(badarg).
error(badarg).

%% @private
format_scientific(T, Precision, E) when (T < 1 andalso T > 0) orelse (T > -1 andalso T < 0) ->
Expand All @@ -366,7 +366,7 @@ format_char(#format{precision = Precision} = Format, T) when Precision =/= undef
[Ch] = format_char(Format#format{field_width = undefined, precision = undefined}, T),
lists:duplicate(Precision, Ch);
format_char(_, _) ->
throw(badarg).
error(badarg).

%% @private
%% String classes:
Expand Down
2 changes: 1 addition & 1 deletion libs/estdlib/src/lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ seq(From, To, Incr) when
(To > (From - Incr) andalso Incr < 0) orelse
(Incr =:= 0 andalso From =/= To)
->
throw(badarg);
error(badarg);
seq(To, To, 0) ->
[To];
seq(From, To, Incr) ->
Expand Down
4 changes: 1 addition & 3 deletions libs/estdlib/src/logger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,7 @@ validate_level(Level) when
Level == info orelse
Level == debug
->
Level;
validate_level(Level) ->
throw({unsupported_level, Level}).
Level.

%% @private
to_int_level(emergency) ->
Expand Down
Loading

0 comments on commit 577da18

Please sign in to comment.