Sha256: 456ccd8f4e8be9b81a7f86e03476a640665ba6c4b8fa626e886896dca62492bf

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module DiverDown
  module Trace
    # To handle call stacks obtained by TracePoint more efficiently.
    # TracePoint also acquires calls that are not trace targets, but for dependency extraction, we want to acquire only a list of targets.
    # In this class, push/pop is performed on all call/return, but it should always be possible to trace back to the caller of the target dependency at high speed.
    class CallStack
      class StackEmptyError < RuntimeError; end

      # @attr_reader stack [Integer] stack size
      attr_reader :stack_size

      def initialize
        @stack_size = 0
        @stack = {}
      end

      # @return [Boolean]
      def empty?
        @stack.empty?
      end

      # @return [Array<Object>]
      def stack
        @stack.values
      end

      # @param context [Object, nil] User defined stack context.
      # @return [void]
      def push(context = nil)
        @stack_size += 1
        @stack[@stack_size] = context unless context.nil?
      end

      # @return [void]
      def pop
        raise StackEmptyError if @stack_size.zero?

        @stack.delete(@stack_size) if @stack.key?(@stack_size)
        @stack_size -= 1
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
diver_down-0.0.1.alpha2 lib/diver_down/trace/call_stack.rb
diver_down-0.0.1.alpha1 lib/diver_down/trace/call_stack.rb