<commit_before>-module(xprof_vm_info).

-export([get_available_funs/1]).

%% @doc Return list of existing module/funcion/arity that match query.
%%
%% If query does not contain colon only the global functions
%% (excluding module_info) of all the matching modules are listed.
%% If query contains colon both the global and local functions
%% (excluding generated local functions) are listed of that particular module
%% matching the query.

-spec get_available_funs(binary()) -> [MFA]
  when MFA :: list(). %% [module(), atom(), arity()]
get_available_funs(Query) ->
    case binary:split(Query, <<":">>) of
        [ModBin, Rest] ->
            case is_module(ModBin) of
                {true, Mod} ->
                    Funs = get_global_functions(Mod) ++ get_local_functions(Mod),
                    [[Mod, Fun, Arity]
                     || {Fun, Arity} <- filter_funs(binary_to_list(Rest), Funs)];
                false ->
                    []
            end;
        [ModPrefix] ->
            [[Mod, Fun, Arity]
             || Mod <- filter_mods(binary_to_list(ModPrefix), get_modules()),
                {Fun, Arity} <- get_global_functions(Mod)]
    end.

filter_funs(Prefix, Funs) ->
    lists:filter(fun({Fun, Arity}) ->
                         Str = lists:flatten(
                                 [atom_to_list(Fun), $/, integer_to_list(Arity)]),
                         lists:prefix(Prefix, Str)
                 end, Funs).

filter_mods(Prefix, Mods) ->
    lists:filter(fun(Mod) ->
                         Str = atom_to_list(Mod),
                         lists:prefix(Prefix, Str)
                 end, Mods).

-spec get_global_functions(module()) -> [{atom(), arity()}].
get_global_functions(Mod) ->
    [FA || FA = {F, _} <- Mod:module_info(exports), F =/= module_info].

-spec get_local_functions(module()) -> [{atom(), arity()}].
get_local_functions(Mod) ->
    case code:which(Mod) of
        Error when is_atom(Error) ->
            %% non_existing | preloaded | cover_compiled ... who knows what else
            [];
        File ->
            {ok, {Mod, [{locals, Locals}]}} = beam_lib:chunks(File, [locals]),
            %% filter out functions generated for fun objects
            %% and list comprehensions like '-filter_funs/2-fun-0-'
            [FA || FA = {F, _} <- Locals, not lists:prefix("-", atom_to_list(F))]
    end.

is_module(ModBin) ->
    try list_to_existing_atom(binary_to_list(ModBin)) of
        Mod ->
            case code:is_loaded(Mod) of
                {file, _} -> {true, Mod};
                false -> false
            end
    catch error:badarg -><commit_msg>Simplify listing local functions<commit_after>-module(xprof_vm_info).

-export([get_available_funs/1]).

%% @doc Return list of existing module/funcion/arity that match query.
%%
%% If query does not contain colon only the global functions
%% (excluding module_info) of all the matching modules are listed.
%% If query contains colon both the global and local functions
%% (excluding generated local functions) are listed of that particular module
%% matching the query.

-spec get_available_funs(binary()) -> [MFA]
  when MFA :: list(). %% [module(), atom(), arity()]
get_available_funs(Query) ->
    case binary:split(Query, <<":">>) of
        [ModBin, Rest] ->
            case is_module(ModBin) of
                {true, Mod} ->
                    Funs = get_all_functions(Mod),
                    [[Mod, Fun, Arity]
                     || {Fun, Arity} <- filter_funs(binary_to_list(Rest), Funs)];
                false ->
                    []
            end;
        [ModPrefix] ->
            [[Mod, Fun, Arity]
             || Mod <- filter_mods(binary_to_list(ModPrefix), get_modules()),
                {Fun, Arity} <- get_global_functions(Mod)]
    end.

filter_funs(Prefix, Funs) ->
    lists:filter(fun({Fun, Arity}) ->
                         Str = lists:flatten(
                                 [atom_to_list(Fun), $/, integer_to_list(Arity)]),
                         lists:prefix(Prefix, Str)
                 end, Funs).

filter_mods(Prefix, Mods) ->
    lists:filter(fun(Mod) ->
                         Str = atom_to_list(Mod),
                         lists:prefix(Prefix, Str)
                 end, Mods).

-spec get_global_functions(module()) -> [{atom(), arity()}].
get_global_functions(Mod) ->
    [FA || FA = {F, _} <- Mod:module_info(exports), F =/= module_info].

-spec get_all_functions(module()) -> [{atom(), arity()}].
get_all_functions(Mod) ->
    %% filter out functions generated for fun objects
    %% and list comprehensions like '-filter_funs/2-fun-0-'
    [FA || FA = {F, _} <- Mod:module_info(functions), not lists:prefix("-", atom_to_list(F))].

is_module(ModBin) ->
    try list_to_existing_atom(binary_to_list(ModBin)) of
        Mod ->
            case code:is_loaded(Mod) of
                {file, _} -> {true, Mod};
                false -> false
            end
    catch error:badarg ->
{'fraction_non_alphanumeric': 0.1402696719661904, 'fraction_numerical': 0.0012074864157778226, 'mean_word_length': 2.0793060718711276, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1963797', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1464, 'n_words': 359}