0
  ]   ˆl–Ü4ý(&”¾R( Jë�?p/©‹Fÿ�þDù]Y¹@ñeEHçÂ„¿Á_�uÐb&=LyZËcõa—Ým_J	õ4Ÿº‚ œP\mË€yLZ7ÿÁ )ç    # frozen_string_literal: true

require("bootsnap/bootsnap")

module Bootsnap
  module CompileCache
    module YAML
      Uncompilable = Class.new(StandardError)
      UnsupportedTags = Class.new(Uncompilable)

      SUPPORTED_INTERNAL_ENCODINGS = [
        nil, # UTF-8
        Encoding::UTF_8,
        Encoding::ASCII,
        Encoding::BINARY,
      ].freeze

      class << self
        attr_accessor(:msgpack_factory, :supported_options)
        attr_reader(:implementation, :cache_dir)

        def cache_dir=(cache_dir)
          @cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}yaml" : "#{cache_dir}-yaml"
        end

        def precompile(path)
          return false unless CompileCache::YAML.supported_internal_encoding?

          CompileCache::Native.precompile(
            cache_dir,
            path.to_s,
            @implementation,
          )
        end

        def install!(cache_dir)
          self.cache_dir = cache_dir
          init!
          ::YAML.singleton_class.prepend(@implementation::Patch)
        end

        # Psych coerce strings to `Encoding.default_internal` but Message Pack only support
        # UTF-8, US-ASCII and BINARY. So if Encoding.default_internal is set to anything else
        # we can't safely use the cache
        def supported_internal_encoding?
          SUPPORTED_INTERNAL_ENCODINGS.include?(Encoding.default_internal)
        end

        module EncodingAwareSymbols
          extend self

          def unpack(payload)
            (+payload).force_encoding(Encoding::UTF_8).to_sym
          end
        end

        def init!
          require("yaml")
          require("msgpack")
          require("date")

          @implementation = ::YAML::VERSION >= "4" ? Psych4 : Psych3
          if @implementation::Patch.method_defined?(:unsafe_load_file) && !::YAML.respond_to?(:unsafe_load_file)
            @implementation::Patch.send(:remove_method, :unsafe_load_file)
          end

          unless const_defined?(:NoTagsVisitor)
            visitor = Class.new(Psych::Visitors::ToRuby) do
              def visit(target)
                if target.tag
                  raise UnsupportedTags, "YAML tags are not supported: #{target.tag}"
                end

                super
              end
            end
            const_set(:NoTagsVisitor, visitor)
          end

          # MessagePack serializes symbols as strings by default.
          # We want them to roundtrip cleanly, so we use a custom factory.
          # see: https://github.com/msgpack/msgpack-ruby/pull/122
          factory = MessagePack::Factory.new
          factory.register_type(
            0x00,
            Symbol,
            packer: :to_msgpack_ext,
            unpacker: EncodingAwareSymbols.method(:unpack).to_proc,
          )

          if defined? MessagePack::Timestamp
            factory.register_type(
              MessagePack::Timestamp::TYPE, # or just -1
              Time,
              packer: MessagePack::Time::Packer,
              unpacker: MessagePack::Time::Unpacker,
            )

            marshal_fallback = {
              packer: ->(value) { Marshal.dump(value) },
              unpacker: ->(payload) { Marshal.load(payload) },
            }
            {
              Date => 0x01,
              Regexp => 0x02,
            }.each do |type, code|
              factory.register_type(code, type, marshal_fallback)
            end
          end

          self.msgpack_factory = factory

          self.supported_options = []
          params = ::YAML.method(:load).parameters
          if params.include?([:key, :symbolize_names])
            supported_options << :symbolize_names
          end
          if params.include?([:key, :freeze]) && factory.load(factory.dump("yaml"), freeze: true).frozen?
            supported_options << :freeze
          end
          supported_options.freeze
        end

        def patch
          @implementation::Patch
        end

        def strict_load(payloa