Permalink
Browse files

Fixed Binding#callers on rbx, added tests

  • Loading branch information...
1 parent 0356766 commit bea77df481beb6d1da56a9e16bda3aa6785b4746 @banister committed Mar 5, 2012
Showing with 34 additions and 30 deletions.
  1. +0 −12 Rakefile
  2. +17 −18 lib/binding_of_caller.rb
  3. +17 −0 test/test_binding_of_caller.rb
View
@@ -83,18 +83,6 @@ namespace :ruby do
end
end
-# namespace :rbx do
-# spec = Gem::Specification.new do |s|
-# apply_spec_defaults(s)
-# s.platform = Gem::Platform::RUBY #.new(["universal", "rubinius"])
-# end
-
-# Gem::PackageTask.new(spec) do |pkg|
-# pkg.need_zip = false
-# pkg.need_tar = false
-# end
-# end
-
desc "build the binaries"
task :compile do
chdir "./ext/#{PROJECT_NAME}/" do
View
@@ -1,13 +1,15 @@
dlext = RbConfig::CONFIG['DLEXT']
direc = File.dirname(__FILE__)
-if RUBY_ENGINE && RUBY_ENGINE == "ruby"
+if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
require "binding_of_caller.#{dlext}"
elsif defined?(Rubinius)
module BindingOfCaller
module BindingExtensions
+ # Retrieve the binding of the nth caller of the current frame.
+ # @return [Binding]
def of_caller(n)
bt = Rubinius::VM.backtrace(1 + n, true).first
@@ -26,41 +28,38 @@ def of_caller(n)
raise RuntimeError, "Invalid frame, gone beyond end of stack!"
end
+ # The description of the frame.
+ # @return [String]
def frame_description
@frame_description
end
+ # Return bindings for all caller frames.
+ # @return [Array<Binding>]
def callers
ary = []
n = 0
- loop {
+ loop do
begin
ary << Binding.of_caller(n)
rescue
break
end
-
n += 1
- }
-
- ary
+ end
+ ary.drop_while do |v|
+ !(v.frame_type == :method && v.eval("__method__") == :callers)
+ end.drop(1)
end
+ # Number of parent frames available at the point of call.
+ # @return [Fixnum]
def frame_count
- n = 1
- loop {
- begin
- Binding.of_caller(n)
- rescue
- break
- end
-
- n += 1
- }
-
- n
+ callers.size - 1
end
+ # The type of the frame.
+ # @return [Symbol]
def frame_type
case self.variables.method.metadata.to_a.first.to_s
when /block/
@@ -58,6 +58,23 @@ def o.a
end
end
+ describe "callers" do
+ before do
+ @o = Object.new
+ end
+
+ it 'should return the first non-internal binding when using callers.first' do
+ def @o.meth
+ x = :a_local
+ [binding.callers.first, binding.of_caller(0)]
+ end
+
+ b1, b2 = @o.meth
+ b1.eval("x").should == :a_local
+ b2.eval("x").should == :a_local
+ end
+ end
+
describe "frame_count" do
it 'frame_count should equal callers.count' do
binding.frame_count.should == binding.callers.count

0 comments on commit bea77df

Please sign in to comment.