2009-10-06 14:20:37 -04:00
|
|
|
|
|
|
|
module Rhino
|
2011-10-05 17:49:51 -04:00
|
|
|
module To
|
2009-10-09 19:36:42 -04:00
|
|
|
JS_UNDEF = [J::Scriptable::NOT_FOUND, J::Undefined]
|
2011-10-05 17:49:51 -04:00
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
def ruby(object)
|
|
|
|
case object
|
2010-08-11 10:56:41 -04:00
|
|
|
when *JS_UNDEF then nil
|
|
|
|
when J::Wrapper then object.unwrap
|
|
|
|
when J::NativeArray then array(object)
|
2011-10-05 17:49:51 -04:00
|
|
|
when J::NativeDate then Time.at(object.getJSTimeValue() / 1000)
|
2010-08-11 10:56:41 -04:00
|
|
|
when J::Regexp::NativeRegExp then object
|
|
|
|
when J::Function then NativeFunction.new(object)
|
|
|
|
when J::Scriptable then NativeObject.new(object)
|
2009-10-06 14:20:37 -04:00
|
|
|
else object
|
2011-10-05 17:49:51 -04:00
|
|
|
end
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
2011-10-05 17:49:51 -04:00
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
def javascript(object)
|
|
|
|
case object
|
2009-11-11 21:12:20 -05:00
|
|
|
when String,Numeric then object
|
|
|
|
when TrueClass,FalseClass then object
|
|
|
|
when Array then J::NativeArray.new(object.to_java)
|
2011-06-28 09:35:10 -04:00
|
|
|
when Hash then ruby_hash_to_native(object)
|
2009-11-11 21:12:20 -05:00
|
|
|
when Proc,Method then RubyFunction.new(object)
|
2011-10-05 17:49:51 -04:00
|
|
|
when NativeObject then object.j
|
2009-11-11 21:12:20 -05:00
|
|
|
when J::Scriptable then object
|
2009-11-09 10:45:23 -05:00
|
|
|
else RubyObject.new(object)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|
2011-10-05 17:49:51 -04:00
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
def array(native)
|
2010-05-26 19:41:53 -04:00
|
|
|
native.length.times.map {|i| ruby(native.get(i,native))}
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-06-28 09:35:10 -04:00
|
|
|
|
|
|
|
def ruby_hash_to_native(ruby_object)
|
|
|
|
native_object = NativeObject.new
|
|
|
|
|
|
|
|
ruby_object.each_pair do |k, v|
|
|
|
|
native_object[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
native_object.j
|
|
|
|
end
|
2011-10-05 17:49:51 -04:00
|
|
|
|
2011-06-28 09:35:10 -04:00
|
|
|
module_function :ruby, :javascript, :array, :ruby_hash_to_native
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|