lib/execjs/ruby_rhino_runtime.rb in execjs-1.3.2 vs lib/execjs/ruby_rhino_runtime.rb in execjs-1.4.0
- old
+ new
@@ -1,56 +1,58 @@
+require "execjs/runtime"
+
module ExecJS
- class RubyRhinoRuntime
- class Context
- def initialize(source = "")
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ class RubyRhinoRuntime < Runtime
+ class Context < Runtime::Context
+ def initialize(runtime, source = "")
+ source = encode(source)
@rhino_context = ::Rhino::Context.new
fix_memory_limit! @rhino_context
@rhino_context.eval(source)
end
def exec(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()", options
end
end
def eval(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
unbox @rhino_context.eval("(#{source})")
end
- rescue ::Rhino::JavascriptError => e
- if e.message == "syntax error"
+ rescue ::Rhino::JSError => e
+ if e.message =~ /^syntax error/
raise RuntimeError, e.message
else
raise ProgramError, e.message
end
end
def call(properties, *args)
unbox @rhino_context.eval(properties).call(*args)
- rescue ::Rhino::JavascriptError => e
+ rescue ::Rhino::JSError => e
if e.message == "syntax error"
raise RuntimeError, e.message
else
raise ProgramError, e.message
end
end
def unbox(value)
- case value = ::Rhino::To.ruby(value)
- when ::Rhino::NativeFunction
+ case value = ::Rhino::to_ruby(value)
+ when Java::OrgMozillaJavascript::NativeFunction
nil
- when ::Rhino::NativeObject
+ when Java::OrgMozillaJavascript::NativeObject
value.inject({}) do |vs, (k, v)|
case v
- when ::Rhino::NativeFunction, ::Rhino::J::Function
+ when Java::OrgMozillaJavascript::NativeFunction, ::Rhino::JS::Function
nil
else
vs[k] = unbox(v)
end
vs
@@ -73,23 +75,9 @@
end
end
def name
"therubyrhino (Rhino)"
- end
-
- def exec(source)
- context = Context.new
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new
- context.eval(source)
- end
-
- def compile(source)
- Context.new(source)
end
def available?
require "rhino"
true