Sha256: 097fa82b8a1f1d925a896a6895beb224e5f48c38882838ea666f7ce8afd72090

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'debug_inspector'

module BindingOfCaller
  module BindingExtensions
    # Retrieve the binding of the nth caller of the current frame.
    # @return [Binding]
    def of_caller(n)
      c = callers.drop(1)
      if n > (c.size - 1)
        raise "No such frame, gone beyond end of stack!"
      else
        c[n]
      end
    end

    # Return bindings for all caller frames.
    # @return [Array<Binding>]
    def callers
      ary = []

      RubyVM::DebugInspector.open do |dc|
        locs = dc.backtrace_locations

        locs.size.times do |i|
          b = dc.frame_binding(i)
          if b
            b.instance_variable_set(:@iseq, dc.frame_iseq(i))
            ary << b
          end
        end
      end

      ary.drop(1)
    end

    # Number of parent frames available at the point of call.
    # @return [Fixnum]
    def frame_count
      callers.size - 1
    end

    # The type of the frame.
    # @return [Symbol]
    def frame_type
      return nil if !@iseq
      
      # apparently the 9th element of the iseq array holds the frame type
      # ...not sure how reliable this is.
      @frame_type ||= @iseq.to_a[9]
    end

    # The description of the frame.
    # @return [String]
    def frame_description
      return nil if !@iseq
      @frame_description ||= @iseq.label
    end
  end
end

class ::Binding
  include BindingOfCaller::BindingExtensions
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
binding_of_caller-1.0.0 lib/binding_of_caller/mri.rb
binding_of_caller-0.8.0 lib/binding_of_caller/mri2.rb