mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
forwardable.rb: adjust backtrace by tail call
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d8eb5ade4f
commit
6fd18ca51b
3 changed files with 38 additions and 18 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Wed Dec 30 11:28:57 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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.
|
||||||
|
|
||||||
Wed Dec 30 11:18:42 2015 Elliot Winkler <elliot.winkler@gmail.com>
|
Wed Dec 30 11:18:42 2015 Elliot Winkler <elliot.winkler@gmail.com>
|
||||||
|
|
||||||
* lib/forwardable.rb (def_instance_delegator) fix delegating to
|
* lib/forwardable.rb (def_instance_delegator) fix delegating to
|
||||||
|
|
|
@ -182,23 +182,28 @@ module Forwardable
|
||||||
accessor = "#{accessor}()"
|
accessor = "#{accessor}()"
|
||||||
end
|
end
|
||||||
|
|
||||||
line_no = __LINE__; str = %{
|
line_no = __LINE__; str = %{proc do
|
||||||
def #{ali}(*args, &block)
|
def #{ali}(*args, &block)
|
||||||
begin
|
begin
|
||||||
#{accessor}.__send__(:#{method}, *args, &block)
|
#{accessor}
|
||||||
rescue ::Exception
|
ensure
|
||||||
$@.delete_if{|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
|
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug
|
||||||
::Kernel::raise
|
end.__send__(:#{method}, *args, &block)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
}
|
end}
|
||||||
|
|
||||||
|
gen = RubyVM::InstructionSequence
|
||||||
|
.compile(str, __FILE__, __FILE__, line_no,
|
||||||
|
trace_instruction: false,
|
||||||
|
tailcall_optimization: true)
|
||||||
|
.eval
|
||||||
|
|
||||||
# If it's not a class or module, it's an instance
|
# If it's not a class or module, it's an instance
|
||||||
begin
|
begin
|
||||||
module_eval(str, __FILE__, line_no)
|
module_eval(&gen)
|
||||||
rescue
|
rescue
|
||||||
instance_eval(str, __FILE__, line_no)
|
instance_eval(&gen)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
alias delegate instance_delegate
|
alias delegate instance_delegate
|
||||||
|
@ -278,18 +283,23 @@ module SingleForwardable
|
||||||
accessor = "#{accessor}()"
|
accessor = "#{accessor}()"
|
||||||
end
|
end
|
||||||
|
|
||||||
line_no = __LINE__; str = %{
|
line_no = __LINE__; str = %{proc do
|
||||||
def #{ali}(*args, &block)
|
def #{ali}(*args, &block)
|
||||||
begin
|
begin
|
||||||
#{accessor}.__send__(:#{method}, *args, &block)
|
#{accessor}
|
||||||
rescue ::Exception
|
ensure
|
||||||
$@.delete_if{|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
|
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug
|
||||||
::Kernel::raise
|
end.__send__(:#{method}, *args, &block)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
}
|
end}
|
||||||
|
|
||||||
instance_eval(str, __FILE__, line_no)
|
gen = RubyVM::InstructionSequence
|
||||||
|
.compile(str, __FILE__, __FILE__, line_no,
|
||||||
|
trace_instruction: false,
|
||||||
|
tailcall_optimization: true)
|
||||||
|
.eval
|
||||||
|
|
||||||
|
instance_eval(&gen)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias delegate single_delegate
|
alias delegate single_delegate
|
||||||
|
|
|
@ -187,6 +187,7 @@ class TestForwardable < Test::Unit::TestCase
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
def_delegator :bar, :baz
|
def_delegator :bar, :baz
|
||||||
|
def_delegator :caller, :itself, :c
|
||||||
|
|
||||||
class Exception
|
class Exception
|
||||||
end
|
end
|
||||||
|
@ -197,6 +198,7 @@ class TestForwardable < Test::Unit::TestCase
|
||||||
Foo.new.baz
|
Foo.new.baz
|
||||||
}
|
}
|
||||||
assert_not_match(/\/forwardable\.rb/, e.backtrace[0])
|
assert_not_match(/\/forwardable\.rb/, e.backtrace[0])
|
||||||
|
assert_equal(caller(0, 1)[0], Foo.new.c[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
class Foo2 < BasicObject
|
class Foo2 < BasicObject
|
||||||
|
|
Loading…
Add table
Reference in a new issue