1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00
therubyrhino/lib/rhino/context.rb

85 lines
1.8 KiB
Ruby
Raw Normal View History

2009-09-24 20:06:31 -04:00
module Rhino
class Context
attr_reader :scope
2009-09-24 20:06:31 -04:00
class << self
def open(options = {})
2009-11-10 09:52:03 -05:00
ContextFactory.new.call do |native|
yield new(native, options)
end
end
2009-09-24 20:06:31 -04:00
private :new
end
def initialize(native, options) #:nodoc:
2009-09-24 20:06:31 -04:00
@native = native
@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
unless options[:java]
for package in ["Packages", "java", "org", "com"]
@global.j.delete(package)
end
end
2009-09-24 20:06:31 -04:00
end
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
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-09-24 20:06:31 -04:00
end
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
def message
@native.cause.details
2009-09-24 20:06:31 -04:00
end
def javascript_backtrace
@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