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.



350
351
352
353
# File 'lib/rhino/context.rb', line 350

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

Instance Attribute Details

#instruction_countObject (readonly)

Returns the value of attribute instruction_count.



365
366
367
# File 'lib/rhino/context.rb', line 365

def instruction_count
  @instruction_count
end

#instruction_limitObject

Returns the value of attribute instruction_limit.



355
356
357
# File 'lib/rhino/context.rb', line 355

def instruction_limit
  @instruction_limit
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



379
380
381
# File 'lib/rhino/context.rb', line 379

def start_time
  @start_time
end

#timeout_limitObject

Returns the value of attribute timeout_limit.



369
370
371
# File 'lib/rhino/context.rb', line 369

def timeout_limit
  @timeout_limit
end

Instance Method Details

#check!(count = nil) ⇒ Object



381
382
383
384
385
# File 'lib/rhino/context.rb', line 381

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

#check_instruction_limit!Object



387
388
389
390
391
# File 'lib/rhino/context.rb', line 387

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



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/rhino/context.rb', line 393

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



414
415
416
417
418
# File 'lib/rhino/context.rb', line 414

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