Skip to content

Commit

Permalink
allow setting breakpoint on type constructors (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored Mar 8, 2022
1 parent cb2df12 commit 810e49e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/breakpoints.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Base: Callable

const _breakpoints = AbstractBreakpoint[]

"""
Expand Down Expand Up @@ -111,10 +113,10 @@ end
breakpoint(radius2, Tuple{Int,Int}, :(y > x))
```
"""
function breakpoint(f::Union{Method, Function}, sig=nothing, line::Integer=0, condition::Condition=nothing)
if sig !== nothing && f isa Function
function breakpoint(f::Union{Method, Callable}, sig=nothing, line::Integer=0, condition::Condition=nothing)
if sig !== nothing && f isa Callable
sig = Base.to_tuple_type(sig)
sig = Tuple{typeof(f), sig.parameters...}
sig = Tuple{_Typeof(f), sig.parameters...}
end
bp = BreakpointSignature(f, sig, line, condition, Ref(true), BreakpointRef[])
add_to_existing_framecodes(bp)
Expand All @@ -129,9 +131,9 @@ function breakpoint(f::Union{Method, Function}, sig=nothing, line::Integer=0, co
firehooks(breakpoint, bp)
return bp
end
breakpoint(f::Union{Method, Function}, sig, condition::Condition) = breakpoint(f, sig, 0, condition)
breakpoint(f::Union{Method, Function}, line::Integer, condition::Condition=nothing) = breakpoint(f, nothing, line, condition)
breakpoint(f::Union{Method, Function}, condition::Condition) = breakpoint(f, nothing, 0, condition)
breakpoint(f::Union{Method, Callable}, sig, condition::Condition) = breakpoint(f, sig, 0, condition)
breakpoint(f::Union{Method, Callable}, line::Integer, condition::Condition=nothing) = breakpoint(f, nothing, line, condition)
breakpoint(f::Union{Method, Callable}, condition::Condition) = breakpoint(f, nothing, 0, condition)


"""
Expand Down
4 changes: 2 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,14 @@ A `BreakpointSignature` is a breakpoint that is set on methods or functions.
Fields:
- `f::Union{Method, Function}`: A method or function that the breakpoint should apply to.
- `f::Union{Method, Function, Type}`: A method or function that the breakpoint should apply to.
- `sig::Union{Nothing, Type}`: if `f` is a `Method`, always equal to `nothing`. Otherwise, contains the method signature
as a tuple type for what methods the breakpoint should apply to.
For common fields shared by all breakpoints, see [`AbstractBreakpoint`](@ref).
"""
struct BreakpointSignature <: AbstractBreakpoint
f::Union{Method, Function}
f::Union{Method, Base.Callable}
sig::Union{Nothing, Type}
line::Int # 0 is a sentinel for first statement
condition::Condition
Expand Down
16 changes: 16 additions & 0 deletions test/breakpoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,19 @@ end
var = JuliaInterpreter.locals(leaf(frame))
@test filter(v->v.name === :args, var)[1].value == (1,3)
end

struct Constructor
x::Int
end
Constructor(x::AbstractString, y::Int) = Constructor(x)

@testset "constructors" begin
breakpoint(Constructor, Tuple{String, Int})
frame, bp = @interpret Constructor("foo", 3)
@test bp isa BreakpointRef
@test @interpret Constructor(3) isa Constructor

breakpoint(Constructor)
frame, bp = @interpret Constructor(2)
@test bp isa BreakpointRef
end

0 comments on commit 810e49e

Please sign in to comment.