Skip to content

Commit

Permalink
Support updating Main-included files
Browse files Browse the repository at this point in the history
  • Loading branch information
cstjean committed Jul 11, 2017
1 parent fe39d16 commit ebe0d3e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
21 changes: 13 additions & 8 deletions src/code_update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ function revertible_update_helper(fn)
end
end

""" `update_code_revertible(new_code_fn::Function, mod::Module)` applies
the source code transformation function `new_code_fn` to each expression in the source
code of `mod`, and returns a `RevertibleCodeUpdate` which can put into effect/revert
that new code.
"""
update_code_revertible(new_code_fn::Function, obj::Union{Module, Function, String})
applies the source code transformation function `new_code_fn` to each expression in the
source code of `obj`, and returns a `RevertibleCodeUpdate` which can put into
effect/revert that new code. `obj` can be a module, a function (will transform each
method), or a Main-included ".jl" filename.
`update_code_revertible` itself is side-effect free (it neither modifies the source file,
nor the state of `Julia`). See the README for usage info.
IMPORTANT: if some expression `x` should not be modified, return `nothing` instead of `x`.
This will significantly improve performance. """
Expand All @@ -154,6 +160,9 @@ function update_code_revertible(new_code_fn::Function, mod::Module,
end
end

update_code_revertible(new_code_fn::Function, file::String) =
update_code_revertible(new_code_fn, Main, file)

method_file_counts(fn_to_change) =
counter((mod, file)
# The Set is so that we count methods that have the same file and line number.
Expand All @@ -177,10 +186,6 @@ end
Base.show(io::IO, fail::MissingMethodFailure) =
write(io, "Only $(fail.count)/$(fail.correct_count) methods of $(fail.fn) in $(fail.file) were found.")

""" `update_code_revertible(new_code_fn::Function, fn_to_change::Function)` applies
the source code transformation function `new_code_fn` to the source of each of the
mehods of `fn_to_change`, and returns a `RevertibleCodeUpdate` which can put into
effect/revert that new code. """
function update_code_revertible(new_code_fn::Function,
fn_to_change::Union{Function, Type};
when_missing=warn)
Expand Down
1 change: 1 addition & 0 deletions test/incl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apple() = :orange
12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ a = ClobberingReload.parse_file("docstring.jl")[1]
################################################################################
# RevertibleCodeUpdate

include("incl.jl")

counter = fill(0)
function add_counter(fdef)
di = ClobberingReload.splitdef(fdef)
Expand All @@ -54,6 +56,9 @@ end
upd_module = update_code_revertible(AA) do code
if ClobberingReload.is_function_definition(code) add_counter(code) end
end
upd_include = update_code_revertible("incl.jl") do code
if ClobberingReload.is_function_definition(code) add_counter(code) end
end
@test AA.high(1) == 10
@test counter[] == 0
upd_high() do
Expand All @@ -68,7 +73,12 @@ upd_module() do
@test AA.high(1.0) == 2
end
@test counter[] == 5 # three calls, since `bar` also becomes counting

upd_include() do
@test apple() == :orange
end
apple()
@test counter[] == 6

################################################################################

@test length(source(creload)) == 2
Expand Down

0 comments on commit ebe0d3e

Please sign in to comment.