Hello!
I’m trying to retrieve functions arguments via AST, but the method I’m using does not always return what I need.
Example 1:
example_1_test() ->
Expr = "fun() -> fun(<<\"foo\">>, Bar, <<\"foobar\">>) -> Bar end end.",
{ok, Tokens, _} = erl_scan:string(Expr),
{ok, Exprs} = erl_parse:parse_exprs(Tokens),
{value, Fun, _} = erl_eval:exprs(Exprs, []),
FunInfo = erlang:fun_info(Fun),
?debugFmt("[EXAMPLE 1]~n~p~n", [FunInfo]).
$ [EXAMPLE 1]
[{pid,<0.184.0>},
{module,erl_eval},
{new_index,43},
{new_uniq,<<6,83,97,170,70,21,144,209,189,241,235,98,209,166,235,8>>},
{index,43},
{uniq,3316493},
{name,'-expr/6-fun-2-'},
{arity,0},
{env,
[{1,[],none,none,
#{[{clause,1,
[{bin,1,[{bin_element,1,{string,1,"foo"},default,default}]},
{var,1,'Bar'},
{bin,1,[{bin_element,1,{string,1,"foobar"},default,default}]}],
[],
[{var,1,'Bar'}]}] =>
{[],#{}}},
[{clause,1,[],[],
[{'fun',1,
{clauses,
[{clause,1,
[{bin,1,
[{bin_element,1,
{string,1,"foo"},
default,default}]},
{var,1,'Bar'},
{bin,1,
[{bin_element,1,
{string,1,"foobar"},
default,default}]}],
[],
[{var,1,'Bar'}]}]}}]}]}]},
{type,local}]
The env
gives me the function clauses. This is enough for me to get the function args/variables.
Example 2:
example_2_test() ->
Expr = "mymodule:foo().",
{ok, Tokens, _} = erl_scan:string(Expr),
{ok, Exprs} = erl_parse:parse_exprs(Tokens),
{value, Fun, _} = erl_eval:exprs(Exprs, []),
FunInfo = erlang:fun_info(Fun),
?debugFmt("[EXAMPLE 2]~n~p~n", [FunInfo]).
foo() -> fun(<<"foo">>, Bar, <<"foobar">>) -> Bar end.
$ [EXAMPLE 2]
[{pid,<0.184.0>},
{module,mymodule},
{new_index,1},
{new_uniq,<<72,79,143,91,217,115,178,85,239,21,168,227,86,130,132,25>>},
{index,1},
{uniq,37911674},
{name,'-foo/0-fun-0-'},
{arity,3},
{env,[]},
{type,local}]
This didn’t give me any information about clauses of the nested function.
Is there a way to get this information?
I need this information for the eel lib.
Here is my implementation for the first example, but this is not always true because of the second one.
Thanks in advance!