lib/phlex.rb
Lines
87%68 of 78 covered
Branches
56%17 of 30 covered
Methods
90%10 of 11 covered
- Not run
- 102106112114118122-126
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | 1 | require "date" |
| 4 | 1 | require "erb" |
| 5 | 1 | require "set" |
| 6 | 1 | require "zeitwerk" |
| 7 | ||
| 8 | 1 | module Phlex |
| 9 | # Ruby 3.2 and 3.3 can’t autoload in a non-main Ractor, so call | |
| 10 | # `Phlex::Loader.eager_load` before rendering in one. | |
| 11 | 1 | Loader = Zeitwerk::Loader.for_gem.tap do |loader| |
| 12 | 1 | loader.ignore("#{__dir__}/ruby_lsp") |
| 13 | 1 | loader.ignore("#{__dir__}/phlex/compiler/scope.rb") |
| 14 | ||
| 15 | 1 | loader.inflector.inflect( |
| 16 | "csv" => "CSV", | |
| 17 | "fifo" => "FIFO", | |
| 18 | "fifo_cache_store" => "FIFOCacheStore", | |
| 19 | "html" => "HTML", | |
| 20 | "sgml" => "SGML", | |
| 21 | "svg" => "SVG", | |
| 22 | ) | |
| 23 | ||
| 24 | 1 | loader.collapse("#{__dir__}/phlex/errors") |
| 25 | 1 | loader.setup |
| 26 | end | |
| 27 | ||
| 28 | 1 | Escape = ERB::Escape |
| 29 | ||
| 30 | 1 | DEPLOYED_AT = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) |
| 31 | 1 | CACHED_FILES = Set.new |
| 32 | ||
| 33 | # The main Ractor’s attribute cache. Other Ractors can’t read it, so each has its own. | |
| 34 | 1 | ATTRIBUTE_CACHE = AttributeCache.new(max_bytesize: 2 ** 20) |
| 35 | 1 | @attribute_cache_max_bytesize = ATTRIBUTE_CACHE.max_bytesize |
| 36 | ||
| 37 | begin | |
| 38 | 1 | require "phlex_native" |
| 39 | rescue LoadError | |
| 40 | 1 | nil |
| 41 | end | |
| 42 | ||
| 43 | 1 | UNBOUND_INSTANCE_METHOD_METHOD = Module.instance_method(:instance_method) |
| 44 | ||
| 45 | # Filled by the compiler: compiled path => Phlex::Compiler::Generation. | |
| 46 | # Lives here so the runtime can consult it without loading the compiler, | |
| 47 | # and is replaced rather than mutated so any Ractor can read it. | |
| 48 | 1 | @compiled_source_maps = {}.freeze |
| 49 | ||
| 50 | 1 | def self.__compiled_source_maps__ = @compiled_source_maps |
| 51 | ||
| 52 | 1 | COMPILED_SOURCE_MAPS_MUTEX = Mutex.new |
| 53 | ||
| 54 | 1 | def self.__add_compiled_source_map__(compiled_path, generation) |
| 55 | 94 | COMPILED_SOURCE_MAPS_MUTEX.synchronize do |
| 56 | 94 | @compiled_source_maps = @compiled_source_maps.merge(compiled_path => generation).freeze |
| 57 | end | |
| 58 | end | |
| 59 | ||
| 60 | # The original line for a location that may be in compiled code. | |
| 61 | 1 | def self.__source_line__(location) |
| 62 | 39 | if (generation = @compiled_source_maps[location.path]) |
| 63 | 1 | generation.lines[location.lineno] || location.lineno |
| 64 | else | |
| 65 | 38 | location.lineno |
| 66 | end | |
| 67 | end | |
| 68 | ||
| 69 | # Rewrites the backtrace of an exception raised in compiled code to name the | |
| 70 | # original lines. It's called on Phlex, not self, because a compiled block | |
| 71 | # can run with any object as self. | |
| 72 | 1 | def self.__map_exception__(exception) |
| 73 | 54 | return exception unless (locations = exception.backtrace_locations)then never taken |
| 74 | ||
| 75 | 54 | exception.set_backtrace( |
| 76 | locations.map do |loc| | |
| 77 | 4188 | if (generation = @compiled_source_maps[loc.path]) && (line = generation.lines[loc.lineno]) |
| 78 | 216 | "#{generation.path}:#{line}:in '#{loc.label}'" |
| 79 | else | |
| 80 | 3972 | loc.to_s |
| 81 | end | |
| 82 | end | |
| 83 | ) | |
| 84 | ||
| 85 | 54 | exception |
| 86 | end | |
| 87 | ||
| 88 | 1 | ATTRIBUTE_CACHE_EXPANSION_MUTEX = Mutex.new |
| 89 | ||
| 90 | 1 | def self.__expand_attribute_cache__(file_path) |
| 91 | 731 | return unless File.exist?(file_path) |
| 92 | ||
| 93 | 636 | ATTRIBUTE_CACHE_EXPANSION_MUTEX.synchronize do |
| 94 | 636 | unless CACHED_FILES.include?(file_path) |
| 95 | 123 | CACHED_FILES << file_path |
| 96 | 123 | @attribute_cache_max_bytesize = Phlex::ATTRIBUTE_CACHE.expand(File.size(file_path)) |
| 97 | end | |
| 98 | end | |
| 99 | end | |
| 100 | ||
| 101 | 1 | if !defined?(::Ractor) |
| 102 | 0 | def self.__main_ractor__? = truethen never taken |
| 103 | 1 | elsif ::Ractor.respond_to?(:main?) |
| 104 | 1 | def self.__main_ractor__? = ::Ractor.main? |
| 105 | else | |
| 106 | 0 | def self.__main_ractor__? = ::Ractor.current == ::Ractor.mainelse never taken |
| 107 | end | |
| 108 | ||
| 109 | # __attribute_cache__ is called once per render, so the check is written out in each. | |
| 110 | 1 | if defined?(Native) |
| 111 | # phlex-native serialises attributes faster than a cache can look them up, on any Ractor. | |
| 112 | 0 | def self.__attribute_cache__ = Native::SerializedAttributesthen never taken |
| 113 | 1 | elsif !defined?(::Ractor) |
| 114 | 0 | def self.__attribute_cache__ = ATTRIBUTE_CACHEthen never taken |
| 115 | 1 | elsif ::Ractor.respond_to?(:main?) |
| 116 | 2447 | def self.__attribute_cache__ = ::Ractor.main? ? ATTRIBUTE_CACHE : __ractor_attribute_cache__else never taken |
| 117 | else | |
| 118 | 0 | def self.__attribute_cache__ = (::Ractor.current == ::Ractor.main) ? ATTRIBUTE_CACHE : __ractor_attribute_cache__else never taken |
| 119 | end | |
| 120 | ||
| 121 | # A Ractor’s own cache is kept as large as the main one. | |
| 122 | 0 | def self.__ractor_attribute_cache__ |
| 123 | 0 | max_bytesize = @attribute_cache_max_bytesize |
| 124 | 0 | cache = (::Ractor.current[:__phlex_attribute_cache__] ||= AttributeCache.new(max_bytesize:)) |
| 125 | 0 | cache.grow_to(max_bytesize) if cache.max_bytesize < max_bytesize |
| 126 | 0 | cache |
| 127 | end | |
| 128 | ||
| 129 | # Generate an HTML string using Phlex’ HTML DSL | |
| 130 | 1 | def self.html(&block) |
| 131 | 22 | HTML.call do |component| |
| 132 | 22 | receiver = block.binding.receiver |
| 133 | ||
| 134 | 22 | receiver.instance_variables.each do |ivar| |
| 135 | 133 | next if component.instance_variable_defined?(ivar)then never taken |
| 136 | ||
| 137 | 133 | value = receiver.instance_variable_get(ivar) |
| 138 | 133 | component.instance_variable_set(ivar, value) |
| 139 | end | |
| 140 | ||
| 141 | 22 | component.instance_exec(receiver, &block) |
| 142 | end | |
| 143 | end | |
| 144 | ||
| 145 | # Generate an SVG string using Phlex’ SVG DSL | |
| 146 | 1 | def self.svg(&block) |
| 147 | 6 | SVG.call do |component| |
| 148 | 6 | receiver = block.binding.receiver |
| 149 | ||
| 150 | 6 | receiver.instance_variables.each do |ivar| |
| 151 | 36 | next if component.instance_variable_defined?(ivar)then never taken |
| 152 | ||
| 153 | 36 | value = receiver.instance_variable_get(ivar) |
| 154 | 36 | component.instance_variable_set(ivar, value) |
| 155 | end | |
| 156 | ||
| 157 | 6 | component.instance_exec(receiver, &block) |
| 158 | end | |
| 159 | end | |
| 160 | end | |
| 161 | ||
| 162 | 1 | def 💪 |
| 163 | 1 | Phlex |
| 164 | end |