-
Notifications
You must be signed in to change notification settings - Fork 13
/
patch.jl
153 lines (123 loc) · 3.64 KB
/
patch.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# `target` will typically a `Function` or `Type` but could also be a function-like object
struct Patch{T}
target::T
alternate::Function
end
macro patch(expr::Expr)
def = splitdef(expr)
if haskey(def, :name) && haskey(def, :body)
target = def[:name]
elseif !haskey(def, :name)
throw(ArgumentError("Function definition must be a named function"))
else
throw(ArgumentError("Function definition must not be an empty function"))
end
# Include the target function name in the patch to make stack traces easier to read.
# If the provided target uses a fully-qualified reference we'll just extract the name
# of the function (e.g `Base.foo` -> `foo`).
target_name = if Meta.isexpr(target, :.)
target.args[2].value
else
target
end
def[:name] = gensym(string(target_name, "_patch"))
alternate = combinedef(def)
# We need to evaluate the alternate function in the context of the `@patch` macro in
# order to support closures.
return esc(:($Patch($target, $alternate)))
end
struct PatchEnv
mapping::Dict{Any,Vector{Function}}
debug::Bool
end
function PatchEnv(patches, debug::Bool=false)
pe = PatchEnv(debug)
apply!(pe, patches)
return pe
end
PatchEnv(debug::Bool=false) = PatchEnv(Dict{Any,Vector{Function}}(), debug)
function Base.:(==)(pe1::PatchEnv, pe2::PatchEnv)
return pe1.mapping == pe2.mapping && pe1.debug == pe2.debug
end
"""
merge(pe1::PatchEnv, pe2::PatchEnv) -> PatchEnv
Merge the two `PatchEnv` instances.
This is done in such a way that the following always holds:
```
patches_1 = Patch[...]
patches_2 = Patch[...]
patches = vcat(patches_1, patches_2)
pe1 = PatchEnv(patches_1)
pe2 = PatchEnv(patches_2)
pe = PatchEnv(patches)
@assert pe == merge(pe1, pe2)
```
The `debug` flag will be set to true if either `pe1` or `pe2` have it set to true.
"""
function Base.merge(pe1::PatchEnv, pe2::PatchEnv)
mapping = mergewith(vcat, pe1.mapping, pe2.mapping)
return PatchEnv(mapping, pe1.debug || pe2.debug)
end
function apply!(pe::PatchEnv, p::Patch)
alternate_funcs = get!(Vector{Function}, pe.mapping, p.target)
push!(alternate_funcs, p.alternate)
return pe
end
function apply!(pe::PatchEnv, patches)
for p in patches
apply!(pe, p)
end
return pe
end
"""
apply(body::Function, patches; debug::Bool=false)
apply(body::Function, pe::PatchEnv)
Convenience function to run `body` in the context of the given `patches`.
This is intended to be used with do-block notation, e.g.:
```
patch = @patch ...
apply(patch) do
...
end
```
## Nesting
Note that calls to apply will nest the patches that are applied. If multiple patches
are made to the same method, the innermost patch takes precedence.
The following two examples are equivalent:
```
patch_2 = @patch ...
apply([patch, patch_2]) do
...
end
```
```
apply(patch) do
apply(patch_2) do
...
end
end
```
"""
function apply(body::Function, pe::PatchEnv)
merged_pe = merge(PATCH_ENV[], pe)
return with_active_env(body, merged_pe)
end
function apply(body::Function, patches; debug::Bool=false)
return apply(body, PatchEnv(patches, debug))
end
# https://github.com/JuliaLang/julia/pull/50958
if isdefined(Base, :ScopedValue)
const PATCH_ENV = Base.ScopedValue(PatchEnv())
with_active_env(body::Function, pe::PatchEnv) = Base.with(body, PATCH_ENV => pe)
else
const PATCH_ENV = Ref{PatchEnv}(PatchEnv())
function with_active_env(body::Function, pe::PatchEnv)
old_pe = PATCH_ENV[]
try
PATCH_ENV[] = pe
body()
finally
PATCH_ENV[] = old_pe
end
end
end