2010-08-10 15:58:11 -04:00
|
|
|
require 'stringio'
|
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
module Rhino
|
2009-11-11 21:12:20 -05:00
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
# ==Overview
|
|
|
|
# All Javascript must be executed in a context which represents the execution environment in
|
|
|
|
# which scripts will run. The environment consists of the standard javascript objects
|
|
|
|
# and functions like Object, String, Array, etc... as well as any objects or functions which
|
|
|
|
# have been defined in it. e.g.
|
|
|
|
#
|
|
|
|
# Context.open do |cxt|
|
|
|
|
# cxt['num'] = 5
|
|
|
|
# cxt.eval('num + 5') #=> 10
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# == Multiple Contexts.
|
|
|
|
# The same object may appear in any number of contexts, but only one context may be executing javascript code
|
|
|
|
# in any given thread. If a new context is opened in a thread in which a context is already opened, the second
|
|
|
|
# context will "mask" the old context e.g.
|
|
|
|
#
|
|
|
|
# six = 6
|
|
|
|
# Context.open do |cxt|
|
|
|
|
# cxt['num'] = 5
|
|
|
|
# cxt.eval('num') # => 5
|
|
|
|
# Context.open do |cxt|
|
|
|
|
# cxt['num'] = 10
|
|
|
|
# cxt.eval('num') # => 10
|
|
|
|
# cxt.eval('++num') # => 11
|
|
|
|
# end
|
|
|
|
# cxt.eval('num') # => 5
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# == Notes
|
|
|
|
# While there are many similarities between Rhino::Context and Java::org.mozilla.javascript.Context, they are not
|
|
|
|
# the same thing and should not be confused.
|
2009-12-02 01:35:16 -05:00
|
|
|
class Context
|
2009-09-24 20:06:31 -04:00
|
|
|
|
|
|
|
class << self
|
2009-12-02 01:35:16 -05:00
|
|
|
|
2009-11-15 21:56:31 -05:00
|
|
|
# initalize a new context with a fresh set of standard objects. All operations on the context
|
|
|
|
# should be performed in the block that is passed.
|
|
|
|
def open(options = {}, &block)
|
2009-12-02 01:35:16 -05:00
|
|
|
new(options).open(&block)
|
2009-11-13 12:09:54 -05:00
|
|
|
end
|
2011-10-06 07:09:00 -04:00
|
|
|
|
2010-01-18 06:06:38 -05:00
|
|
|
def eval(javascript)
|
|
|
|
new.eval(javascript)
|
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
2009-12-02 01:35:16 -05:00
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
|
|
|
@@default_factory = nil
|
|
|
|
def self.default_factory
|
|
|
|
@@default_factory ||= ContextFactory.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_factory=(factory)
|
|
|
|
@@default_factory = factory
|
|
|
|
end
|
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
attr_reader :scope
|
|
|
|
|
2009-12-02 01:46:51 -05:00
|
|
|
# Create a new javascript environment for executing javascript and ruby code.
|
|
|
|
# * <tt>:sealed</tt> - if this is true, then the standard objects such as Object, Function, Array will not be able to be modified
|
|
|
|
# * <tt>:with</tt> - use this ruby object as the root scope for all javascript that is evaluated
|
|
|
|
# * <tt>:java</tt> - if true, java packages will be accessible from within javascript
|
2009-12-02 01:35:16 -05:00
|
|
|
def initialize(options = {}) #:nodoc:
|
2012-02-15 05:04:14 -05:00
|
|
|
factory = options[:factory] ||
|
|
|
|
(options[:restrictable] ? RestrictableContextFactory.instance : self.class.default_factory)
|
|
|
|
factory.call do |context|
|
2011-12-07 13:59:39 -05:00
|
|
|
@native = context
|
|
|
|
@global = @native.initStandardObjects(nil, options[:sealed] == true)
|
2009-12-02 01:35:16 -05:00
|
|
|
if with = options[:with]
|
2011-12-09 01:52:47 -05:00
|
|
|
@scope = Rhino.to_javascript(with)
|
2011-12-07 13:59:39 -05:00
|
|
|
@scope.setParentScope(@global)
|
2009-12-02 01:35:16 -05:00
|
|
|
else
|
|
|
|
@scope = @global
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
2009-12-02 01:35:16 -05:00
|
|
|
unless options[:java]
|
2010-05-24 13:40:43 -04:00
|
|
|
for package in ["Packages", "java", "javax", "org", "com", "edu", "net"]
|
2011-12-07 13:59:39 -05:00
|
|
|
@global.delete(package)
|
2009-12-02 01:35:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
|
|
|
# Returns the ContextFactory used while creating this context.
|
|
|
|
def factory
|
|
|
|
@native.getFactory
|
|
|
|
end
|
|
|
|
|
2009-11-15 21:56:31 -05:00
|
|
|
# Read a value from the global scope of this context
|
2011-12-07 13:59:39 -05:00
|
|
|
def [](key)
|
|
|
|
@scope[key]
|
2009-11-11 01:50:36 -05:00
|
|
|
end
|
2009-11-15 21:56:31 -05:00
|
|
|
|
2011-10-06 07:09:00 -04:00
|
|
|
# Set a value in the global scope of this context. This value will be visible to all the
|
|
|
|
# javascript that is executed in this context.
|
2011-12-07 13:59:39 -05:00
|
|
|
def []=(key, val)
|
|
|
|
@scope[key] = val
|
2009-11-11 01:50:36 -05:00
|
|
|
end
|
2009-11-15 21:56:31 -05:00
|
|
|
|
|
|
|
# Evaluate a string of javascript in this context:
|
2009-11-20 10:38:31 -05:00
|
|
|
# * <tt>source</tt> - the javascript source code to evaluate. This can be either a string or an IO object.
|
2009-11-15 21:56:31 -05:00
|
|
|
# * <tt>source_name</tt> - associated name for this source code. Mainly useful for backtraces.
|
|
|
|
# * <tt>line_number</tt> - associate this number with the first line of executing source. Mainly useful for backtraces
|
|
|
|
def eval(source, source_name = "<eval>", line_number = 1)
|
2011-12-09 05:28:27 -05:00
|
|
|
open do
|
|
|
|
if IO === source || StringIO === source
|
|
|
|
result = @native.evaluateReader(@scope, IOReader.new(source), source_name, line_number, nil)
|
|
|
|
else
|
|
|
|
result = @native.evaluateString(@scope, source.to_s, source_name, line_number, nil)
|
2009-11-20 10:38:31 -05:00
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
Rhino.to_ruby(result)
|
2010-01-18 06:06:38 -05:00
|
|
|
end
|
2009-12-02 01:35:16 -05:00
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
|
2009-12-15 03:33:55 -05:00
|
|
|
def evaluate(*args) # :nodoc:
|
2011-12-09 05:28:27 -05:00
|
|
|
eval(*args) # an alias
|
2009-12-15 03:33:55 -05:00
|
|
|
end
|
2009-12-02 01:35:16 -05:00
|
|
|
|
2009-11-20 10:58:15 -05:00
|
|
|
# Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the
|
|
|
|
# javascript. e.g.
|
|
|
|
#
|
|
|
|
# Context.open do |cxt|
|
|
|
|
# cxt.load("path/to/some/lib.js")
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def load(filename)
|
|
|
|
File.open(filename) do |file|
|
2009-12-15 03:33:55 -05:00
|
|
|
evaluate file, filename, 1
|
2009-11-20 10:58:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 05:04:14 -05:00
|
|
|
# Returns true if this context supports restrictions.
|
|
|
|
def restrictable?
|
|
|
|
@native.is_a?(RestrictableContextFactory::Context)
|
|
|
|
end
|
|
|
|
|
|
|
|
def instruction_limit
|
|
|
|
restrictable? ? @native.instruction_limit : false
|
|
|
|
end
|
|
|
|
|
2009-11-15 21:56:31 -05:00
|
|
|
# Set the maximum number of instructions that this context will execute.
|
2012-02-15 05:04:14 -05:00
|
|
|
# If this instruction limit is exceeded, then a #Rhino::RunawayScriptError
|
|
|
|
# will be raised.
|
2009-11-10 09:52:03 -05:00
|
|
|
def instruction_limit=(limit)
|
2012-02-15 05:04:14 -05:00
|
|
|
if restrictable?
|
|
|
|
@native.instruction_limit = limit
|
|
|
|
else
|
2012-02-15 05:46:36 -05:00
|
|
|
raise "setting an instruction_limit has no effect on this context, use " +
|
2012-04-12 14:12:30 -04:00
|
|
|
"Context.open(:restrictable => true) to gain a restrictable instance"
|
2012-02-15 05:04:14 -05:00
|
|
|
end
|
2009-11-10 09:52:03 -05:00
|
|
|
end
|
2012-02-15 05:08:48 -05:00
|
|
|
|
|
|
|
def timeout_limit
|
|
|
|
restrictable? ? @native.timeout_limit : false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the duration (in seconds e.g. 1.5) this context is allowed to execute.
|
|
|
|
# After the timeout passes (no matter if any JS has been evaluated) and this
|
|
|
|
# context is still attempted to run code, a #Rhino::ScriptTimeoutError will
|
|
|
|
# be raised.
|
|
|
|
def timeout_limit=(limit)
|
|
|
|
if restrictable?
|
|
|
|
@native.timeout_limit = limit
|
|
|
|
else
|
2012-02-15 05:46:36 -05:00
|
|
|
raise "setting an timeout_limit has no effect on this context, use " +
|
2012-04-12 14:12:30 -04:00
|
|
|
"Context.open(:restrictable => true) to gain a restrictable instance"
|
2012-02-15 05:08:48 -05:00
|
|
|
end
|
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
2012-01-10 11:24:12 -05:00
|
|
|
def optimization_level
|
|
|
|
@native.getOptimizationLevel
|
|
|
|
end
|
|
|
|
|
2010-08-12 08:04:04 -04:00
|
|
|
# Set the optimization level that this context will use. This is sometimes necessary
|
|
|
|
# in Rhino, if the bytecode size of the compiled javascript exceeds the 64KB limit.
|
|
|
|
# By using the -1 optimization level, you tell Rhino to run in interpretative mode,
|
|
|
|
# taking a hit to performance but escaping the Java bytecode limit.
|
|
|
|
def optimization_level=(level)
|
2012-02-15 05:04:14 -05:00
|
|
|
if JS::Context.isValidOptimizationLevel(level)
|
2012-01-10 11:24:12 -05:00
|
|
|
@native.setOptimizationLevel(level)
|
|
|
|
level
|
|
|
|
else
|
|
|
|
@native.setOptimizationLevel(0)
|
|
|
|
nil
|
|
|
|
end
|
2010-08-12 08:04:04 -04:00
|
|
|
end
|
|
|
|
|
2011-12-07 06:50:43 -05:00
|
|
|
# Get the JS interpreter version.
|
|
|
|
# Returns a number e.g. 1.7, nil if unknown and 0 for default.
|
|
|
|
def version
|
|
|
|
case const_value = @native.getLanguageVersion
|
|
|
|
when -1 then nil # VERSION_UNKNOWN
|
|
|
|
when 0 then 0 # VERSION_DEFAULT
|
|
|
|
else const_value / 100.0 # VERSION_1_1 (1.1 = 110 / 100)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets interpreter mode a.k.a. JS language version e.g. 1.7 (if supported).
|
|
|
|
def version=(version)
|
|
|
|
const = version.to_s.gsub('.', '_').upcase
|
|
|
|
const = "VERSION_#{const}" if const[0, 7] != 'VERSION'
|
2012-02-15 06:36:16 -05:00
|
|
|
if JS::Context.constants.find { |c| c.to_s == const }
|
2012-02-15 05:04:14 -05:00
|
|
|
const_value = JS::Context.const_get(const)
|
2011-12-07 06:50:43 -05:00
|
|
|
@native.setLanguageVersion(const_value)
|
|
|
|
const_value
|
|
|
|
else
|
2012-02-15 05:04:14 -05:00
|
|
|
@native.setLanguageVersion(JS::Context::VERSION_DEFAULT)
|
2011-12-07 06:50:43 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-09 05:28:27 -05:00
|
|
|
# Enter this context for operations.
|
|
|
|
# Some methods such as eval() will fail unless this context is open !
|
|
|
|
def open(&block)
|
|
|
|
do_open(&block)
|
|
|
|
rescue JS::RhinoException => e
|
|
|
|
raise Rhino::JSError.new(e)
|
2009-12-02 01:35:16 -05:00
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def do_open
|
2012-02-15 05:04:14 -05:00
|
|
|
factory.enterContext(@native)
|
2011-12-09 05:28:27 -05:00
|
|
|
begin
|
|
|
|
yield self
|
|
|
|
ensure
|
2012-02-15 05:04:14 -05:00
|
|
|
factory.exit
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
2009-11-20 10:38:31 -05:00
|
|
|
|
2011-12-09 05:28:27 -05:00
|
|
|
class IOReader < java.io.Reader # :nodoc:
|
2009-11-20 10:38:31 -05:00
|
|
|
|
|
|
|
def initialize(io)
|
|
|
|
@io = io
|
|
|
|
end
|
|
|
|
|
2011-12-09 05:28:27 -05:00
|
|
|
# implement int Reader#read(char[] buffer, int offset, int length)
|
|
|
|
def read(buffer, offset, length)
|
|
|
|
str = nil
|
2009-11-20 10:38:31 -05:00
|
|
|
begin
|
|
|
|
str = @io.read(length)
|
2009-12-02 01:35:16 -05:00
|
|
|
rescue StandardError => e
|
2012-01-10 11:24:12 -05:00
|
|
|
raise java.io.IOException.new("failed reading from ruby IO object")
|
2009-11-20 10:38:31 -05:00
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
if str.nil?
|
|
|
|
return -1
|
|
|
|
else
|
|
|
|
jstr = str.to_java
|
|
|
|
for i in 0 .. jstr.length - 1
|
|
|
|
buffer[i + offset] = jstr.charAt(i)
|
|
|
|
end
|
|
|
|
return jstr.length
|
|
|
|
end
|
2009-11-20 10:38:31 -05:00
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2009-11-20 10:38:31 -05:00
|
|
|
end
|
2009-12-02 01:35:16 -05:00
|
|
|
|
2012-02-15 05:04:14 -05:00
|
|
|
ContextFactory = JS::ContextFactory # :nodoc: backward compatibility
|
2009-12-02 01:35:16 -05:00
|
|
|
|
2012-02-15 05:04:14 -05:00
|
|
|
class RestrictableContextFactory < ContextFactory # :nodoc:
|
|
|
|
|
|
|
|
@@instance = nil
|
|
|
|
def self.instance
|
|
|
|
@@instance ||= new
|
2009-11-10 09:52:03 -05:00
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
|
|
|
# protected Context makeContext()
|
|
|
|
def makeContext
|
|
|
|
Context.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# protected void observeInstructionCount(Context context, int instructionCount)
|
|
|
|
def observeInstructionCount(context, count)
|
2012-02-15 05:08:48 -05:00
|
|
|
context.check!(count) if context.is_a?(Context)
|
2012-02-15 05:04:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# protected Object doTopCall(Callable callable, Context context,
|
|
|
|
# Scriptable scope, Scriptable thisObj, Object[] args)
|
|
|
|
def doTopCall(callable, context, scope, this, args)
|
2012-02-15 05:08:48 -05:00
|
|
|
context.reset! if context.is_a?(Context)
|
2012-02-15 05:04:14 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
class Context < JS::Context # :nodoc:
|
|
|
|
|
|
|
|
def initialize(factory)
|
|
|
|
super(factory)
|
|
|
|
reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :instruction_limit
|
|
|
|
|
|
|
|
def instruction_limit=(limit)
|
|
|
|
treshold = getInstructionObserverThreshold
|
|
|
|
if limit && (treshold == 0 || treshold > limit)
|
|
|
|
setInstructionObserverThreshold(limit)
|
|
|
|
end
|
|
|
|
@instruction_limit = limit
|
|
|
|
end
|
|
|
|
|
2012-02-15 05:08:48 -05:00
|
|
|
attr_reader :instruction_count
|
|
|
|
|
|
|
|
TIMEOUT_INSTRUCTION_TRESHOLD = 42
|
|
|
|
|
|
|
|
attr_reader :timeout_limit
|
|
|
|
|
|
|
|
def timeout_limit=(limit) # in seconds
|
|
|
|
treshold = getInstructionObserverThreshold
|
|
|
|
if limit && (treshold == 0 || treshold > TIMEOUT_INSTRUCTION_TRESHOLD)
|
|
|
|
setInstructionObserverThreshold(TIMEOUT_INSTRUCTION_TRESHOLD)
|
|
|
|
end
|
|
|
|
@timeout_limit = limit
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :start_time
|
|
|
|
|
|
|
|
def check!(count = nil)
|
|
|
|
@instruction_count += count if count
|
|
|
|
check_instruction_limit!
|
|
|
|
check_timeout_limit!(count)
|
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
2012-02-15 05:08:48 -05:00
|
|
|
def check_instruction_limit!
|
2012-02-15 05:04:14 -05:00
|
|
|
if instruction_limit && instruction_count > instruction_limit
|
|
|
|
raise RunawayScriptError, "script exceeded allowable instruction count: #{instruction_limit}"
|
|
|
|
end
|
|
|
|
end
|
2012-02-15 05:08:48 -05:00
|
|
|
|
|
|
|
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
|
2012-02-15 05:04:14 -05:00
|
|
|
end
|
2012-02-15 05:08:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset!
|
|
|
|
@instruction_count = 0
|
|
|
|
@start_time = Time.now
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-11-10 09:52:03 -05:00
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2009-11-10 09:52:03 -05:00
|
|
|
end
|
2012-02-15 05:04:14 -05:00
|
|
|
|
2009-12-02 01:35:16 -05:00
|
|
|
class ContextError < StandardError # :nodoc:
|
|
|
|
end
|
|
|
|
|
2011-12-09 05:28:27 -05:00
|
|
|
class RunawayScriptError < ContextError # :nodoc:
|
2009-12-02 01:35:16 -05:00
|
|
|
end
|
2012-02-15 05:08:48 -05:00
|
|
|
|
|
|
|
class ScriptTimeoutError < ContextError # :nodoc:
|
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2010-08-10 15:58:11 -04:00
|
|
|
end
|