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.
71module Phlex::Compiler::CoreCalls
81 extend self
9
10 # Receiver class => methods called on it, or nil for any that Phlex defines.
11 CALLS = {
121 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.
411 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.
441 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.
471 REFINEMENTS = Module.instance_method(:refinements)
481 TARGET = Refinement.instance_method(:target)
491 INSTANCE_METHODS = Module.instance_method(:instance_methods)
501 PRIVATE_INSTANCE_METHODS = Module.instance_method(:private_instance_methods)
511 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.
541 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 = {
591 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
631 PHLEX_PATH = "#{File.dirname(__dir__)}/"
64
6526 ORIGINALS = CALLS.merge(HELPERS).select { |_receiver, names| names }.flat_map { |receiver, names|
6623 names.filter_map do |name|
6738 if receiver.method_defined?(name) || receiver.private_method_defined?(name)
6837 [[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".
741 def refined(modules, component, descendants = [])
751652 calls = CALLS.merge([component, *descendants].to_h { |receiver| [receiver, COMPONENT_CALLS] })
76
77126 modules.each do |mod|
7813 REFINEMENTS.bind_call(mod).each do |refinement|
7915 target = TARGET.bind_call(refinement)
8015 names = INSTANCE_METHODS.bind_call(refinement, false) | PRIVATE_INSTANCE_METHODS.bind_call(refinement, false) |
81 UNDEFINED_INSTANCE_METHODS.bind_call(refinement)
82
8315 calls.each do |receiver, called|
84316 names.each do |name|
85368 return "#{target.inspect}##{name}" if affects?(receiver, called, target, name)
86 end
87 end
88 end
89 end
90
91123 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.
971 def redefined
98126 ORIGINALS.each do |(receiver, name), (original, public)|
994662 method = receiver.instance_method(name)
1004662 changed = method != original || receiver.public_method_defined?(name) != public ||
1014662 (RUBY_ENGINE == "ruby" && NATIVE.include?(receiver) && defined_in_ruby?(method)) ||
1024662 (HELPERS[receiver]&.include?(name) && !defined_by_phlex?(method))
1034662 return "#{receiver.inspect}##{name}" if changedthen never taken
104 rescue NameError
1050 return "#{receiver.inspect}##{name}"
106 end
107
108126 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.
1131 private def defined_by_phlex?(method)
1141008 if (path = method.source_location&.first)receiver never nil
1151008 path.start_with?(PHLEX_PATH)
116 else
1170 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.
1221 def native?(method)
12365 RUBY_ENGINE != "ruby" || !defined_in_ruby?(method)
124 end
125
126 # CRuby writes some core methods in Ruby, under `<internal:…>` paths.
1271 private def defined_in_ruby?(method)
1283089 (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.
1351 private def affects?(receiver, called, target, name)
136368 return false unless called.nil? || called.include?(name)
137
13859 if receiver.ancestors.include?(target)
1392 found_after?(receiver, target, name, missing: !called.nil?, phlex_only: called.nil?)
14057 elsif receiver.singleton_class? || !(Module === target)
14142 false
14215 elsif called.nil?
143 # Only a method Phlex defines is called on a subclass of a Phlex receiver.
14414 descends?(receiver, target) && (receiver.method_defined?(name) || receiver.private_method_defined?(name)) && phlex?(receiver.instance_method(name).owner)
145 else
1461 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.
1531 private def descends?(receiver, target)
15415 return true unless Class === targetthen never taken
15515 return true if target.ancestors.include?(receiver)
15614 return false unless Class === receiverthen never taken
157
15814 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.
1641 private def found_after?(receiver, target, name, missing:, phlex_only:)
1652 owner = receiver.instance_method(name).owner
1662 return false if phlex_only && !phlex?(owner)then never taken
167
1682 ancestors = receiver.ancestors
1692 ancestors.index(target) <= ancestors.index(owner)
170 rescue NameError
1710 missing
172 end
173
174 # A class method's owner is the singleton class of the module defining it.
1750 private def phlex?(owner)
1760 owner = owner.attached_object if owner.singleton_class?
1770 Module === owner && owner.name&.start_with?("Phlex")
178 end
179end