lib/phlex/compiler/core_calls.rb
Lines
91%61 of 67 covered
Branches
70%24 of 34 covered
Methods
88%8 of 9 covered
- Not run
- 105117171175-177
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # The methods compiled code calls for the runtime, which calls them from its | |
| 4 | # own files. A refinement active in a component's file, or a core method | |
| 5 | # redefined in Ruby, would change what compiled code does but not what the | |
| 6 | # runtime does, so the compiler refuses to compile in either case. | |
| 7 | 1 | module Phlex::Compiler::CoreCalls |
| 8 | 1 | extend self |
| 9 | ||
| 10 | # Receiver class => methods called on it, or nil for any that Phlex defines. | |
| 11 | CALLS = { | |
| 12 | 1 | String => %i[<< include? ascii_only? encoding valid_encoding? +@], |
| 13 | Symbol => %i[name], | |
| 14 | Integer => %i[== > to_s], | |
| 15 | Float => %i[to_s], | |
| 16 | Hash => %i[length], | |
| 17 | String.singleton_class => %i[===], | |
| 18 | Symbol.singleton_class => %i[===], | |
| 19 | Integer.singleton_class => %i[===], | |
| 20 | Float.singleton_class => %i[===], | |
| 21 | Phlex::SGML::SafeObject.singleton_class => %i[===], | |
| 22 | Exception.singleton_class => %i[===], | |
| 23 | TrueClass => %i[=== ==], | |
| 24 | NilClass => %i[=== ==], | |
| 25 | Encoding => %i[equal?], | |
| 26 | Set.singleton_class => %i[[]], | |
| 27 | Kernel => %i[block_given?], | |
| 28 | Kernel.singleton_class => %i[raise], | |
| 29 | Phlex::SGML::SafeObject => %i[to_s], | |
| 30 | Phlex.singleton_class => %i[__map_exception__], | |
| 31 | Phlex::Escape.singleton_class => %i[html_escape], | |
| 32 | Phlex.__attribute_cache__.singleton_class => %i[fetch], | |
| 33 | Phlex::SGML::Attributes.singleton_class => nil, | |
| 34 | Phlex::SGML::Elements.singleton_class => nil, | |
| 35 | Phlex::Compiler.singleton_class => nil, | |
| 36 | Phlex::SGML::State => nil, | |
| 37 | }.freeze | |
| 38 | ||
| 39 | # Called on the component itself, where element methods call them from | |
| 40 | # the file that defines them. | |
| 41 | 1 | COMPONENT_CALLS = %i[block_given? __phlex_equal__? format_object __implicit_output__ __yield_content__ __text__ flush].freeze |
| 42 | ||
| 43 | # Core methods implemented in C on CRuby, which a definition in Ruby replaced. | |
| 44 | 1 | NATIVE = [String, Symbol, Integer, Float, Hash, String.singleton_class, Symbol.singleton_class, Integer.singleton_class, Float.singleton_class, Exception.singleton_class, TrueClass, NilClass, Encoding, Kernel, Kernel.singleton_class].freeze |
| 45 | ||
| 46 | # Called unbound, so a refinement module can't hide what it refines. | |
| 47 | 1 | REFINEMENTS = Module.instance_method(:refinements) |
| 48 | 1 | TARGET = Refinement.instance_method(:target) |
| 49 | 1 | INSTANCE_METHODS = Module.instance_method(:instance_methods) |
| 50 | 1 | PRIVATE_INSTANCE_METHODS = Module.instance_method(:private_instance_methods) |
| 51 | 1 | UNDEFINED_INSTANCE_METHODS = Module.instance_method(:undefined_instance_methods) |
| 52 | ||
| 53 | # The core methods, and whether they were public, as they were when Phlex loaded. | |
| 54 | 1 | SUBCLASSES = Class.instance_method(:subclasses) |
| 55 | ||
| 56 | # Phlex's own methods compiled code calls, whose redefinition would change | |
| 57 | # it but not the runtime, which doesn't call them. | |
| 58 | HELPERS = { | |
| 59 | 1 | Phlex::Compiler.singleton_class => %i[keywords escape_quotes symbol_value yield_component nested_attributes], |
| 60 | Phlex::SGML::Attributes.singleton_class => %i[attribute reference_attribute generate_attributes], | |
| 61 | }.freeze | |
| 62 | ||
| 63 | 1 | PHLEX_PATH = "#{File.dirname(__dir__)}/" |
| 64 | ||
| 65 | 26 | ORIGINALS = CALLS.merge(HELPERS).select { |_receiver, names| names }.flat_map { |receiver, names| |
| 66 | 23 | names.filter_map do |name| |
| 67 | 38 | if receiver.method_defined?(name) || receiver.private_method_defined?(name) |
| 68 | 37 | [[receiver, name], [receiver.instance_method(name), receiver.public_method_defined?(name)]] |
| 69 | end | |
| 70 | end | |
| 71 | }.to_h.freeze | |
| 72 | ||
| 73 | # The first refined method compiled code calls, as "Class#method". | |
| 74 | 1 | def refined(modules, component, descendants = []) |
| 75 | 1652 | calls = CALLS.merge([component, *descendants].to_h { |receiver| [receiver, COMPONENT_CALLS] }) |
| 76 | ||
| 77 | 126 | modules.each do |mod| |
| 78 | 13 | REFINEMENTS.bind_call(mod).each do |refinement| |
| 79 | 15 | target = TARGET.bind_call(refinement) |
| 80 | 15 | names = INSTANCE_METHODS.bind_call(refinement, false) | PRIVATE_INSTANCE_METHODS.bind_call(refinement, false) | |
| 81 | UNDEFINED_INSTANCE_METHODS.bind_call(refinement) | |
| 82 | ||
| 83 | 15 | calls.each do |receiver, called| |
| 84 | 316 | names.each do |name| |
| 85 | 368 | return "#{target.inspect}##{name}" if affects?(receiver, called, target, name) |
| 86 | end | |
| 87 | end | |
| 88 | end | |
| 89 | end | |
| 90 | ||
| 91 | 123 | nil |
| 92 | end | |
| 93 | ||
| 94 | # The first core method compiled code calls that's been redefined, or had | |
| 95 | # its visibility changed, since Phlex loaded, or that's defined in Ruby | |
| 96 | # where CRuby implements it in C. | |
| 97 | 1 | def redefined |
| 98 | 126 | ORIGINALS.each do |(receiver, name), (original, public)| |
| 99 | 4662 | method = receiver.instance_method(name) |
| 100 | 4662 | changed = method != original || receiver.public_method_defined?(name) != public || |
| 101 | 4662 | (RUBY_ENGINE == "ruby" && NATIVE.include?(receiver) && defined_in_ruby?(method)) || |
| 102 | 4662 | (HELPERS[receiver]&.include?(name) && !defined_by_phlex?(method)) |
| 103 | 4662 | return "#{receiver.inspect}##{name}" if changedthen never taken |
| 104 | rescue NameError | |
| 105 | 0 | return "#{receiver.inspect}##{name}" |
| 106 | end | |
| 107 | ||
| 108 | 126 | nil |
| 109 | end | |
| 110 | ||
| 111 | # The snapshot is taken when the compiler loads, which may be after an | |
| 112 | # override, so helpers must also still be Phlex's own, or phlex-native's. | |
| 113 | 1 | private def defined_by_phlex?(method) |
| 114 | 1008 | if (path = method.source_location&.first)receiver never nil |
| 115 | 1008 | path.start_with?(PHLEX_PATH) |
| 116 | else | |
| 117 | 0 | defined?(Phlex::Native::Attributes) && method.owner.equal?(Phlex::Native::Attributes)else never taken |
| 118 | end | |
| 119 | end | |
| 120 | ||
| 121 | # Whether a core method is the original, as far as the engine can tell. | |
| 122 | 1 | def native?(method) |
| 123 | 65 | RUBY_ENGINE != "ruby" || !defined_in_ruby?(method) |
| 124 | end | |
| 125 | ||
| 126 | # CRuby writes some core methods in Ruby, under `<internal:…>` paths. | |
| 127 | 1 | private def defined_in_ruby?(method) |
| 128 | 3089 | (path = method.source_location&.first) && !path.start_with?("<internal:") |
| 129 | end | |
| 130 | ||
| 131 | # A refinement applies to a call when its target comes no later in the | |
| 132 | # receiver's ancestors than where the method would otherwise be found. A | |
| 133 | # refinement of a subclass applies too, as the receiver may be one, except | |
| 134 | # for a class method, called on that class. | |
| 135 | 1 | private def affects?(receiver, called, target, name) |
| 136 | 368 | return false unless called.nil? || called.include?(name) |
| 137 | ||
| 138 | 59 | if receiver.ancestors.include?(target) |
| 139 | 2 | found_after?(receiver, target, name, missing: !called.nil?, phlex_only: called.nil?) |
| 140 | 57 | elsif receiver.singleton_class? || !(Module === target) |
| 141 | 42 | false |
| 142 | 15 | elsif called.nil? |
| 143 | # Only a method Phlex defines is called on a subclass of a Phlex receiver. | |
| 144 | 14 | descends?(receiver, target) && (receiver.method_defined?(name) || receiver.private_method_defined?(name)) && phlex?(receiver.instance_method(name).owner) |
| 145 | else | |
| 146 | 1 | descends?(receiver, target) |
| 147 | end | |
| 148 | end | |
| 149 | ||
| 150 | # Whether the target is a subclass of the receiver, or mixed into one, so | |
| 151 | # a value compiled code receives could be one. A module could also be | |
| 152 | # extended onto any one value. | |
| 153 | 1 | private def descends?(receiver, target) |
| 154 | 15 | return true unless Class === targetthen never taken |
| 155 | 15 | return true if target.ancestors.include?(receiver) |
| 156 | 14 | return false unless Class === receiverthen never taken |
| 157 | ||
| 158 | 14 | SUBCLASSES.bind_call(receiver).any? { |subclass| subclass.ancestors.include?(target) || descends?(subclass, target) } |
| 159 | end | |
| 160 | ||
| 161 | # A named method compiled code calls that's missing is affected by a | |
| 162 | # refinement defining it; any other missing method isn't called. Where any | |
| 163 | # method counts, only those Phlex defines are called. | |
| 164 | 1 | private def found_after?(receiver, target, name, missing:, phlex_only:) |
| 165 | 2 | owner = receiver.instance_method(name).owner |
| 166 | 2 | return false if phlex_only && !phlex?(owner)then never taken |
| 167 | ||
| 168 | 2 | ancestors = receiver.ancestors |
| 169 | 2 | ancestors.index(target) <= ancestors.index(owner) |
| 170 | rescue NameError | |
| 171 | 0 | missing |
| 172 | end | |
| 173 | ||
| 174 | # A class method's owner is the singleton class of the module defining it. | |
| 175 | 0 | private def phlex?(owner) |
| 176 | 0 | owner = owner.attached_object if owner.singleton_class? |
| 177 | 0 | Module === owner && owner.name&.start_with?("Phlex") |
| 178 | end | |
| 179 | end |