-
Notifications
You must be signed in to change notification settings - Fork 17
Simpler and faster check for the delegation fastpath #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,8 +109,6 @@ | |
| # +delegate.rb+. | ||
| # | ||
| module Forwardable | ||
| require 'forwardable/impl' | ||
|
|
||
| # Version of +forwardable.rb+ | ||
| VERSION = "1.3.3" | ||
| VERSION.freeze | ||
|
|
@@ -206,37 +204,33 @@ def self._delegator_method(obj, accessor, method, ali) | |
| if Module === obj ? | ||
| obj.method_defined?(accessor) || obj.private_method_defined?(accessor) : | ||
| obj.respond_to?(accessor, true) | ||
| accessor = "#{accessor}()" | ||
| accessor = "(#{accessor}())" | ||
| end | ||
|
|
||
| args = RUBY_VERSION >= '2.7' ? '...' : '*args, &block' | ||
| method_call = ".__send__(:#{method}, #{args})" | ||
| if _valid_method?(method) | ||
| if method.match?(/\A[_a-zA-Z]\w*[?!]?\z/) | ||
| loc, = caller_locations(2,1) | ||
| pre = "_ =" | ||
| mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method " | ||
| method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}" | ||
| begin; | ||
| unless defined? _.#{method} | ||
| ::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1 | ||
| _#{method_call} | ||
| else | ||
| _.#{method}(#{args}) | ||
| end | ||
| end; | ||
| method_call = <<~RUBY.chomp | ||
| if defined?(_.#{method}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before Ruby 3, using In this case, Example: module M
refine Object do
public :puts
end
end
o = Object.new
o.puts '' if defined?(o.puts)On Ruby < 3, we could probably use something like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with making that change, but note that I only moved the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, that makes sense. |
||
| _.#{method}(#{args}) | ||
| else | ||
| ::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1 | ||
| _#{method_call} | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| _compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1) | ||
| begin; | ||
| eval(<<~RUBY, nil, __FILE__, __LINE__ + 1) | ||
| proc do | ||
| def #{ali}(#{args}) | ||
| #{pre} | ||
| begin | ||
| #{accessor} | ||
| end#{method_call} | ||
| #{pre}#{accessor} | ||
| #{method_call} | ||
| end | ||
| end | ||
| end; | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this will fail if the method has any Unicode characters in its name, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, if this is trying to check if the method name is a valid identifier then Prism has methods for doing that check out of the box. Maybe we should use those
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's checking for two things, first that it's a valid name, but also that it can be called with
..e.g.
foo=is valid, but_.foo = *args, &blockisn't.Yes, but the idea is to be conservative, the regexp can be improved (or we could use prism) if we want, but as long as 99.9% of methods go in the fast path, it's fine IMO.