Module: Kernel
- Defined in:
- lib/runtime/kernel.rb
Overview
The Kernel module
Defined Under Namespace
Classes: Cache
Constant Summary collapse
- Index =
Cache.new({}, {})
- NamespaceDelimiterPattern =
%r{::}.freeze
- JavaPattern =
%r{java}.freeze
- DefineNewRand =
%(alias :original_rand :rand def rand(ignore=0) %<n>s end ).freeze
- ResetOriginalRand =
%(alias :rand :original_rand remove_method :original_rand ).freeze
Instance Method Summary collapse
-
#attempt_twice(&_block) ⇒ Object
rubocop: disable Metrics/MethodLength.
- #Attribute(attribute) ⇒ Object
- #child(obj = nil) ⇒ Object
- #children(obj = nil, prop = :@children) ⇒ Object
- #class_loader ⇒ Object
-
#collect_garbage ⇒ Object
rubocop: disable Metrics/AbcSize.
- #Constant(property, value) ⇒ Object
- #Default(property, value) ⇒ Object
- #destroy(obj = nil) ⇒ Object
- #find_class(class_name) ⇒ Object
- #find_module(module_name) ⇒ Object
-
#get_constant(constant_name, target = Object) ⇒ Object
rubocop: disable Metrics/MethodLength.
- #give(obj = nil, *attributes) ⇒ Object
- #import_global(symbol) ⇒ Object
- #Include(inclusion) ⇒ Object
- #indent(stack_depth = [0, caller(2).length - 9].max) ⇒ Object
- #java? ⇒ Boolean
- #jump(symbol) ⇒ Object
- #kneel(obj) ⇒ Object
- #label(symbol, &_block) ⇒ Object
- #Link(link) ⇒ Object
- #load_service ⇒ Object
- #memory_usage ⇒ Object
- #metaclass(obj) ⇒ Object
-
#move(obj, new_parent) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity.
- #object_space ⇒ Object
- #override(replacement, method_name) ⇒ Object
-
#parent(obj = nil, prop = :@parent, &block) ⇒ Object
rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity.
- #parent_selector(obj, &block) ⇒ Object
- #Property(property_or_modifier, modifier = nil, value = nil) ⇒ Object
- #recline(obj) ⇒ Object
- #reload_feature(feature) ⇒ Object
- #reset_constant(constant, value, target = Object) ⇒ Object
-
#reset_runtime ⇒ Object
rubocop: disable Metrics/AbcSize.
-
#return_after(*args, &block) ⇒ Object
TODO: Remove def with(context = nil, &block) if context.nil? self.instance_eval(&block) else self.instance_exec(context, &block) end self.save if self.respond_to?(:save) self end.
- #setrandom(n) ⇒ Object
-
#sibling(obj = nil) ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity.
- #silence_warnings(&_block) ⇒ Object
- #sit(obj) ⇒ Object
-
#stand(obj) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity.
- #take(obj = nil, *attributes) ⇒ Object
- #toggle_constant(constant, target = ::Object) ⇒ Object
Instance Method Details
#attempt_twice(&_block) ⇒ Object
rubocop: disable Metrics/MethodLength
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/runtime/kernel.rb', line 144 def attempt_twice(&_block) tries ||= 2 return yield rescue StandardError => e tries -= 1 if tries > 0 log.warn e. retry else log.error e return nil end end |
#Attribute(attribute) ⇒ Object
45 46 47 |
# File 'lib/runtime/kernel.rb', line 45 def Attribute(attribute) Inform::Runtime.Attribute(attribute) end |
#child(obj = nil) ⇒ Object
308 309 310 311 312 313 |
# File 'lib/runtime/kernel.rb', line 308 def child(obj = nil) return nil if obj.nil? return nil unless obj.respond_to?(:child) obj.refresh if obj.respond_to?(:refresh) obj.child.refresh end |
#children(obj = nil, prop = :@children) ⇒ Object
347 348 349 350 351 352 353 354 355 356 |
# File 'lib/runtime/kernel.rb', line 347 def children(obj = nil, prop = :@children) return 0 if obj.nil? obj.refresh if obj.respond_to?(:refresh) if defined? db return db.run('SELECT count(id) from "object" where parent_id = ' + obj.id) unless sysobj? end return obj.instance_variable_get(prop).length if obj.instance_variable_defined?(prop) return unless obj.respond_to?(:children) obj.children.length end |
#class_loader ⇒ Object
188 189 190 |
# File 'lib/runtime/kernel.rb', line 188 def class_loader JRuby.runtime.jruby_class_loader end |
#collect_garbage ⇒ Object
rubocop: disable Metrics/AbcSize
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/runtime/kernel.rb', line 217 def collect_garbage java.lang.System.gc() sleep 0.1 java.lang.System.runFinalization() sleep 0.1 rescue java.lang.Throwable => e log.error "Unexpected java error invoking garbage collection: #{e}", e rescue StandardError => e log.error "Unexpected error invoking garbage collection: #{e}", e end |
#Constant(property, value) ⇒ Object
57 58 59 |
# File 'lib/runtime/kernel.rb', line 57 def Constant(property, value) Inform::Runtime.Constant(property, value) end |
#Default(property, value) ⇒ Object
53 54 55 |
# File 'lib/runtime/kernel.rb', line 53 def Default(property, value) Inform::Runtime.Default(property, value) end |
#destroy(obj = nil) ⇒ Object
358 359 360 361 362 363 364 |
# File 'lib/runtime/kernel.rb', line 358 def destroy(obj = nil) return if obj.nil? return unless obj.respond_to?(:has?) return if obj.has? :prized return unless obj.respond_to?(:destroy) obj.destroy end |
#find_class(class_name) ⇒ Object
136 137 138 139 140 |
# File 'lib/runtime/kernel.rb', line 136 def find_class(class_name) klass = Index.classes[class_name] klass ||= get_constant(class_name) Index.classes[class_name] = klass end |
#find_module(module_name) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/runtime/kernel.rb', line 124 def find_module(module_name) module_name = "::#{module_name}" unless NamespaceDelimiterPattern.match?(module_name) mod = Index.modules[module_name] # TODO: Test # return mod if mod.is_a?(Module) mod ||= get_constant(module_name) return nil if mod.is_a?(Class) Index.modules[module_name] = mod end |
#get_constant(constant_name, target = Object) ⇒ Object
rubocop: disable Metrics/MethodLength
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/runtime/kernel.rb', line 104 def get_constant(constant_name, target = Object) constant = nil begin constant = target.const_get(constant_name) rescue NameError => e log.error "Undefined constant #{constant_name}: #{e}", e rescue SyntaxError => e log.error "Unexpected syntax error getting constant #{constant_name}", e rescue StandardError => e log.error "Unexpected error getting constant #{constant_name}", e end constant end |
#give(obj = nil, *attributes) ⇒ Object
366 367 368 369 370 |
# File 'lib/runtime/kernel.rb', line 366 def give(obj = nil, *attributes) return if obj.nil? return unless obj.respond_to?(:tag) obj.tag(*attributes) end |
#import_global(symbol) ⇒ Object
32 33 34 |
# File 'lib/runtime/kernel.rb', line 32 def import_global(symbol) Inform::Runtime.import_global(symbol) end |
#Include(inclusion) ⇒ Object
40 41 42 43 |
# File 'lib/runtime/kernel.rb', line 40 def Include(inclusion) Inform::Runtime.instance.main_object = self if inclusion == "Parser" Inform::Runtime.Include(inclusion) end |
#indent(stack_depth = [0, caller(2).length - 9].max) ⇒ Object
160 161 162 |
# File 'lib/runtime/kernel.rb', line 160 def indent(stack_depth = [0, caller(2).length - 9].max) ' ' * stack_depth end |
#java? ⇒ Boolean
204 205 206 |
# File 'lib/runtime/kernel.rb', line 204 def java? JavaPattern.match?(RUBY_PLATFORM) end |
#jump(symbol) ⇒ Object
282 283 284 |
# File 'lib/runtime/kernel.rb', line 282 def jump(symbol) throw symbol end |
#kneel(obj) ⇒ Object
409 410 411 412 413 |
# File 'lib/runtime/kernel.rb', line 409 def kneel(obj) give obj, :kneeling unless obj.has? :kneeling take obj, :sitting take obj, :prone end |
#label(symbol, &_block) ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/runtime/kernel.rb', line 272 def label(symbol, &_block) loop do catch symbol do yield break end end end |
#Link(link) ⇒ Object
36 37 38 |
# File 'lib/runtime/kernel.rb', line 36 def Link(link) Inform::Runtime.Link(link) end |
#load_service ⇒ Object
193 194 195 |
# File 'lib/runtime/kernel.rb', line 193 def load_service JRuby.runtime.load_service end |
#memory_usage ⇒ Object
209 210 211 212 213 |
# File 'lib/runtime/kernel.rb', line 209 def memory_usage total_memory = java.lang.Runtime.getRuntime().totalMemory() free_memory = java.lang.Runtime.getRuntime().freeMemory() total_memory - free_memory end |
#metaclass(obj) ⇒ Object
246 247 248 |
# File 'lib/runtime/kernel.rb', line 246 def (obj) obj.class. end |
#move(obj, new_parent) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity
381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/runtime/kernel.rb', line 381 def move(obj, new_parent) return if obj.nil? return unless new_parent.respond_to?(:<<) return unless new_parent.respond_to?(:in?) new_parent.safe_refresh if new_parent.respond_to?(:safe_refresh) obj.safe_refresh if obj.respond_to?(:safe_refresh) raise CycleError if new_parent.in? obj obj.remove if obj.respond_to?(:remove) new_parent << obj obj.safe_refresh if obj.respond_to?(:safe_refresh) new_parent.safe_refresh if new_parent.respond_to?(:safe_refresh) end |
#object_space ⇒ Object
198 199 200 |
# File 'lib/runtime/kernel.rb', line 198 def object_space JRuby.runtime.object_space end |
#override(replacement, method_name) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/runtime/kernel.rb', line 287 def override(replacement, method_name) new_method = nil begin old_method = self.instance_method(method_name) remove_method(method_name) if old_method.arity > 0 new_method = define_method(replacement.to_s, old_method) rescue NameError => e log.error "Error overriding method #{method_name}: #{e}", e end new_method end |
#parent(obj = nil, prop = :@parent, &block) ⇒ Object
rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/runtime/kernel.rb', line 326 def parent(obj = nil, prop = :@parent, &block) return nil if obj.nil? obj.refresh if obj.respond_to?(:refresh) obj_parent = nil obj_parent = select_parent(obj, &block) if block_given? obj_parent ||= obj.instance_variable_get(prop) if obj.instance_variable_defined?(prop) obj_parent ||= obj.parent if obj.respond_to?(:parent) return nil if obj_parent.nil? obj_parent.refresh if obj_parent.respond_to?(:refresh) obj_parent end |
#parent_selector(obj, &block) ⇒ Object
315 316 317 318 319 320 321 322 |
# File 'lib/runtime/kernel.rb', line 315 def parent_selector(obj, &block) return nil if obj.nil? loop do obj = obj.parent break if block.call(obj) end obj end |
#Property(property_or_modifier, modifier = nil, value = nil) ⇒ Object
49 50 51 |
# File 'lib/runtime/kernel.rb', line 49 def Property(property_or_modifier, modifier = nil, value = nil) Inform::Runtime.Property(property_or_modifier, modifier, value) end |
#recline(obj) ⇒ Object
415 416 417 418 419 |
# File 'lib/runtime/kernel.rb', line 415 def recline(obj) give obj, :prone unless obj.has? :prone take obj, :sitting take obj, :kneeling end |
#reload_feature(feature) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/runtime/kernel.rb', line 176 def reload_feature(feature) silence_warnings do # Remove a loaded feature: load_service.remove_internal_loaded_feature(feature) # Supposedly, purge old compiled script code: reset_runtime # Reload the feature: load_service.load(feature, true) end end |
#reset_constant(constant, value, target = Object) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/runtime/kernel.rb', line 78 def reset_constant(constant, value, target = Object) return if constant.nil? silence_warnings do target.instance_eval { const_set(constant.to_sym, value) } end rescue StandardError => e log.error "Unexpected error resetting constant #{constant}", e nil end |
#reset_runtime ⇒ Object
rubocop: disable Metrics/AbcSize
166 167 168 169 170 171 172 |
# File 'lib/runtime/kernel.rb', line 166 def reset_runtime org.jruby.Ruby.new_instance().use_as_global_runtime() engine = javax.script.ScriptEngineManager.new().get_engine_by_name('jruby') # rubocop: disable Style/RedundantInterpolation log.warn "#{(engine.methods - Object.instance_methods).uniq.sort.inspect}" # rubocop: enable Style/RedundantInterpolation end |
#return_after(*args, &block) ⇒ Object
TODO: Remove def with(context = nil, &block)
if context.nil?
self.instance_eval(&block)
else
self.instance_exec(context, &block)
end
self.save if self.respond_to?(:save)
self
end
72 73 74 75 |
# File 'lib/runtime/kernel.rb', line 72 def return_after(*args, &block) block.call args.first end |
#setrandom(n) ⇒ Object
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/runtime/kernel.rb', line 260 def setrandom(n) puts "--------------------" # TODO: Remove puts "Before setrandom Kernel.respond_to?(:original_rand) #{Kernel.respond_to?(:original_rand)}" # TODO: Remove code = ResetOriginalRand if Kernel.respond_to?(:original_rand) code ||= format(DefineNewRand, n: n) log.debug "code:\n#{code}" # TODO: Remove Kernel.module_eval(code) ensure puts "After setrandom Kernel.respond_to?(:original_rand) #{Kernel.respond_to?(:original_rand)}" end |
#sibling(obj = nil) ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity
340 341 342 343 344 345 |
# File 'lib/runtime/kernel.rb', line 340 def sibling(obj = nil) return nil if obj.nil? return nil unless obj.respond_to?(:sibling) obj.refresh if obj.respond_to?(:refresh) obj.sibling.refresh end |
#silence_warnings(&_block) ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/runtime/kernel.rb', line 300 def silence_warnings(&_block) old_verbose = $VERBOSE $VERBOSE = nil yield ensure $VERBOSE = old_verbose end |
#sit(obj) ⇒ Object
403 404 405 406 407 |
# File 'lib/runtime/kernel.rb', line 403 def sit(obj) give obj, :sitting unless obj.has? :sitting take obj, :kneeling take obj, :prone end |
#stand(obj) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity
397 398 399 400 401 |
# File 'lib/runtime/kernel.rb', line 397 def stand(obj) take obj, :sitting take obj, :kneeling take obj, :prone end |
#take(obj = nil, *attributes) ⇒ Object
372 373 374 375 376 |
# File 'lib/runtime/kernel.rb', line 372 def take(obj = nil, *attributes) return if obj.nil? return unless obj.respond_to?(:untag) obj.untag(*attributes) end |
#toggle_constant(constant, target = ::Object) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/runtime/kernel.rb', line 89 def toggle_constant(constant, target = ::Object) silence_warnings do if target.const_defined?(constant) target.instance_eval { remove_const(constant) } else target.instance_eval { const_set(constant, true) } end end rescue StandardError => e log.error "Unexpected error toggling constant #{constant}", e nil end |