File: rubinius.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 (74 lines) | stat: -rw-r--r-- 1,711 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
70
71
72
73
74
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

      raise RuntimeError, "Invalid frame, gone beyond end of stack!" if bt.nil?

      b = Binding.setup(
                        bt.variables,
                        bt.variables.method,
                        bt.constant_scope,
                        bt.variables.self,
                        bt
                        )

      b.instance_variable_set :@frame_description,
                              bt.describe.gsub("{ } in", "block in")

      b
    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 do
        begin
          ary << Binding.of_caller(n)
        rescue
          break
        end
        n += 1
      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
      callers.size - 1
    end

    # The type of the frame.
    # @return [Symbol]
    def frame_type
      if compiled_code.for_module_body?
        :class
      elsif compiled_code.for_eval?
        :eval
      elsif compiled_code.is_block?
        :block
      else
        :method
      end
    end
  end
end

class ::Binding
  include BindingOfCaller::BindingExtensions
  extend BindingOfCaller::BindingExtensions
end