lib/phlex/compiler/environment.rb

Lines

98%

83 of 84 covered

Branches

80%

48 of 60 covered

Methods

100%

17 of 17 covered

Not run
148
1# frozen_string_literal: true
2
3# What the compiler knows about the class whose methods it's compiling: which
4# bare calls are elements or helpers it can inline, which of those a loaded
5# subclass overrides, and whether a bare `Set` in the class body is the
6# standard library's. Built once per class body and shared by its methods.
71class Phlex::Compiler::Environment
81 ELEMENTS_SOURCE_PATH = Phlex::SGML::Elements.instance_method(:register_element).source_location[0]
91 HELPER_OWNERS = Set[Phlex::SGML, Phlex::HTML, Phlex::SVG].freeze
101 SGML_SOURCE_PATH = Phlex::SGML.instance_method(:plain).source_location[0]
111 HELPER_SOURCE_PATHS = Set[
12 SGML_SOURCE_PATH,
13 Phlex::HTML.instance_method(:doctype).source_location[0],
14 ].freeze
15
161 Element = Data.define(:tag, :void)
17
18 # What Phlex::HTML#svg's Phlex::SVG runs around its block, which compiled
19 # code skips when it inlines the block.
201 SVG_RENDER = %i[render? before_template around_template after_template view_template internal_call __yield_content__ __implicit_output__].freeze
21
22 # The core methods Phlex::SGML.new and render call on the Phlex::SVG, and
23 # block_given?, which its render methods and elements ask, by owner.
24 # Each as it was when the compiler loaded.
25 SVG_CORE = {
261 initialize: BasicObject.instance_method(:initialize),
27 __send__: BasicObject.instance_method(:__send__),
28 instance_variable_set: Kernel.instance_method(:instance_variable_set),
29 block_given?: Kernel.instance_method(:block_given?),
30 }.freeze
31
321 CLASS_ALLOCATE = Class.instance_method(:allocate)
331 MODULE_CASE_EQUALITY = Module.instance_method(:===)
34
35 # The class methods `render Phlex::SVG.new` calls.
361 SVG_CLASS_METHODS = %i[new allocate ===].freeze
37
38 # Everything an inlined svg block relies on besides the elements it calls.
391 SVG_DEPENDENCIES = [*SVG_RENDER, *SVG_CORE.keys, *SVG_CLASS_METHODS].freeze
40
41 # The element and helper names whose implementation compiled methods have
42 # baked in, by inlining them or by relying on what they return.
431 attr_reader :inlined
44
45 # The Phlex::SVG methods an inlined svg block relies on.
461 attr_reader :inlined_svg
47
48 # With `inline: false` nothing is recognised, so every method compiles to
49 # its original definition. Used to restore a class before it's frozen.
501 def initialize(component, standard_set: standard_set_on?(component), method_resolver: Phlex::UNBOUND_INSTANCE_METHOD_METHOD.method(:bind_call), inline: true)
51189 @component = component
52189 @standard_set = standard_set
53189 @method_resolver = method_resolver
54189 @inline = inline
55189 @elements = {}
56189 @helpers = {}
57189 @inlined = Set.new
58189 @inlined_svg = Set.new
59 end
60
611 def standard_set? = @standard_set
62
63 # The element a bare call to the method renders, if it's a registered
64 # element that no loaded descendant overrides.
651 def element(name)
66577 return unless @inline && kernel_block_given?
67574 return @elements[name] if @elements.key?(name)
68
69388 @elements[name] = resolve_element(name)
70 end
71
72 # Whether a bare call to the method reaches one of the SGML helpers, such
73 # as `plain`, that no loaded descendant overrides.
741 def helper?(name)
75358 return false unless @inline && kernel_block_given?
76355 return @helpers[name] if @helpers.key?(name)
77
78223 @helpers[name] = resolve_helper(name)
79 end
80
81 # An environment for the Phlex::SVG that `svg` renders around its block,
82 # if the block can be inlined: while svg is Phlex::HTML's, its super is
83 # the registered element, and Phlex::SVG renders as Phlex wrote it.
841 def svg_environment
8522 return @svg_environment if defined?(@svg_environment)
86
8716 @svg_environment = (Phlex::Compiler::Environment.new(Phlex::SVG, standard_set: @standard_set, method_resolver: @method_resolver) if inline_svg?)
88 end
89
901 private def inline_svg?
9116 return false unless helper?(:svg) && helper?(:render) && (element = instance_method(:svg).super_method)then never taken
9216 return false unless element.source_location&.first == ELEMENTS_SOURCE_PATH && element.original_name == :svgreceiver never nilthen never taken
9316 return false unless element.owner.respond_to?(:__element_registry__)then never taken
94
9516 registry = element.owner.__element_registry__
9616 return false unless registry.elements[:svg] == "svg" && !registry.void_elements.key?(:svg)then never taken
9716 return false unless sgml_new?
98 # render calls it on the Phlex::SVG.
9912 return false unless Phlex::SVG.public_method_defined?(:internal_call)
100
10154 return false unless SVG_CORE.all? { |name, original| core?(instance_method(name, Phlex::SVG), original) }
102
1039 SVG_RENDER.all? do |name|
10472 method = instance_method(name, Phlex::SVG)
105 # Lazy compilation compiles a component before rendering it as before.
10672 method = method.super_method if method&.owner == Phlex::Compiler::LazyCompilationreceiver never nil
107
10872 method && method.original_name == name && method.owner == Phlex::SGML && method.source_location&.first == SGML_SOURCE_PATHreceiver never nil
109 end
110 end
111
112 # Whether `render Phlex::SVG.new` builds and recognises the SVG as Phlex wrote it.
1131 private def sgml_new?
11416 Phlex::SVG.singleton_class.public_method_defined?(:new) &&
11514 (new = instance_method(:new, Phlex::SVG.singleton_class)) && new.owner == Phlex::SGML.singleton_class &&
116 new.original_name == :new && new.source_location&.first == SGML_SOURCE_PATH &&receiver never nil
117 core?(instance_method(:allocate, Phlex::SVG.singleton_class), CLASS_ALLOCATE) &&
118 core?(instance_method(:===, Phlex::SGML.singleton_class), MODULE_CASE_EQUALITY)
119 end
120
121 # Still the core method, unless it was replaced before the compiler loaded
122 # on an engine that can't tell.
1231 private def core?(method, original)
12468 method == original && Phlex::Compiler::CoreCalls.native?(method)
125 end
126
127 # An element called on an explicit receiver, so only if it's public.
1281 def public_element(name)
12917 element(name) if @component.public_method_defined?(name)else never taken
130 end
131
132 # Element and helper methods ask block_given? whether they were given a
133 # block, so they're only inlined while it's Kernel's.
1341 def kernel_block_given?
135933 return @kernel_block_given if defined?(@kernel_block_given)
136
137183 @kernel_block_given = standard?(:block_given?, Kernel)
138 end
139
1401 private def standard?(name, owner)
141183 (method = instance_method(name)) && method.owner == owner && method.original_name == name && !overridden_by_descendant?(name, owner)
142 end
143
144 # Without the lexical scopes, the class and its ancestors are the best guess.
1451 private def standard_set_on?(component)
14656 component.const_get(:Set).equal?(::Set)
147 rescue NameError
1480 false
149 end
150
1511 private def resolve_element(name)
152388 return unless (method = instance_method(name))
153
154381 owner = method.owner
155381 return unless owner.respond_to?(:__element_registry__)
156258 return unless method.source_location&.first == ELEMENTS_SOURCE_PATH && method.original_name == namereceiver never nil
157
158217 registry = owner.__element_registry__
159217 return unless (tag = registry.elements[name])then never taken
160217 return if overridden_by_descendant?(name, owner)
161
162213 Element.new(tag:, void: registry.void_elements.key?(name))
163 end
164
1651 private def resolve_helper(name)
166223 return false unless (method = instance_method(name))
167
168216 HELPER_OWNERS.include?(method.owner) && method.original_name == name &&
169 HELPER_SOURCE_PATHS.include?(method.source_location&.first) &&receiver never nil
170 !overridden_by_descendant?(name, method.owner)
171 end
172
173 # A compiled method is inherited, so it must not bake in a method that a
174 # loaded subclass overrides.
1751 private def overridden_by_descendant?(name, owner)
176553 descendants.any? do |descendant|
1775629 instance_method(name, descendant)&.owner != owner
178 end
179 end
180
1811 private def descendants
182553 @descendants ||= Phlex::Compiler.descendants_of(@component)
183 end
184
185 # The resolver runs in the file's lexical scope, where a refinement of
186 # UnboundMethod#bind or Method#call could intercept it. Anything but the
187 # named method counts as unresolved, which only keeps the call as it is.
1881 private def instance_method(name, component = @component)
1896593 method = @method_resolver.call(component, name)
1906577 method if UnboundMethod === method && method.name == name
191 rescue
19216 nil
193 end
194end