2009-09-24 20:06:31 -04:00
|
|
|
module Rhino
|
2009-11-11 21:12:20 -05:00
|
|
|
|
|
|
|
class Context
|
2009-11-11 01:50:36 -05:00
|
|
|
attr_reader :scope
|
2009-09-24 20:06:31 -04:00
|
|
|
|
|
|
|
class << self
|
2009-11-11 01:50:36 -05:00
|
|
|
def open(options = {})
|
2009-11-10 09:52:03 -05:00
|
|
|
ContextFactory.new.call do |native|
|
2009-11-11 01:50:36 -05:00
|
|
|
yield new(native, options)
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-11 01:50:36 -05:00
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
private :new
|
|
|
|
end
|
|
|
|
|
2009-11-11 01:50:36 -05:00
|
|
|
def initialize(native, options) #:nodoc:
|
2009-09-24 20:06:31 -04:00
|
|
|
@native = native
|
2009-11-13 12:09:54 -05:00
|
|
|
@global = NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true))
|
|
|
|
if with = options[:with]
|
|
|
|
@scope = To.javascript(with)
|
|
|
|
@scope.setParentScope(@global.j)
|
|
|
|
else
|
|
|
|
@scope = @global
|
|
|
|
end
|
2009-11-11 01:50:36 -05:00
|
|
|
unless options[:java]
|
|
|
|
for package in ["Packages", "java", "org", "com"]
|
2009-11-13 12:09:54 -05:00
|
|
|
@global.j.delete(package)
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
2009-11-11 01:50:36 -05:00
|
|
|
end
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
|
|
|
|
2009-11-11 01:50:36 -05:00
|
|
|
def [](k)
|
|
|
|
@scope[k]
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(k,v)
|
|
|
|
@scope[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval(str)
|
2009-11-08 23:25:25 -05:00
|
|
|
str = str.to_s
|
2009-09-24 20:06:31 -04:00
|
|
|
begin
|
2009-11-13 12:09:54 -05:00
|
|
|
scope = To.javascript(@scope)
|
|
|
|
result = @native.evaluateString(scope, str, "<eval>", 1, nil)
|
|
|
|
To.ruby result
|
2009-09-24 20:06:31 -04:00
|
|
|
rescue J::RhinoException => e
|
|
|
|
raise Rhino::RhinoError, e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-10 09:52:03 -05:00
|
|
|
def instruction_limit=(limit)
|
|
|
|
@native.setInstructionObserverThreshold(limit);
|
|
|
|
@native.factory.instruction_limit = limit
|
|
|
|
end
|
2009-11-11 01:50:36 -05:00
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
2009-11-11 21:12:20 -05:00
|
|
|
|
2009-11-10 09:52:03 -05:00
|
|
|
class ContextFactory < J::ContextFactory
|
|
|
|
|
|
|
|
def observeInstructionCount(cxt, count)
|
|
|
|
raise RunawayScriptError, "script exceeded allowable instruction count" if count > @limit
|
|
|
|
end
|
|
|
|
|
|
|
|
def instruction_limit=(count)
|
|
|
|
@limit = count
|
|
|
|
end
|
|
|
|
end
|
2009-09-24 20:06:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RhinoError < StandardError
|
|
|
|
def initialize(native)
|
|
|
|
@native = native
|
|
|
|
end
|
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def message
|
|
|
|
@native.cause.details
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_backtrace
|
2009-10-06 09:52:45 -04:00
|
|
|
@native.getScriptStackTrace()
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-10 09:52:03 -05:00
|
|
|
|
|
|
|
class RunawayScriptError < StandardError; end
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|