2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# frame.rb -
|
2002-07-09 07:17:17 -04:00
|
|
|
# $Release Version: 0.9$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
|
|
|
|
module IRB
|
|
|
|
class Frame
|
|
|
|
extend Exception2MessageMapper
|
|
|
|
def_exception :FrameOverflow, "frame overflow"
|
|
|
|
def_exception :FrameUnderflow, "frame underflow"
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Default number of stack frames
|
2000-05-12 05:07:57 -04:00
|
|
|
INIT_STACK_TIMES = 3
|
2012-12-21 00:45:50 -05:00
|
|
|
# Default number of frames offset
|
2000-05-12 05:07:57 -04:00
|
|
|
CALL_STACK_OFFSET = 3
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new stack frame
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize
|
|
|
|
@frames = [TOPLEVEL_BINDING] * INIT_STACK_TIMES
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Used by Kernel#set_trace_func to register each event in the call stack
|
2000-05-12 05:07:57 -04:00
|
|
|
def trace_func(event, file, line, id, binding)
|
|
|
|
case event
|
|
|
|
when 'call', 'class'
|
2014-08-08 21:36:49 -04:00
|
|
|
@frames.push binding
|
2000-05-12 05:07:57 -04:00
|
|
|
when 'return', 'end'
|
2014-08-08 21:36:49 -04:00
|
|
|
@frames.pop
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the +n+ number of frames on the call stack from the last frame
|
|
|
|
# initialized.
|
|
|
|
#
|
|
|
|
# Raises FrameUnderflow if there are no frames in the given stack range.
|
2000-05-12 05:07:57 -04:00
|
|
|
def top(n = 0)
|
|
|
|
bind = @frames[-(n + CALL_STACK_OFFSET)]
|
|
|
|
Fail FrameUnderflow unless bind
|
|
|
|
bind
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the +n+ number of frames on the call stack from the first frame
|
|
|
|
# initialized.
|
|
|
|
#
|
|
|
|
# Raises FrameOverflow if there are no frames in the given stack range.
|
2000-05-12 05:07:57 -04:00
|
|
|
def bottom(n = 0)
|
|
|
|
bind = @frames[n]
|
|
|
|
Fail FrameOverflow unless bind
|
|
|
|
bind
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Convenience method for Frame#bottom
|
2000-05-12 05:07:57 -04:00
|
|
|
def Frame.bottom(n = 0)
|
|
|
|
@backtrace.bottom(n)
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Convenience method for Frame#top
|
2000-05-12 05:07:57 -04:00
|
|
|
def Frame.top(n = 0)
|
|
|
|
@backtrace.top(n)
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the binding context of the caller from the last frame initialized
|
2000-05-12 05:07:57 -04:00
|
|
|
def Frame.sender
|
|
|
|
eval "self", @backtrace.top
|
|
|
|
end
|
|
|
|
|
|
|
|
@backtrace = Frame.new
|
2000-09-18 12:28:40 -04:00
|
|
|
set_trace_func proc{|event, file, line, id, binding, klass|
|
2000-05-12 05:07:57 -04:00
|
|
|
@backtrace.trace_func(event, file, line, id, binding)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|