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. | |
| 7 | 1 | class Phlex::Compiler::Environment |
| 8 | 1 | ELEMENTS_SOURCE_PATH = Phlex::SGML::Elements.instance_method(:register_element).source_location[0] |
| 9 | 1 | HELPER_OWNERS = Set[Phlex::SGML, Phlex::HTML, Phlex::SVG].freeze |
| 10 | 1 | SGML_SOURCE_PATH = Phlex::SGML.instance_method(:plain).source_location[0] |
| 11 | 1 | HELPER_SOURCE_PATHS = Set[ |
| 12 | SGML_SOURCE_PATH, | |
| 13 | Phlex::HTML.instance_method(:doctype).source_location[0], | |
| 14 | ].freeze | |
| 15 | ||
| 16 | 1 | 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. | |
| 20 | 1 | 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 = { | |
| 26 | 1 | 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 | ||
| 32 | 1 | CLASS_ALLOCATE = Class.instance_method(:allocate) |
| 33 | 1 | MODULE_CASE_EQUALITY = Module.instance_method(:===) |
| 34 | ||
| 35 | # The class methods `render Phlex::SVG.new` calls. | |
| 36 | 1 | SVG_CLASS_METHODS = %i[new allocate ===].freeze |
| 37 | ||
| 38 | # Everything an inlined svg block relies on besides the elements it calls. | |
| 39 | 1 | 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. | |
| 43 | 1 | attr_reader :inlined |
| 44 | ||
| 45 | # The Phlex::SVG methods an inlined svg block relies on. | |
| 46 | 1 | 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. | |
| 50 | 1 | def initialize(component, standard_set: standard_set_on?(component), method_resolver: Phlex::UNBOUND_INSTANCE_METHOD_METHOD.method(:bind_call), inline: true) |
| 51 | 189 | @component = component |
| 52 | 189 | @standard_set = standard_set |
| 53 | 189 | @method_resolver = method_resolver |
| 54 | 189 | @inline = inline |
| 55 | 189 | @elements = {} |
| 56 | 189 | @helpers = {} |
| 57 | 189 | @inlined = Set.new |
| 58 | 189 | @inlined_svg = Set.new |
| 59 | end | |
| 60 | ||
| 61 | 1 | 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. | |
| 65 | 1 | def element(name) |
| 66 | 577 | return unless @inline && kernel_block_given? |
| 67 | 574 | return @elements[name] if @elements.key?(name) |
| 68 | ||
| 69 | 388 | @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. | |
| 74 | 1 | def helper?(name) |
| 75 | 358 | return false unless @inline && kernel_block_given? |
| 76 | 355 | return @helpers[name] if @helpers.key?(name) |
| 77 | ||
| 78 | 223 | @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. | |
| 84 | 1 | def svg_environment |
| 85 | 22 | return @svg_environment if defined?(@svg_environment) |
| 86 | ||
| 87 | 16 | @svg_environment = (Phlex::Compiler::Environment.new(Phlex::SVG, standard_set: @standard_set, method_resolver: @method_resolver) if inline_svg?) |
| 88 | end | |
| 89 | ||
| 90 | 1 | private def inline_svg? |
| 91 | 16 | return false unless helper?(:svg) && helper?(:render) && (element = instance_method(:svg).super_method)then never taken |
| 92 | 16 | return false unless element.source_location&.first == ELEMENTS_SOURCE_PATH && element.original_name == :svgreceiver never nilthen never taken |
| 93 | 16 | return false unless element.owner.respond_to?(:__element_registry__)then never taken |
| 94 | ||
| 95 | 16 | registry = element.owner.__element_registry__ |
| 96 | 16 | return false unless registry.elements[:svg] == "svg" && !registry.void_elements.key?(:svg)then never taken |
| 97 | 16 | return false unless sgml_new? |
| 98 | # render calls it on the Phlex::SVG. | |
| 99 | 12 | return false unless Phlex::SVG.public_method_defined?(:internal_call) |
| 100 | ||
| 101 | 54 | return false unless SVG_CORE.all? { |name, original| core?(instance_method(name, Phlex::SVG), original) } |
| 102 | ||
| 103 | 9 | SVG_RENDER.all? do |name| |
| 104 | 72 | method = instance_method(name, Phlex::SVG) |
| 105 | # Lazy compilation compiles a component before rendering it as before. | |
| 106 | 72 | method = method.super_method if method&.owner == Phlex::Compiler::LazyCompilationreceiver never nil |
| 107 | ||
| 108 | 72 | 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. | |
| 113 | 1 | private def sgml_new? |
| 114 | 16 | Phlex::SVG.singleton_class.public_method_defined?(:new) && |
| 115 | 14 | (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. | |
| 123 | 1 | private def core?(method, original) |
| 124 | 68 | method == original && Phlex::Compiler::CoreCalls.native?(method) |
| 125 | end | |
| 126 | ||
| 127 | # An element called on an explicit receiver, so only if it's public. | |
| 128 | 1 | def public_element(name) |
| 129 | 17 | 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. | |
| 134 | 1 | def kernel_block_given? |
| 135 | 933 | return @kernel_block_given if defined?(@kernel_block_given) |
| 136 | ||
| 137 | 183 | @kernel_block_given = standard?(:block_given?, Kernel) |
| 138 | end | |
| 139 | ||
| 140 | 1 | private def standard?(name, owner) |
| 141 | 183 | (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. | |
| 145 | 1 | private def standard_set_on?(component) |
| 146 | 56 | component.const_get(:Set).equal?(::Set) |
| 147 | rescue NameError | |
| 148 | 0 | false |
| 149 | end | |
| 150 | ||
| 151 | 1 | private def resolve_element(name) |
| 152 | 388 | return unless (method = instance_method(name)) |
| 153 | ||
| 154 | 381 | owner = method.owner |
| 155 | 381 | return unless owner.respond_to?(:__element_registry__) |
| 156 | 258 | return unless method.source_location&.first == ELEMENTS_SOURCE_PATH && method.original_name == namereceiver never nil |
| 157 | ||
| 158 | 217 | registry = owner.__element_registry__ |
| 159 | 217 | return unless (tag = registry.elements[name])then never taken |
| 160 | 217 | return if overridden_by_descendant?(name, owner) |
| 161 | ||
| 162 | 213 | Element.new(tag:, void: registry.void_elements.key?(name)) |
| 163 | end | |
| 164 | ||
| 165 | 1 | private def resolve_helper(name) |
| 166 | 223 | return false unless (method = instance_method(name)) |
| 167 | ||
| 168 | 216 | 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. | |
| 175 | 1 | private def overridden_by_descendant?(name, owner) |
| 176 | 553 | descendants.any? do |descendant| |
| 177 | 5629 | instance_method(name, descendant)&.owner != owner |
| 178 | end | |
| 179 | end | |
| 180 | ||
| 181 | 1 | private def descendants |
| 182 | 553 | @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. | |
| 188 | 1 | private def instance_method(name, component = @component) |
| 189 | 6593 | method = @method_resolver.call(component, name) |
| 190 | 6577 | method if UnboundMethod === method && method.name == name |
| 191 | rescue | |
| 192 | 16 | nil |
| 193 | end | |
| 194 | end |