Class CallStack


  • public final class CallStack
    extends Object
    A bounded stack of interpreter frames with overflow and underflow checking.
    • Constructor Detail

      • CallStack

        public CallStack​(int maxDepth)
        Creates an empty call stack.
        Parameters:
        maxDepth - maximum number of frames before overflow
        Throws:
        IllegalArgumentException - if maxDepth is not positive
    • Method Detail

      • peekAt

        public StackFrame peekAt​(int depth)
        Returns the frame a given number of levels below the top without removing it.
        Parameters:
        depth - levels below the top, 0 being the top frame
        Returns:
        the frame at that depth
        Throws:
        IllegalArgumentException - if depth is negative
        IndexOutOfBoundsException - if depth is beyond the current stack size
      • depth

        public int depth()
        Returns:
        the number of frames on the stack
      • isEmpty

        public boolean isEmpty()
        Returns:
        whether the stack has no frames
      • clear

        public void clear()
        Removes all frames from the stack.
      • snapshot

        public List<StackFrame> snapshot()
        Copies the current frames into an immutable list.
        Returns:
        the frames ordered bottom to top
      • topToBottom

        public Iterable<StackFrame> topToBottom()
        Returns:
        the live frames iterated from top to bottom
      • formatStackTrace

        public String formatStackTrace()
        Renders the stack as a multi-line trace with method, line, and PC per frame.
        Returns:
        the formatted trace, or a placeholder when the stack is empty