Class: Byebug::Frame

Inherits:
Object
  • Object
show all
Includes:
Helpers::FileHelper
Defined in:
lib/byebug/frame.rb

Overview

Represents a frame in the stack trace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::FileHelper

#get_line, #get_lines, #n_lines, #normalize, #shortpath, #virtual_file?

Constructor Details

#initialize(context, pos) ⇒ Frame

Returns a new instance of Frame.



14
15
16
17
# File 'lib/byebug/frame.rb', line 14

def initialize(context, pos)
  @context = context
  @pos = pos
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



12
13
14
# File 'lib/byebug/frame.rb', line 12

def pos
  @pos
end

Instance Method Details

#_bindingObject



31
32
33
# File 'lib/byebug/frame.rb', line 31

def _binding
  @context.frame_binding(pos)
end

#_classObject



35
36
37
# File 'lib/byebug/frame.rb', line 35

def _class
  @context.frame_class(pos)
end

#_methodObject



39
40
41
# File 'lib/byebug/frame.rb', line 39

def _method
  @context.frame_method(pos)
end

#_selfObject



27
28
29
# File 'lib/byebug/frame.rb', line 27

def _self
  @context.frame_self(pos)
end

#argsObject

Gets current method arguments for the frame.



61
62
63
64
65
# File 'lib/byebug/frame.rb', line 61

def args
  return c_args unless _binding

  ruby_args
end

#c_frame?Boolean

Checks whether the frame is a c-frame

Returns:

  • (Boolean)


140
141
142
# File 'lib/byebug/frame.rb', line 140

def c_frame?
  _binding.nil?
end

#current?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/byebug/frame.rb', line 43

def current?
  @context.frame.pos == pos
end

#deco_argsObject

Builds a string containing all available args in the frame number, in a verbose or non verbose way according to the value of the callstyle setting



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/byebug/frame.rb', line 88

def deco_args
  return '' if args.empty?

  my_args = args.map do |arg|
    prefix, default = prefix_and_default(arg[0])

    kls = use_short_style?(arg) ? '' : "##{locals[arg[1]].class}"

    "#{prefix}#{arg[1] || default}#{kls}"
  end

  "(#{my_args.join(', ')})"
end

#deco_blockObject



75
76
77
# File 'lib/byebug/frame.rb', line 75

def deco_block
  _method[/(?:block(?: \(\d+ levels\))?|rescue) in /] || ''
end

#deco_callObject

Builds a formatted string containing information about current method call



105
106
107
# File 'lib/byebug/frame.rb', line 105

def deco_call
  deco_block + deco_class + deco_method + deco_args
end

#deco_classObject

Returns the current class in the frame or an empty string if the current callstyle setting is ‘short’



71
72
73
# File 'lib/byebug/frame.rb', line 71

def deco_class
  Setting[:callstyle] == 'short' || _class.to_s.empty? ? '' : "#{_class}."
end

#deco_fileObject

Formatted filename in frame



112
113
114
# File 'lib/byebug/frame.rb', line 112

def deco_file
  Setting[:fullpath] ? File.expand_path(file) : shortpath(file)
end

#deco_methodObject



79
80
81
# File 'lib/byebug/frame.rb', line 79

def deco_method
  _method[/((?:block(?: \(\d+ levels\))?|rescue) in )?(.*)/]
end

#deco_posObject

Properly formatted frame number of frame



119
120
121
# File 'lib/byebug/frame.rb', line 119

def deco_pos
  format('%-2d', pos)
end

#fileObject



19
20
21
# File 'lib/byebug/frame.rb', line 19

def file
  @context.frame_file(pos)
end

#lineObject



23
24
25
# File 'lib/byebug/frame.rb', line 23

def line
  @context.frame_line(pos)
end

#localsObject

Gets local variables for the frame.

TODO: Use brand new local_variable_get,set,defined? for rubies >= 2.1



52
53
54
55
56
# File 'lib/byebug/frame.rb', line 52

def locals
  return [] unless _binding

  _binding.eval('local_variables.inject({}){|h, v| h[v] = eval(v.to_s); h}')
end

#markObject

Formatted mark for the frame.

–> marks the current frame ͱ– marks c-frames

marks regular frames


130
131
132
133
134
135
# File 'lib/byebug/frame.rb', line 130

def mark
  return '-->' if current?
  return '    ͱ--' if c_frame?

  '   '
end

#to_hashObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/byebug/frame.rb', line 144

def to_hash
  {
    mark: mark,
    pos: deco_pos,
    call: deco_call,
    file: deco_file,
    line: line,
    full_path: File.expand_path(deco_file)
  }
end