Class: Rhino::RestrictableContextFactory::Context

Inherits:
JS::Context
  • Object
show all
Defined in:
lib/rhino/context.rb

Overview

:nodoc:

Constant Summary collapse

TIMEOUT_INSTRUCTION_TRESHOLD =
42

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory) ⇒ Context

Returns a new instance of Context.



292
293
294
295
# File 'lib/rhino/context.rb', line 292

def initialize(factory)
  super(factory)
  reset!
end

Instance Attribute Details

#instruction_countObject (readonly)

Returns the value of attribute instruction_count.



307
308
309
# File 'lib/rhino/context.rb', line 307

def instruction_count
  @instruction_count
end

#instruction_limitObject

Returns the value of attribute instruction_limit.



297
298
299
# File 'lib/rhino/context.rb', line 297

def instruction_limit
  @instruction_limit
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



321
322
323
# File 'lib/rhino/context.rb', line 321

def start_time
  @start_time
end

#timeout_limitObject

Returns the value of attribute timeout_limit.



311
312
313
# File 'lib/rhino/context.rb', line 311

def timeout_limit
  @timeout_limit
end

Instance Method Details

#check!(count = nil) ⇒ Object



323
324
325
326
327
# File 'lib/rhino/context.rb', line 323

def check!(count = nil)
  @instruction_count += count if count
  check_instruction_limit!
  check_timeout_limit!(count)
end

#check_instruction_limit!Object



329
330
331
332
333
# File 'lib/rhino/context.rb', line 329

def check_instruction_limit!
  if instruction_limit && instruction_count > instruction_limit
    raise RunawayScriptError, "script exceeded allowable instruction count: #{instruction_limit}"
  end
end

#check_timeout_limit!(count = nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rhino/context.rb', line 335

def check_timeout_limit!(count = nil)
  if timeout_limit
    elapsed_time = Time.now.to_f - start_time.to_f
    if elapsed_time > timeout_limit
      raise ScriptTimeoutError, "script exceeded timeout: #{timeout_limit} seconds"
    end
    # adapt instruction treshold as needed :
    if count
      treshold = getInstructionObserverThreshold
      if elapsed_time * 2 < timeout_limit
        next_treshold_guess = treshold * 2
        if instruction_limit && instruction_limit < next_treshold_guess
          setInstructionObserverThreshold(instruction_limit)
        else
          setInstructionObserverThreshold(next_treshold_guess)
        end
      end
    end
  end
end

#reset!Object



356
357
358
359
360
# File 'lib/rhino/context.rb', line 356

def reset!
  @instruction_count = 0
  @start_time = Time.now
  self
end