Skip to content

Commit

Permalink
Preserve file, path, and line number of optimized methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tdg5 committed Oct 12, 2015
1 parent c296327 commit e2b4f63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/tco_method.rb
Expand Up @@ -53,7 +53,9 @@ def self.reevaluate_method_with_tco(receiver, method_name, method_owner)
#{existing_method.source}
end
CODE
tco_eval(code)

file, line = existing_method.source_location
tco_eval(code, file, File.dirname(file), line - 1)
method_name.to_sym
end

Expand All @@ -65,8 +67,8 @@ def self.reevaluate_method_with_tco(receiver, method_name, method_owner)
# @return [Object] Returns the value of the final expression of the provided
# code String.
# @raise [ArgumentError] if the provided code argument is not a String.
def self.tco_eval(code)
def self.tco_eval(code, file = nil, path = nil, line = nil)
raise ArgumentError, "Invalid code string!" unless code.is_a?(String)
RubyVM::InstructionSequence.new(code, nil, nil, nil, ISEQ_OPTIONS).eval
RubyVM::InstructionSequence.new(code, file, path, line, ISEQ_OPTIONS).eval
end
end
18 changes: 12 additions & 6 deletions test/unit/tco_method_test.rb
Expand Up @@ -125,8 +125,10 @@ class #{dummy_class.name}
refute tail_call_optimized?(fib_yielder, 5)

subject.call(method_owner, :module_fib_yielder, :module)
fib_yielder = method_owner.method(:module_fib_yielder)
assert tail_call_optimized?(fib_yielder, 5)
tco_fib_yielder = method_owner.method(:module_fib_yielder)
assert tail_call_optimized?(tco_fib_yielder, 5)

assert_equal fib_yielder.source_location, tco_fib_yielder.source_location
end
end

Expand All @@ -142,8 +144,10 @@ class #{dummy_class.name}
refute tail_call_optimized?(fib_yielder, 5)

subject.call(method_owner, :class_fib_yielder, :module)
fib_yielder = method_owner.method(:class_fib_yielder)
assert tail_call_optimized?(fib_yielder, 5)
tco_fib_yielder = method_owner.method(:class_fib_yielder)
assert tail_call_optimized?(tco_fib_yielder, 5)

assert_equal fib_yielder.source_location, tco_fib_yielder.source_location
end
end

Expand All @@ -161,8 +165,10 @@ class #{dummy_class.name}
refute tail_call_optimized?(fib_yielder, 5)

subject.call(method_owner, :instance_fib_yielder, :instance)
fib_yielder = instance_class.new.method(:instance_fib_yielder)
assert tail_call_optimized?(fib_yielder, 5)
tco_fib_yielder = instance_class.new.method(:instance_fib_yielder)
assert tail_call_optimized?(tco_fib_yielder, 5)

assert_equal fib_yielder.source_location, tco_fib_yielder.source_location
end
end
end
Expand Down

0 comments on commit e2b4f63

Please sign in to comment.