Class: MiniRacer::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_racer.rb,
lib/mini_racer/truffleruby.rb

Overview

eval is defined in the C class

Defined Under Namespace

Classes: ExternalFunction

Instance Method Summary collapse

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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mini_racer.rb', line 151

def initialize(max_memory: nil, timeout: nil, isolate: nil, ensure_gc_after_idle: nil, snapshot: nil, marshal_stack_depth: nil)
  options ||= {}

  check_init_options!(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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/mini_racer.rb', line 261

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



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/mini_racer.rb', line 236

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

#disposeObject



250
251
252
253
254
255
256
257
258
# File 'lib/mini_racer.rb', line 250

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/mini_racer.rb', line 219

def eval(str, options=nil)
  raise(ContextDisposedError, 'attempted to call eval on a disposed context!') if @disposed

  filename = options && options[: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_statsObject



45
46
47
48
49
50
51
52
53
# File 'lib/mini_racer/truffleruby.rb', line 45

def heap_stats
  {
    total_physical_size: 0,
    total_heap_size_executable: 0,
    total_heap_size: 0,
    used_heap_size: 0,
    heap_size_limit: 0,
  }
end

#isolateObject



186
187
188
189
190
# File 'lib/mini_racer.rb', line 186

def isolate
  return @isolate if @isolate != false
  # defined in the C class
  @isolate = create_isolate_value
end

#load(filename) ⇒ Object



192
193
194
195
# File 'lib/mini_racer.rb', line 192

def load(filename)
  # TODO do this native cause no need to allocate VALUE here
  eval(File.read(filename))
end

#stopObject



55
56
57
58
59
60
61
# File 'lib/mini_racer/truffleruby.rb', line 55

def stop
  if @entered
    @context.stop
    @stopped = true
    stop_attached
  end
end

#write_heap_snapshot(file_or_io) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/mini_racer.rb', line 197

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

  if !(File === f)
    raise ArgumentError, "file_or_io"
  end

  write_heap_snapshot_unsafe(f)

ensure
  f.close if implicit
end