Skip to content

Commit de1fbd1

Browse files
committed
Simpler and faster check for the delegation fastpath
Fix: #35 [Bug #21708] Trying to compile code to check if a method can use the delegation fastpath is a bit wasteful and cause `RUPYOPT=-d` to be full of misleading errors. It's simpler and faster to use a simple regexp to do the same check.
1 parent 2169571 commit de1fbd1

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

forwardable.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
1919
spec.licenses = ["Ruby", "BSD-2-Clause"]
2020

2121
spec.required_ruby_version = '>= 2.4.0'
22-
spec.files = ["forwardable.gemspec", "lib/forwardable.rb", "lib/forwardable/impl.rb"]
22+
spec.files = ["forwardable.gemspec", "lib/forwardable.rb"]
2323
spec.bindir = "exe"
2424
spec.executables = []
2525
spec.require_paths = ["lib"]

lib/forwardable.rb

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@
109109
# +delegate.rb+.
110110
#
111111
module Forwardable
112-
require 'forwardable/impl'
113-
114112
# Version of +forwardable.rb+
115113
VERSION = "1.3.3"
116114
VERSION.freeze
@@ -206,37 +204,33 @@ def self._delegator_method(obj, accessor, method, ali)
206204
if Module === obj ?
207205
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
208206
obj.respond_to?(accessor, true)
209-
accessor = "#{accessor}()"
207+
accessor = "(#{accessor}())"
210208
end
211209

212210
args = RUBY_VERSION >= '2.7' ? '...' : '*args, &block'
213211
method_call = ".__send__(:#{method}, #{args})"
214-
if _valid_method?(method)
212+
if method.match?(/\A[_a-zA-Z]\w*[?!]?\z/)
215213
loc, = caller_locations(2,1)
216214
pre = "_ ="
217215
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
218-
method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}"
219-
begin;
220-
unless defined? _.#{method}
221-
::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
222-
_#{method_call}
223-
else
224-
_.#{method}(#{args})
225-
end
226-
end;
216+
method_call = <<~RUBY.chomp
217+
if defined?(_.#{method})
218+
_.#{method}(#{args})
219+
else
220+
::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
221+
_#{method_call}
222+
end
223+
RUBY
227224
end
228225

229-
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
230-
begin;
226+
eval(<<~RUBY, nil, __FILE__, __LINE__ + 1)
231227
proc do
232228
def #{ali}(#{args})
233-
#{pre}
234-
begin
235-
#{accessor}
236-
end#{method_call}
229+
#{pre}#{accessor}
230+
#{method_call}
237231
end
238232
end
239-
end;
233+
RUBY
240234
end
241235
end
242236

lib/forwardable/impl.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)