\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/method/

Viewing File: patcher.rb

# frozen_string_literal: true

class Pry
  class Method
    class Patcher
      attr_accessor :method

      # rubocop:disable Style/ClassVars
      @@source_cache = {}
      # rubocop:enable Style/ClassVars

      def initialize(method)
        @method = method
      end

      def self.code_for(filename)
        @@source_cache[filename]
      end

      # perform the patch
      def patch_in_ram(source)
        if method.alias?
          with_method_transaction do
            redefine source
          end
        else
          redefine source
        end
      end

      private

      def redefine(source)
        @@source_cache[cache_key] = source
        TOPLEVEL_BINDING.eval wrap(source), cache_key
      end

      def cache_key
        "pry-redefined(0x#{method.owner.object_id.to_s(16)}##{method.name})"
      end

      # Run some code ensuring that at the end target#meth_name will not have changed.
      #
      # When we're redefining aliased methods we will overwrite the method at the
      # unaliased name (so that super continues to work). By wrapping that code in a
      # transation we make that not happen, which means that alias_method_chains, etc.
      # continue to work.
      #
      def with_method_transaction
        temp_name = "__pry_#{method.original_name}__"
        method = self.method
        method.owner.class_eval do
          alias_method temp_name, method.original_name
          yield
          alias_method method.name, method.original_name
          alias_method method.original_name, temp_name
        end
      ensure
        begin
          method.send(:remove_method, temp_name)
        rescue StandardError
          nil
        end
      end

      # Update the definition line so that it can be eval'd directly on the Method's
      # owner instead of from the original context.
      #
      # In particular this takes `def self.foo` and turns it into `def foo` so that we
      # don't end up creating the method on the singleton class of the singleton class
      # by accident.
      #
      # This is necessarily done by String manipulation because we can't find out what
      # syntax is needed for the argument list by ruby-level introspection.
      #
      # @param [String] line The original definition line. e.g. def self.foo(bar, baz=1)
      # @return [String]  The new definition line. e.g. def foo(bar, baz=1)
      def definition_for_owner(line)
        if line =~ /\Adef (?:.*?\.)?#{Regexp.escape(method.original_name)}(?=[\(\s;]|$)/
          "def #{method.original_name}#{$'}"
        else
          raise CommandError,
                "Could not find original `def #{method.original_name}` line " \
                "to patch."
        end
      end

      # Apply wrap_for_owner and wrap_for_nesting successively to `source`
      # @param [String] source
      # @return [String] The wrapped source.
      def wrap(source)
        wrap_for_nesting(wrap_for_owner(source))
      end

      # Update the source code so that when it has the right owner when eval'd.
      #
      # This (combined with definition_for_owner) is backup for the case that
      # wrap_for_nesting fails, to ensure that the method will stil be defined in
      # the correct place.
      #
      # @param [String] source  The source to wrap
      # @return [String]
      def wrap_for_owner(source)
        Pry.current[:pry_owner] = method.owner
        owner_source = definition_for_owner(source)
        visibility_fix = "#{method.visibility} #{method.name.to_sym.inspect}"
        "Pry.current[:pry_owner].class_eval do; #{owner_source}\n#{visibility_fix}\nend"
      end

      # Update the new source code to have the correct Module.nesting.
      #
      # This method uses syntactic analysis of the original source file to determine
      # the new nesting, so that we can tell the difference between:
      #
      #   class A; def self.b; end; end
      #   class << A; def b; end; end
      #
      # The resulting code should be evaluated in the TOPLEVEL_BINDING.
      #
      # @param [String] source  The source to wrap.
      # @return [String]
      def wrap_for_nesting(source)
        nesting = Pry::Code.from_file(method.source_file).nesting_at(method.source_line)

        (nesting + [source] + nesting.map { "end" } + [""]).join(";")
      rescue Pry::Indent::UnparseableNestingError
        source
      end
    end
  end
end