Module: Lich::Common::HMR
- Defined in:
- documented/common/hmr.rb
Overview
Hot Module Reloading: utilities to safely reload script files without restarting the engine.
Reloading a file that belongs to Ruby's standard library or an installed gem is skipped because those libraries are not written to be re-executed and doing so can corrupt their state (e.g., stack overflow with REXML). User .lic scripts and Lich's own modules are safe to reload and will be reloaded on demand, with error reporting to the game client.
Constant Summary collapse
- RELOAD_ERRORS =
What a reload of one file may raise and still leave Lich running. StandardError alone misses the common failures: a typo in the edited file is a SyntaxError (a ScriptError), and a file whose body is not safe to re-run can overflow the stack (SystemStackError). Either one used to escape into the client thread and stop it reading input.
[StandardError, ScriptError, SystemStackError].freeze
- BACKTRACE_LINES =
Most backtrace lines printed for a failed reload. A stack overflow's backtrace repeats one frame about 10,000 times; printing it all would flood the frontend.
25- SKIPPED_NAMES =
Most skipped file names printed; the rest are counted.
;hmr xmlalone skips about 34 rexml files. 5- RUBY_LIB_DIR_KEYS =
RbConfig keys for Ruby's own library directories.
%w[rubylibdir rubyarchdir sitelibdir sitearchdir vendorlibdir vendorarchdir].freeze
Class Method Summary collapse
-
.clear_cache ⇒ void
private
Clears cached gem paths to ensure the next reload picks up newly installed gems.
-
.foreign?(path, roots) ⇒ Boolean
True for a loaded file that is not Lich's to reload: part of Ruby itself or of an installed gem.
-
.foreign_roots ⇒ Array<String>
Directories holding Ruby's standard library and installed gems, resolved through symlinks so they compare with resolved file paths.
-
.format_backtrace(backtrace) ⇒ String
At most BACKTRACE_LINES lines, then a count of the rest.
-
.loaded ⇒ Array<String>
private
Returns all currently loaded Ruby files.
-
.msg(message) ⇒ void
private
Sends a message to the game client with visual formatting.
-
.reload(pattern) ⇒ void
private
Reloads all loaded script files matching a regex pattern, excluding Ruby standard library and gem files.
-
.skipped_message(skipped) ⇒ String
The count, the first SKIPPED_NAMES basenames, and how many more were left out.
Class Method Details
.clear_cache ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clears cached gem paths to ensure the next reload picks up newly installed gems.
38 39 40 |
# File 'documented/common/hmr.rb', line 38 def self.clear_cache Gem.clear_paths end |
.foreign?(path, roots) ⇒ Boolean
True for a loaded file that is not Lich's to reload: part of Ruby
itself or of an installed gem. Their bodies are not written to be
re-run; reloading rexml (which ;hmr xml matches) overflows the stack
and leaves REXML broken for the rest of the session.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'documented/common/hmr.rb', line 93 def self.foreign?(path, roots) # Ruby records some built-in features (thread.rb, set.rb) without a # path; load would resolve them against $LOAD_PATH. return true unless File.absolute_path?(path) real = begin File.realpath(path) rescue SystemCallError path end.b roots.any? { |root| real.start_with?(root) } end |
.foreign_roots ⇒ Array<String>
Directories holding Ruby's standard library and installed gems, resolved through symlinks so they compare with resolved file paths. Returned as bytes (BINARY): RbConfig's strings are BINARY and feature paths are UTF-8. Once both hold a non-ASCII character (Ruby and Lich both under a home directory whose name is not ASCII), String#start_with? raises Encoding::CompatibilityError instead of comparing them.
74 75 76 77 78 79 80 81 82 83 |
# File 'documented/common/hmr.rb', line 74 def self.foreign_roots dirs = RUBY_LIB_DIR_KEYS.map { |key| RbConfig::CONFIG[key] } dirs.concat(Gem.path) dirs.concat(Gem.loaded_specs.values.map(&:full_gem_path)) dirs.compact.reject(&:empty?).filter_map do |dir| File.join(File.realpath(dir), '').b rescue SystemCallError nil end.uniq end |
.format_backtrace(backtrace) ⇒ String
Returns at most BACKTRACE_LINES lines, then a count of the rest.
108 109 110 111 112 113 114 |
# File 'documented/common/hmr.rb', line 108 def self.format_backtrace(backtrace) lines = Array(backtrace) shown = lines.first(BACKTRACE_LINES) hidden = lines.size - shown.size shown << "... #{hidden} more backtrace lines" if hidden.positive? shown.join("\n") end |
.loaded ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns all currently loaded Ruby files.
61 62 63 |
# File 'documented/common/hmr.rb', line 61 def self.loaded $LOADED_FEATURES.select { |path| path.end_with?(".rb") } end |
.msg(message) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Sends a message to the game client with visual formatting.
Uses the client's high-fidelity _respond method if available and the message contains
HTML tags; falls back to respond for plain text; outputs to stdout if neither method
is defined.
51 52 53 54 55 |
# File 'documented/common/hmr.rb', line 51 def self.msg() return _respond if defined?(:_respond) && .include?("<b>") return respond if defined?(:respond) puts end |
.reload(pattern) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Reloads all loaded script files matching a regex pattern, excluding Ruby standard library and gem files.
Clears the gem cache, then finds all .rb files in $LOADED_FEATURES that match the pattern. Files belonging to Ruby's standard library or installed gems are skipped with a message listing the first few skipped names. For each user script or Lich module that is reloaded, a success message is printed; if reloading raises StandardError, ScriptError, or SystemStackError, the exception message and backtrace (capped at 25 lines) are printed and execution continues to the next file.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'documented/common/hmr.rb', line 140 def self.reload(pattern) self.clear_cache matched = self.loaded.grep(pattern) roots = self.foreign_roots unless matched.empty? skipped, loaded_paths = matched.partition { |file| self.foreign?(file, roots) } unless skipped.empty? self.msg self.(skipped) end unless loaded_paths.empty? loaded_paths.each { |file| begin load(file) self.msg "<b>[lich.hmr] reloaded %s</b>" % file rescue *RELOAD_ERRORS => exception self.msg exception. self.msg self.format_backtrace(exception.backtrace) end } else self.msg "<b>[lich.hmr] nothing matching regex pattern: %s</b>" % pattern.source if skipped.empty? end end |
.skipped_message(skipped) ⇒ String
Returns the count, the first SKIPPED_NAMES basenames, and how many more were left out.
119 120 121 122 123 124 |
# File 'documented/common/hmr.rb', line 119 def self.(skipped) names = skipped.first(SKIPPED_NAMES).map { |file| File.basename(file) }.join(", ") more = skipped.size - SKIPPED_NAMES names += " and #{more} more" if more.positive? "<b>[lich.hmr] skipped %d file(s) that belong to Ruby or an installed gem: %s</b>" % [skipped.size, names] end |