Skip to content

Commit

Permalink
Update forwardable from MRI trunk.
Browse files Browse the repository at this point in the history
headius committed Oct 12, 2017
1 parent a6fca15 commit 9f6f054
Showing 3 changed files with 63 additions and 27 deletions.
65 changes: 38 additions & 27 deletions lib/ruby/stdlib/forwardable.rb
Original file line number Diff line number Diff line change
@@ -110,15 +110,14 @@
# +delegate.rb+.
#
module Forwardable
# Version of +forwardable.rb+
FORWARDABLE_VERSION = "1.1.0"
require 'forwardable/impl'

FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"
# Version of +forwardable.rb+
FORWARDABLE_VERSION = "1.2.0"

@debug = nil
class << self
# If true, <tt>__FILE__</tt> will remain in the backtrace in the event an
# Exception is raised.
# ignored
attr_accessor :debug
end

@@ -131,12 +130,13 @@ class << self
# delegate [method, method, ...] => accessor
#
def instance_delegate(hash)
hash.each{ |methods, accessor|
methods = [methods] unless methods.respond_to?(:each)
methods.each{ |method|
def_instance_delegator(accessor, method)
}
}
hash.each do |methods, accessor|
unless defined?(methods.each)
def_instance_delegator(accessor, methods)
else
methods.each {|method| def_instance_delegator(accessor, method)}
end
end
end

#
@@ -188,6 +188,7 @@ def def_instance_delegator(accessor, method, ali = method)
alias def_delegators def_instance_delegators
alias def_delegator def_instance_delegator

# :nodoc:
def self._delegator_method(obj, accessor, method, ali)
accessor = accessor.to_s unless Symbol === accessor

@@ -197,24 +198,33 @@ def self._delegator_method(obj, accessor, method, ali)
accessor = "#{accessor}()"
end

line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}"
method_call = ".__send__(:#{method}, *args, &block)"
if _valid_method?(method)
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 "\#{caller_locations(1)[0]}: "#{mesg.dump}"\#{_.class}"'##{method}'
_#{method_call}
else
_.#{method}(*args, &block)
end
end;
end

_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
begin;
proc do
def #{ali}(*args, &block)
#{pre}
begin
#{accessor}
ensure
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug
end.__send__ :#{method}, *args, &block
end#{method_call}#{FILTER_EXCEPTION}
end
end
end;

RubyVM::InstructionSequence
.compile(str, __FILE__, __FILE__, line_no,
trace_instruction: false,
tailcall_optimization: true)
.eval
end
end

@@ -252,12 +262,13 @@ module SingleForwardable
# delegate [method, method, ...] => accessor
#
def single_delegate(hash)
hash.each{ |methods, accessor|
methods = [methods] unless methods.respond_to?(:each)
methods.each{ |method|
def_single_delegator(accessor, method)
}
}
hash.each do |methods, accessor|
unless defined?(methods.each)
def_single_delegator(accessor, methods)
else
methods.each {|method| def_single_delegator(accessor, method)}
end
end
end

#
24 changes: 24 additions & 0 deletions lib/ruby/stdlib/forwardable/impl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# :stopdoc:
module Forwardable
FILE_REGEXP = %r"#{Regexp.quote(File.dirname(__FILE__))}"
FILTER_EXCEPTION = <<-'END'
rescue ::Exception
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
::Kernel::raise
END

def self._valid_method?(method)
catch {|tag|
eval("BEGIN{throw tag}; ().#{method}", binding, __FILE__, __LINE__)
}
rescue SyntaxError
false
else
true
end

def self._compile_method(src, file, line)
eval(src, nil, file, line)
end
end
1 change: 1 addition & 0 deletions tool/globals_2_3_5.rb
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
erb.rb
fileutils.rb
find.rb
forwardable
forwardable.rb
getoptlong.rb
ipaddr.rb

0 comments on commit 9f6f054

Please sign in to comment.