commit 6a28d978d8975bfa6b5c9cf7505948760a3430ba
parent 53f07beb4310b71784861d4d81db6425b4af33c7
Author: Dan Callaghan <djc@djc.id.au>
Date: Sat, 28 Nov 2026 17:59:14 +1100
store HookContext closures on the instance not the class
HookContext allows context variables to be Procs, which get injected
into the hook context as methods. Currently only used for :filename in
mime-decode to make it lazy.
Define the method on the HookContext instance, rather than the class,
and ensure it is cleaned up after evaluating the hook.
This fixes several issues: 'filename' was being leaked into all other
hook contexts after mime-decode was invoked the first time, and its Proc
would live eternally for no good reason. And this also fixes a warning
about redefining the method:
lib/sup/hook.rb:60: warning: method redefined; discarding old filename
lib/sup/hook.rb:60: warning: previous definition of filename was here
Diffstat:
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/lib/sup/hook.rb b/lib/sup/hook.rb
@@ -56,16 +56,19 @@ class HookManager
## we have to do this via method calls, so you don't get all the
## semantics of a regular variable. not ideal.
__lprocs.each do |k, v|
- self.class.instance_eval do
- define_method k do
- @__cache[k] ||= v.call
- end
+ define_singleton_method k do
+ @__cache[k] ||= v.call
end
end
ret = eval __hook, __binding, __filename
BufferManager.clear @__say_id if @__say_id
- @__cache = {}
ret
+ ensure
+ (__lprocs || []).each do |k, v|
+ next unless singleton_methods(false).include? k
+ singleton_class.send :remove_method, k
+ end
+ @__cache = {}
end
end