ruby-changes:45497
From: nagachika <ko1@a...>
Date: Wed, 8 Feb 2017 02:18:51 +0900 (JST)
Subject: [ruby-changes:45497] nagachika:r57570 (ruby_2_3): merge revision(s) 53383, 55366: [Backport #12478]
nagachika 2017-02-08 02:18:46 +0900 (Wed, 08 Feb 2017) New Revision: 57570 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57570 Log: merge revision(s) 53383,55366: [Backport #12478] * lib/forwardable.rb (def_instance_delegator): adjust backtrace of method body by tail call optimization. adjusting the delegated target is still done by deleting backtrace. * lib/forwardable.rb (def_single_delegator): ditto. * lib/forwardable.rb (Forwardable._delegator_method): extract method generator and deal with non-module objects. [ruby-dev:49656] [Bug #12478] Modified directories: branches/ruby_2_3/ Modified files: branches/ruby_2_3/ChangeLog branches/ruby_2_3/lib/forwardable.rb branches/ruby_2_3/test/test_forwardable.rb branches/ruby_2_3/version.h Index: ruby_2_3/lib/forwardable.rb =================================================================== --- ruby_2_3/lib/forwardable.rb (revision 57569) +++ ruby_2_3/lib/forwardable.rb (revision 57570) @@ -178,33 +178,44 @@ module Forwardable https://github.com/ruby/ruby/blob/trunk/ruby_2_3/lib/forwardable.rb#L178 # q.push 23 #=> NoMethodError # def def_instance_delegator(accessor, method, ali = method) - accessor = accessor.to_s - if method_defined?(accessor) || private_method_defined?(accessor) - accessor = "#{accessor}()" - end + gen = Forwardable._delegator_method(self, accessor, method, ali) - line_no = __LINE__; str = %{ - def #{ali}(*args, &block) - begin - #{accessor}.__send__(:#{method}, *args, &block) - rescue ::Exception - $@.delete_if{|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug - ::Kernel::raise - end - end - } # If it's not a class or module, it's an instance - begin - module_eval(str, __FILE__, line_no) - rescue - instance_eval(str, __FILE__, line_no) - end - + (Module === self ? self : singleton_class).module_eval(&gen) end alias delegate instance_delegate alias def_delegators def_instance_delegators alias def_delegator def_instance_delegator + + def self._delegator_method(obj, accessor, method, ali) + accessor = accessor.to_s unless Symbol === accessor + + if Module === obj ? + obj.method_defined?(accessor) || obj.private_method_defined?(accessor) : + obj.respond_to?(accessor, true) + accessor = "#{accessor}()" + end + + line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}" + begin; + proc do + def #{ali}(*args, &block) + begin + #{accessor} + ensure + $@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug + end.__send__ :#{method}, *args, &block + end + end + end; + + RubyVM::InstructionSequence + .compile(str, __FILE__, __FILE__, line_no, + trace_instruction: false, + tailcall_optimization: true) + .eval + end end # SingleForwardable can be used to setup delegation at the object level as well. @@ -275,23 +286,9 @@ module SingleForwardable https://github.com/ruby/ruby/blob/trunk/ruby_2_3/lib/forwardable.rb#L286 # the method of the same name in _accessor_). If _new_name_ is # provided, it is used as the name for the delegate method. def def_single_delegator(accessor, method, ali = method) - accessor = accessor.to_s - if method_defined?(accessor) || private_method_defined?(accessor) - accessor = "#{accessor}()" - end - - line_no = __LINE__; str = %{ - def #{ali}(*args, &block) - begin - #{accessor}.__send__(:#{method}, *args, &block) - rescue ::Exception - $@.delete_if{|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug - ::Kernel::raise - end - end - } + gen = Forwardable._delegator_method(self, accessor, method, ali) - instance_eval(str, __FILE__, line_no) + instance_eval(&gen) end alias delegate single_delegate Index: ruby_2_3/ChangeLog =================================================================== --- ruby_2_3/ChangeLog (revision 57569) +++ ruby_2_3/ChangeLog (revision 57570) @@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_3/ChangeLog#L1 +Wed Feb 8 02:17:02 2017 Nobuyoshi Nakada <nobu@r...> + + * lib/forwardable.rb (Forwardable._delegator_method): extract + method generator and deal with non-module objects. + [ruby-dev:49656] [Bug #12478] + +Wed Feb 8 02:17:02 2017 Nobuyoshi Nakada <nobu@r...> + + * lib/forwardable.rb (def_instance_delegator): adjust backtrace of + method body by tail call optimization. adjusting the delegated + target is still done by deleting backtrace. + + * lib/forwardable.rb (def_single_delegator): ditto. + Tue Jan 17 03:51:48 2017 Nobuyoshi Nakada <nobu@r...> * compile.c (setup_args): duplicate splatting array if more Index: ruby_2_3/version.h =================================================================== --- ruby_2_3/version.h (revision 57569) +++ ruby_2_3/version.h (revision 57570) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_3/version.h#L1 #define RUBY_VERSION "2.3.3" #define RUBY_RELEASE_DATE "2017-02-08" -#define RUBY_PATCHLEVEL 242 +#define RUBY_PATCHLEVEL 243 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 2 Index: ruby_2_3/test/test_forwardable.rb =================================================================== --- ruby_2_3/test/test_forwardable.rb (revision 57569) +++ ruby_2_3/test/test_forwardable.rb (revision 57570) @@ -144,7 +144,7 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L144 end end - def test_def_single_delegator + def test_class_single_delegator %i[def_delegator def_single_delegator].each do |m| cls = single_forwardable_class do __send__ m, :@receiver, :delegated1 @@ -154,7 +154,7 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L154 end end - def test_def_single_delegators + def test_class_single_delegators %i[def_delegators def_single_delegators].each do |m| cls = single_forwardable_class do __send__ m, :@receiver, :delegated1, :delegated2 @@ -165,7 +165,7 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L165 end end - def test_single_delegate + def test_class_single_delegate %i[delegate single_delegate].each do |m| cls = single_forwardable_class do __send__ m, delegated1: :@receiver, delegated2: :@receiver @@ -183,10 +183,50 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L183 end end + def test_obj_single_delegator + %i[def_delegator def_single_delegator].each do |m| + obj = single_forwardable_object do + __send__ m, :@receiver, :delegated1 + end + + assert_same RETURNED1, obj.delegated1 + end + end + + def test_obj_single_delegators + %i[def_delegators def_single_delegators].each do |m| + obj = single_forwardable_object do + __send__ m, :@receiver, :delegated1, :delegated2 + end + + assert_same RETURNED1, obj.delegated1 + assert_same RETURNED2, obj.delegated2 + end + end + + def test_obj_single_delegate + %i[delegate single_delegate].each do |m| + obj = single_forwardable_object do + __send__ m, delegated1: :@receiver, delegated2: :@receiver + end + + assert_same RETURNED1, obj.delegated1 + assert_same RETURNED2, obj.delegated2 + + obj = single_forwardable_object do + __send__ m, %i[delegated1 delegated2] => :@receiver + end + + assert_same RETURNED1, obj.delegated1 + assert_same RETURNED2, obj.delegated2 + end + end + class Foo extend Forwardable def_delegator :bar, :baz + def_delegator :caller, :itself, :c class Exception end @@ -197,6 +237,7 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L237 Foo.new.baz } assert_not_match(/\/forwardable\.rb/, e.backtrace[0]) + assert_equal(caller(0, 1)[0], Foo.new.c[0]) end class Foo2 < BasicObject @@ -245,4 +286,11 @@ class TestForwardable < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_3/test/test_forwardable.rb#L286 class_exec(&block) end end + + def single_forwardable_object(&block) + obj = Object.new.extend SingleForwardable + obj.instance_variable_set(:@receiver, RECEIVER) + obj.instance_eval(&block) + obj + end end Property changes on: ruby_2_3 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r53383,55366 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/