Class: MiniRacer::Context
- Inherits:
-
Object
- Object
- MiniRacer::Context
- Defined in:
- lib/mini_racer/shared.rb,
lib/mini_racer.rb,
lib/mini_racer/truffleruby.rb,
lib/mini_racer/truffleruby.rb,
ext/mini_racer_extension/mini_racer_extension.c
Overview
GraalJS has no per-script bytecode cache reachable from Polyglot::InnerContext#eval, so cached_data: is silently ignored and Script#run replays the source through Context#eval. compile_module + dynamic_import_resolver raise NotImplementedError because GraalJS has its own module-loading mechanism that doesn’t map onto this handle-based API. The MiniRacer::Module class itself is stubbed so cross-engine code can reference it for is_a? / rescue without tripping NameError.
Defined Under Namespace
Classes: ExternalFunction
Instance Method Summary collapse
- #attach(name, callback) ⇒ Object
- #call(function_name, *arguments) ⇒ Object
- #compile(source, filename: nil, cached_data: nil, produce_cache: false) ⇒ Object
- #compile_module(*_args, **_opts) ⇒ Object
- #dispose ⇒ Object
- #dynamic_import_resolver ⇒ Object
-
#dynamic_import_resolver=(blk) ⇒ Object
nil is the documented “disable” value; accept it as a no-op so that ‘ctx.dynamic_import_resolver ||= …` style code doesn’t crash on TruffleRuby.
- #eval(str, options = nil) ⇒ Object
- #heap_stats ⇒ Object
-
#initialize(max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: nil, host_namespace: nil) ⇒ Context
constructor
A new instance of Context.
- #isolate ⇒ Object
- #load(filename) ⇒ Object
- #load_module_graph(*_args, **_opts) ⇒ Object
- #low_memory_notification ⇒ Object
-
#reset_realm ⇒ Object
reset_realm tears down the JavaScript realm while keeping the warm isolate.
- #stop ⇒ Object
- #write_heap_snapshot(file_or_io) ⇒ Object
Constructor Details
#initialize(max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: nil, host_namespace: nil) ⇒ Context
Returns a new instance of Context.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mini_racer/shared.rb', line 102 def initialize(max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: nil, host_namespace: nil) ||= {} (isolate: isolate, snapshot: snapshot, max_memory: max_memory, marshal_stack_depth: marshal_stack_depth, ensure_gc_after_idle: ensure_gc_after_idle, timeout: timeout, host_namespace: host_namespace) @functions = {} @timeout = nil @max_memory = nil @current_exception = nil @timeout = timeout @max_memory = max_memory @marshal_stack_depth = marshal_stack_depth # false signals it should be fetched if requested @isolate = isolate || false @ensure_gc_after_idle = ensure_gc_after_idle if @ensure_gc_after_idle @last_eval = nil @ensure_gc_thread = nil @ensure_gc_mutex = Mutex.new end @disposed = false @callback_mutex = Mutex.new @callback_running = false @thread_raise_called = false @eval_thread = nil # defined in the C class init_unsafe(isolate, snapshot) end |
Instance Method Details
#attach(name, callback) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/mini_racer/shared.rb', line 185 def attach(name, callback) raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed wrapped = lambda do |*args| begin r = nil begin @callback_mutex.synchronize{ @callback_running = true } r = callback.call(*args) ensure @callback_mutex.synchronize{ @callback_running = false } end # wait up to 2 seconds for this to be interrupted # will very rarely be called cause #raise is called # in another mutex @callback_mutex.synchronize { if @thread_raise_called sleep 2 end } r ensure @callback_mutex.synchronize { @thread_raise_called = false } end end isolate_mutex.synchronize do external = ExternalFunction.new(name, wrapped, self) @functions["#{name}"] = external end end |
#call(function_name, *arguments) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mini_racer/shared.rb', line 160 def call(function_name, *arguments) raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed @eval_thread = Thread.current isolate_mutex.synchronize do timeout do call_unsafe(function_name, *arguments) end end ensure @eval_thread = nil ensure_gc_thread if @ensure_gc_after_idle end |
#compile(source, filename: nil, cached_data: nil, produce_cache: false) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/mini_racer/truffleruby.rb', line 405 def compile(source, filename: nil, cached_data: nil, produce_cache: false) raise(ContextDisposedError, 'attempted to call compile on a disposed context!') if @disposed raise TypeError, "wrong type argument #{source.class} (should be a string)" unless source.is_a?(String) raise TypeError, "wrong type argument #{filename.class} (should be a string)" unless filename.nil? || filename.is_a?(String) if cached_data raise TypeError, "wrong type argument #{cached_data.class} (should be a string)" unless cached_data.is_a?(String) raise EncodingError, "cached_data must be ASCII-8BIT (binary), got #{cached_data.encoding}" if cached_data.encoding != Encoding::ASCII_8BIT end # produce_cache is accepted for API parity but has no effect — the shim # has no per-script bytecode cache to produce. Script.send(:new, self, source, filename) end |
#compile_module(*_args, **_opts) ⇒ Object
418 419 420 421 |
# File 'lib/mini_racer/truffleruby.rb', line 418 def compile_module(*_args, **_opts) raise NotImplementedError, 'Context#compile_module is not supported on TruffleRuby' end |
#dispose ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/mini_racer/shared.rb', line 174 def dispose return if @disposed isolate_mutex.synchronize do return if @disposed dispose_unsafe @disposed = true @isolate = nil # allow it to be garbage collected, if set end end |
#dynamic_import_resolver ⇒ Object
437 |
# File 'lib/mini_racer/truffleruby.rb', line 437 def dynamic_import_resolver = nil |
#dynamic_import_resolver=(blk) ⇒ Object
nil is the documented “disable” value; accept it as a no-op so that ‘ctx.dynamic_import_resolver ||= …` style code doesn’t crash on TruffleRuby. Any callable raises, mirroring ‘compile_module`.
431 432 433 434 435 |
# File 'lib/mini_racer/truffleruby.rb', line 431 def dynamic_import_resolver=(blk) return blk if blk.nil? raise NotImplementedError, 'Context#dynamic_import_resolver= is not supported on TruffleRuby' end |
#eval(str, options = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mini_racer/shared.rb', line 143 def eval(str, =nil) raise(ContextDisposedError, 'attempted to call eval on a disposed context!') if @disposed filename = && [:filename].to_s @eval_thread = Thread.current isolate_mutex.synchronize do @current_exception = nil timeout do eval_unsafe(str, filename) end end ensure @eval_thread = nil ensure_gc_thread if @ensure_gc_after_idle end |
#heap_stats ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mini_racer/truffleruby.rb', line 51 def heap_stats raise ContextDisposedError if @disposed { total_physical_size: 0, total_heap_size_executable: 0, total_heap_size: 0, used_heap_size: 0, heap_size_limit: 0, } end |
#isolate ⇒ Object
137 138 139 140 141 |
# File 'lib/mini_racer/shared.rb', line 137 def isolate return @isolate if @isolate != false # defined in the C class @isolate = create_isolate_value end |
#load(filename) ⇒ Object
95 96 97 |
# File 'lib/mini_racer.rb', line 95 def load(filename) eval(File.read(filename)) end |
#load_module_graph(*_args, **_opts) ⇒ Object
423 424 425 426 |
# File 'lib/mini_racer/truffleruby.rb', line 423 def load_module_graph(*_args, **_opts) raise NotImplementedError, 'Context#load_module_graph is not supported on TruffleRuby' end |
#low_memory_notification ⇒ Object
70 71 72 |
# File 'lib/mini_racer/truffleruby.rb', line 70 def low_memory_notification GC.start end |
#reset_realm ⇒ Object
reset_realm tears down the JavaScript realm while keeping the warm isolate. It depends on V8’s isolate/realm split, which the TruffleRuby (Graal.js) backend does not expose, so it is unavailable here.
231 232 233 |
# File 'lib/mini_racer/shared.rb', line 231 def reset_realm raise NotImplementedError, "reset_realm is only available on the V8 backend" end |
#stop ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/mini_racer/truffleruby.rb', line 62 def stop if @entered @context.stop @stopped = true stop_attached end end |
#write_heap_snapshot(file_or_io) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mini_racer.rb', line 99 def write_heap_snapshot(file_or_io) f = nil implicit = false if String === file_or_io f = File.open(file_or_io, "w") implicit = true else f = file_or_io end raise ArgumentError, "file_or_io" unless File === f f.write(heap_snapshot()) ensure f.close if implicit end |