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
31require "date"
41require "erb"
51require "set"
61require "zeitwerk"
7
81module 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.
111 Loader = Zeitwerk::Loader.for_gem.tap do |loader|
121 loader.ignore("#{__dir__}/ruby_lsp")
131 loader.ignore("#{__dir__}/phlex/compiler/scope.rb")
14
151 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
241 loader.collapse("#{__dir__}/phlex/errors")
251 loader.setup
26 end
27
281 Escape = ERB::Escape
29
301 DEPLOYED_AT = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
311 CACHED_FILES = Set.new
32
33 # The main Ractor’s attribute cache. Other Ractors can’t read it, so each has its own.
341 ATTRIBUTE_CACHE = AttributeCache.new(max_bytesize: 2 ** 20)
351 @attribute_cache_max_bytesize = ATTRIBUTE_CACHE.max_bytesize
36
37 begin
381 require "phlex_native"
39 rescue LoadError
401 nil
41 end
42
431 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.
481 @compiled_source_maps = {}.freeze
49
501 def self.__compiled_source_maps__ = @compiled_source_maps
51
521 COMPILED_SOURCE_MAPS_MUTEX = Mutex.new
53
541 def self.__add_compiled_source_map__(compiled_path, generation)
5594 COMPILED_SOURCE_MAPS_MUTEX.synchronize do
5694 @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.
611 def self.__source_line__(location)
6239 if (generation = @compiled_source_maps[location.path])
631 generation.lines[location.lineno] || location.lineno
64 else
6538 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.
721 def self.__map_exception__(exception)
7354 return exception unless (locations = exception.backtrace_locations)then never taken
74
7554 exception.set_backtrace(
76 locations.map do |loc|
774188 if (generation = @compiled_source_maps[loc.path]) && (line = generation.lines[loc.lineno])
78216 "#{generation.path}:#{line}:in '#{loc.label}'"
79 else
803972 loc.to_s
81 end
82 end
83 )
84
8554 exception
86 end
87
881 ATTRIBUTE_CACHE_EXPANSION_MUTEX = Mutex.new
89
901 def self.__expand_attribute_cache__(file_path)
91731 return unless File.exist?(file_path)
92
93636 ATTRIBUTE_CACHE_EXPANSION_MUTEX.synchronize do
94636 unless CACHED_FILES.include?(file_path)
95123 CACHED_FILES << file_path
96123 @attribute_cache_max_bytesize = Phlex::ATTRIBUTE_CACHE.expand(File.size(file_path))
97 end
98 end
99 end
100
1011 if !defined?(::Ractor)
1020 def self.__main_ractor__? = truethen never taken
1031 elsif ::Ractor.respond_to?(:main?)
1041 def self.__main_ractor__? = ::Ractor.main?
105 else
1060 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.
1101 if defined?(Native)
111 # phlex-native serialises attributes faster than a cache can look them up, on any Ractor.
1120 def self.__attribute_cache__ = Native::SerializedAttributesthen never taken
1131 elsif !defined?(::Ractor)
1140 def self.__attribute_cache__ = ATTRIBUTE_CACHEthen never taken
1151 elsif ::Ractor.respond_to?(:main?)
1162447 def self.__attribute_cache__ = ::Ractor.main? ? ATTRIBUTE_CACHE : __ractor_attribute_cache__else never taken
117 else
1180 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.
1220 def self.__ractor_attribute_cache__
1230 max_bytesize = @attribute_cache_max_bytesize
1240 cache = (::Ractor.current[:__phlex_attribute_cache__] ||= AttributeCache.new(max_bytesize:))
1250 cache.grow_to(max_bytesize) if cache.max_bytesize < max_bytesize
1260 cache
127 end
128
129 # Generate an HTML string using Phlex’ HTML DSL
1301 def self.html(&block)
13122 HTML.call do |component|
13222 receiver = block.binding.receiver
133
13422 receiver.instance_variables.each do |ivar|
135133 next if component.instance_variable_defined?(ivar)then never taken
136
137133 value = receiver.instance_variable_get(ivar)
138133 component.instance_variable_set(ivar, value)
139 end
140
14122 component.instance_exec(receiver, &block)
142 end
143 end
144
145 # Generate an SVG string using Phlex’ SVG DSL
1461 def self.svg(&block)
1476 SVG.call do |component|
1486 receiver = block.binding.receiver
149
1506 receiver.instance_variables.each do |ivar|
15136 next if component.instance_variable_defined?(ivar)then never taken
152
15336 value = receiver.instance_variable_get(ivar)
15436 component.instance_variable_set(ivar, value)
155 end
156
1576 component.instance_exec(receiver, &block)
158 end
159 end
160end
161
1621def 💪
1631 Phlex
164end