Skip to content

Instantly share code, notes, and snippets.

@stolen
Created February 13, 2012 16:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolen/1817904 to your computer and use it in GitHub Desktop.
Save stolen/1817904 to your computer and use it in GitHub Desktop.
Erlang module alias generator
{application, modalias,
[{vsn, "1.0"},
{modules, [modalias]},
{applications, [stdlib, kernel, sasl]}]
}.
-module(modalias).
-export([new/2]).
new(Origin, Alias) ->
Exports = lists:filter(fun ({module_info, _}) -> false;
(_) -> true end, Origin:module_info(exports)),
Abstract = [
{attribute, 1, module, Alias},
{attribute, 2, export, Exports}
]
++ lists:map(fun ({FunName, Arity}) ->
alias_fun(Origin, FunName, Arity)
end, Exports),
{ok, Alias, AliasBin} = compile:forms(Abstract),
{module, Alias} = code:load_binary(Alias, "/dev/null", AliasBin),
{ok, Alias}.
%% @doc Create abstract form of alias function
alias_fun(Origin, FunName, Arity) ->
ArgList = gen_arglist(Arity),
{function, 3, FunName, Arity, [
{clause, 3, ArgList, [],
[{call, 4,
{remote, 4, {atom, 4, Origin}, {atom, 4, FunName}},
ArgList
}]
}]
}.
gen_arglist(Arity) ->
[ {var, 3, list_to_atom("Arg" ++ integer_to_list(N))} || N <- lists:seq(1, Arity)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment