Class: MiniRacer::Context
- Inherits:
-
Object
- Object
- MiniRacer::Context
- Defined in:
- lib/mini_racer/shared.rb,
lib/mini_racer.rb,
lib/mini_racer/truffleruby.rb,
ext/mini_racer_extension/mini_racer_extension.c
Overview
eval is defined in the C class
Defined Under Namespace
Classes: ExternalFunction
Instance Method Summary collapse
- #attach(name, callback) ⇒ Object
- #call(function_name, *arguments) ⇒ Object
- #call_await ⇒ Object
- #dispose ⇒ Object
- #eval(str, options = nil) ⇒ Object
- #eval_await ⇒ Object
- #heap_stats ⇒ Object
-
#initialize(max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: nil) ⇒ Context
constructor
A new instance of Context.
- #isolate ⇒ Object
- #load(filename) ⇒ Object
- #low_memory_notification ⇒ Object
- #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) ⇒ Context
Returns a new instance of Context.
101 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 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mini_racer/shared.rb', line 101 def initialize( max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: 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 ) @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
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/mini_racer/shared.rb', line 209 def attach(name, callback) if @disposed raise( ContextDisposedError, "attempted to call function on a disposed context!" ) end 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 { sleep 2 if @thread_raise_called } 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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/mini_racer/shared.rb', line 174 def call(function_name, *arguments) if @disposed raise( ContextDisposedError, "attempted to call function on a disposed context!" ) end @eval_thread = Thread.current isolate_mutex.synchronize do timeout { call_unsafe(function_name, *arguments) } end ensure @eval_thread = nil ensure_gc_thread if @ensure_gc_after_idle end |
#call_await ⇒ Object
195 196 197 |
# File 'lib/mini_racer/shared.rb', line 195 def call_await(*, **) raise MiniRacer::Error, "call_await is not supported on TruffleRuby" end |
#dispose ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/mini_racer/shared.rb', line 199 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 |
#eval(str, options = nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mini_racer/shared.rb', line 154 def eval(str, = nil) if @disposed raise( ContextDisposedError, "attempted to call eval on a disposed context!" ) end filename = && [:filename].to_s @eval_thread = Thread.current isolate_mutex.synchronize do @current_exception = nil timeout { eval_unsafe(str, filename) } end ensure @eval_thread = nil ensure_gc_thread if @ensure_gc_after_idle end |
#eval_await ⇒ Object
191 192 193 |
# File 'lib/mini_racer/shared.rb', line 191 def eval_await(*, **) raise MiniRacer::Error, "eval_await is not supported on TruffleRuby" end |
#heap_stats ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mini_racer/truffleruby.rb', line 48 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
148 149 150 151 152 |
# File 'lib/mini_racer/shared.rb', line 148 def isolate return @isolate if @isolate != false # defined in the C class @isolate = create_isolate_value end |
#load(filename) ⇒ Object
100 101 102 |
# File 'lib/mini_racer.rb', line 100 def load(filename) eval(File.read(filename)) end |
#low_memory_notification ⇒ Object
67 68 69 |
# File 'lib/mini_racer/truffleruby.rb', line 67 def low_memory_notification GC.start end |
#stop ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/mini_racer/truffleruby.rb', line 59 def stop if @entered @context.stop @stopped = true stop_attached end end |
#write_heap_snapshot(file_or_io) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mini_racer.rb', line 104 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 |