File: mri2.rb

package info (click to toggle)
ruby-binding-of-caller 0.7.2%2Bdebian1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 176 kB
  • ctags: 65
  • sloc: ruby: 308; ansic: 173; makefile: 9
file content (69 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 |i|
        n = 0
        loop do
          begin
            b = i.frame_binding(n) 
          rescue ArgumentError
            break
          end

          if b
            b.instance_variable_set(:@iseq, i.frame_iseq(n))
            ary << b
          end
          
          n += 1
        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