Sha256: 6b1320ff0a6d84b3a47f36c5d7ad2977bf73d4fc970ba1f9f47535e2b1f66b3f

Contents?: true

Size: 1.44 KB

Versions: 14

Compression:

Stored size: 1.44 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Agent
    # Thread local way to access the current RequireState. Used as a convenient
    # wrapper to ensure specific key is used and that we handle the case when
    # we try to enter a previously unset require scope.
    # (Note that 'require scopes' track nested requires.  This is distinct
    # from 'contrast scope', which tracks instrumentation.)
    module RequireStates
      class << self
        KEY = :thread_local_contrast_require_scope

        def enter
          scope = current_scope
          unless current_scope
            scope = RequireState.new
            Thread.current[KEY] = scope
          end
          scope.enter
        end

        def exit
          current_scope.exit
        end

        def current_scope
          Thread.current[KEY]
        end

        def status
          current_scope&.scope.to_s
        end

        def in_scope?
          scope = current_scope
          scope && scope.scope > 1
        end
      end
    end

    # Simple counter class for tracking how deep in nested requires / file
    # load operations we currently are.
    class RequireState
      attr_reader :scope

      def initialize
        @scope = 0
      end

      def enter
        @scope += 1
      end

      def exit
        @scope -= 1
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
contrast-agent-3.13.2 lib/contrast/agent/require_state.rb
contrast-agent-3.13.1 lib/contrast/agent/require_state.rb
contrast-agent-3.13.0 lib/contrast/agent/require_state.rb
contrast-agent-3.12.2 lib/contrast/agent/require_state.rb
contrast-agent-3.12.1 lib/contrast/agent/require_state.rb
contrast-agent-3.12.0 lib/contrast/agent/require_state.rb
contrast-agent-3.11.0 lib/contrast/agent/require_state.rb
contrast-agent-3.10.2 lib/contrast/agent/require_state.rb
contrast-agent-3.10.1 lib/contrast/agent/require_state.rb
contrast-agent-3.10.0 lib/contrast/agent/require_state.rb
contrast-agent-3.9.1 lib/contrast/agent/require_state.rb
contrast-agent-3.9.0 lib/contrast/agent/require_state.rb
contrast-agent-3.8.5 lib/contrast/agent/require_state.rb
contrast-agent-3.8.4 lib/contrast/agent/require_state.rb