Skip to content

Handle built-in protocol aliases #9718

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions erts/emulator/nifs/common/prim_socket_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -5088,8 +5088,7 @@ ERL_NIF_TERM esock_supports_options(ErlNifEnv* env)
}
levels =
MKC(env,
MKT2(env,
esock_encode_level(env, levelP->level), options),
MKT3(env, *levelP->nameP, MKI(env, levelP->level), options),
levels);
}

Expand Down
Binary file modified erts/preloaded/ebin/prim_socket.beam
Binary file not shown.
89 changes: 44 additions & 45 deletions erts/preloaded/src/prim_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,44 @@ on_load(Extra) when is_map(Extra) ->
init().

init() ->
PT =
put_supports_table(protocols,
fun (Protocols) -> protocols_table(Protocols) end),
_ = put_supports_table(options,
fun (Options) -> options_table(Options, PT) end),
_ = put_supports_table(ioctl_requests,
fun (Requests) -> Requests end),
_ = put_supports_table(ioctl_flags, fun (Flags) -> Flags end),
_ = put_supports_table(msg_flags, fun (Flags) -> Flags end),
put_supports_table(protocols,
fun (Protocols) -> protocols_table(Protocols) end),
put_supports_table(options,
fun (Options) -> options_table(Options) end),
put_supports_table(ioctl_requests,
fun (Requests) -> Requests end),
put_supports_table(ioctl_flags, fun (Flags) -> Flags end),
put_supports_table(msg_flags, fun (Flags) -> Flags end),
ok.

put_supports_table(Tag, MkTable) ->
Table =
try nif_supports(Tag) of
Data ->
maps:from_list(MkTable(Data))
merge_values(MkTable(Data))
catch
error : notsup ->
#{}
end,
p_put(Tag, Table),
Table.
p_put(Tag, Table).

%% Like maps:from_list/1 the last duplicate key wins,
%% except if both values are lists; append the second to the first.
%%
merge_values([]) -> #{};
merge_values([{Key, Val} | L]) ->
M = merge_values(L),
case M of
#{ Key := Val2 } ->
if
is_list(Val), is_list(Val2) ->
M#{ Key := Val ++ Val2 };
true ->
M
end;
#{} ->
M#{ Key => Val }
end.

%% ->
%% [{Num, [Name, ...]}
Expand All @@ -201,41 +217,24 @@ protocols_table(Protocols, [], _Num) ->
protocols_table(Protocols).

%% ->
%% [{{socket,Opt}, {socket,OptNum}} |
%% {{Level, Opt}, {LevelNum, OptNum}} for all Levels (protocol aliases)]
options_table([], _PT) ->
%% [{{socket,Opt}, {socket,OptNum} | undefined} |
%% {{Level, Opt}, {LevelNum, OptNum} | undefined}]
options_table([]) ->
[];
options_table([{socket, LevelOpts} | Options], PT) ->
options_table(Options, PT, socket, LevelOpts, [socket]);
options_table([{LevelNum, LevelOpts} | Options], PT) ->
Levels = maps:get(LevelNum, PT),
options_table(Options, PT, LevelNum, LevelOpts, Levels).
%%
options_table(Options, PT, _Level, [], _Levels) ->
options_table(Options, PT);
options_table(Options, PT, Level, [LevelOpt | LevelOpts], Levels) ->
LevelOptNum =
case LevelOpt of
{Opt, OptNum} ->
{Level,OptNum};
Opt when is_atom(Opt) ->
undefined
end,
options_table(
Options, PT, Level, LevelOpts, Levels,
Opt, LevelOptNum, Levels).
options_table([{socket = Level, _LevelNum, LevelOpts} | Options]) ->
options_table(Options, Level, Level, LevelOpts);
options_table([{Level, LevelNum, LevelOpts} | Options]) ->
options_table(Options, Level, LevelNum, LevelOpts).
%%
options_table(
Options, PT, Level, LevelOpts, Levels,
_Opt, _LevelOptNum, []) ->
options_table(Options, PT, Level, LevelOpts, Levels);
options_table(
Options, PT, Level, LevelOpts, Levels,
Opt, LevelOptNum, [L | Ls]) ->
[{{L,Opt}, LevelOptNum} |
options_table(
Options, PT, Level, LevelOpts, Levels,
Opt, LevelOptNum, Ls)].
options_table(Options, _Level, _LevelNum, []) ->
options_table(Options);
options_table(Options, Level, LevelNum, [LevelOpt | LevelOpts]) ->
[case LevelOpt of
{Opt, OptNum} ->
{{Level, Opt}, {LevelNum,OptNum}};
Opt when is_atom(Opt) ->
{{Level, Opt}, undefined}
end | options_table(Options, Level, LevelNum, LevelOpts)].

%% ===========================================================================
%% API for 'socket'
Expand Down
Loading