2009-09-24 20:06:31 -04:00
|
|
|
module Rhino
|
|
|
|
|
|
|
|
def function(&impl)
|
|
|
|
Function.new &impl
|
|
|
|
end
|
|
|
|
|
|
|
|
class Context
|
|
|
|
|
|
|
|
class << self
|
2009-10-06 09:52:45 -04:00
|
|
|
def open
|
|
|
|
J::ContextFactory.new.call do |native|
|
|
|
|
yield new(native)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
def open_std(options = {})
|
|
|
|
open do |cxt|
|
|
|
|
yield cxt, cxt.init_standard_objects(options)
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2009-10-06 14:20:37 -04:00
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
private :new
|
|
|
|
end
|
|
|
|
|
2009-09-25 09:46:01 -04:00
|
|
|
def initialize(native) #:nodoc:
|
2009-09-24 20:06:31 -04:00
|
|
|
@native = native
|
|
|
|
end
|
2009-10-06 09:52:45 -04:00
|
|
|
|
|
|
|
def init_standard_objects(options = {})
|
|
|
|
NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true)).tap do |objects|
|
|
|
|
unless options[:java]
|
|
|
|
for package in ["Packages", "java", "org", "com"]
|
|
|
|
objects.j.delete(package)
|
|
|
|
end
|
|
|
|
end
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-08 23:25:25 -05:00
|
|
|
def eval(str, scope = @native.initStandardObjects())
|
|
|
|
str = str.to_s
|
2009-09-24 20:06:31 -04:00
|
|
|
begin
|
2009-10-06 14:20:37 -04:00
|
|
|
To.ruby @native.evaluateString(To.javascript(scope), str, "<eval>", 1, nil)
|
2009-09-24 20:06:31 -04:00
|
|
|
rescue J::RhinoException => e
|
|
|
|
raise Rhino::RhinoError, e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def standard
|
|
|
|
yield @native.initStandardObjects()
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-10-06 09:52:45 -04:00
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
class Function < J::BaseFunction
|
|
|
|
def initialize(&block)
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(cxt, scope, this, args)
|
|
|
|
@block.call(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class RhinoError < StandardError
|
|
|
|
def initialize(native)
|
|
|
|
@native = native
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
@native.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_backtrace
|
2009-10-06 09:52:45 -04:00
|
|
|
@native.getScriptStackTrace()
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|