Skip to content

Commit 0d1b0be

Browse files
committed
Add cleanup intercepts function
This function purges specified target module, which was created as a proxy, and its backuped original module, then reloads original module.
1 parent 0816c31 commit 0d1b0be

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

intercepts/intercept.erl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
%% Export explicit API but also send compile directive to export all
2323
%% because some of these private functions are useful in their own
2424
%% right.
25-
-export([add/3, add/4]).
25+
-export([add/3, add/4, clean/1]).
2626
-compile(export_all).
2727

2828
-type abstract_code() :: term().
@@ -69,6 +69,18 @@ add(Target, Intercept, Mapping, OutDir) ->
6969
add(Target, Intercept, Mapping) ->
7070
add(Target, Intercept, Mapping, undefined).
7171

72+
%% @doc Cleanup proxy and backuped original module
73+
-spec clean(module()) -> ok|{error, term()}.
74+
clean(Target) ->
75+
_ = code:purge(Target),
76+
_ = code:purge(?ORIGINAL(Target)),
77+
case code:load_file(Target) of
78+
{module, Target} ->
79+
ok;
80+
{error, Reason} ->
81+
{error, Reason}
82+
end.
83+
7284
%% @private
7385
%%
7486
%% @doc Compile the abstract code `AC' and load it into the code server.

src/rt_intercept.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ add(Node, {Target, Intercept, Mapping}, OutDir) ->
7878
NMapping = [transform_anon_fun(M) || M <- Mapping],
7979
ok = rpc:call(Node, intercept, add, [Target, Intercept, NMapping, OutDir]).
8080

81+
clean(Node, Targets) when is_list(Targets) ->
82+
[ok = clean(Node, T) || T <- Targets],
83+
ok;
84+
clean(Node, Target) ->
85+
ok = rpc:call(Node, intercept, clean, [Target]).
86+
8187
%% The following function transforms anonymous function mappings passed
8288
%% from an Erlang shell. Anonymous intercept functions from compiled code
8389
%% require the developer to supply free variables themselves, and also

0 commit comments

Comments
 (0)