Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions lib/delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,15 @@ def __setobj__(obj)
end
end

def Delegator.delegating_block(mid) # :nodoc:
prok = lambda do |*args, &block|
def Delegator.delegating_code(mid) # :nodoc:
line = __LINE__+2
src = <<~RUBY
ruby2_keywords def #{mid}(*args, &block)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mid can be any String by defining define_method, so it is not safe to use it directly here.
For example, checking /[A-z_][A-z\d_]+/ is needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class C
  define_method("_; end; 1.tap do ;"){}
end

p C.instance_methods.grep(/;/) #=> [:"_; end; 1.tap do ;"]

target = self.__getobj__
target.__send__(mid, *args, &block)
end
prok.ruby2_keywords
if defined?(Ractor.shareable_proc)
Ractor.shareable_proc(&prok)
else
prok
target.__send__(:'#{mid}', *args, &block)
end
RUBY
[src, __FILE__, line]
end

#
Expand Down Expand Up @@ -433,7 +431,7 @@ def __setobj__(obj) # :nodoc:
class_eval(source.join(";"), __FILE__, __LINE__)

special.each do |method|
define_method(method, Delegator.delegating_block(method))
module_eval(*Delegator.delegating_code(method))
end

protected(*protected_instance_methods)
Expand Down