module Rhino::To
Public Class Methods
javascript(object, scope = nil)
click to toggle source
deprecated use {Rhino#to_javascript} instead
# File lib/rhino/deprecations.rb, line 35 def self.javascript(object, scope = nil) warn "[DEPRECATION] `Rhino::To.javascript` is deprecated, use `Rhino.to_javascript` instead." to_javascript(object, scope) end
ruby(object)
click to toggle source
deprecated use {Rhino#to_ruby} instead
# File lib/rhino/deprecations.rb, line 29 def self.ruby(object) warn "[DEPRECATION] `Rhino::To.ruby` is deprecated, use `Rhino.to_ruby` instead." to_ruby(object) end
Public Instance Methods
args_to_javascript(args, scope = nil)
click to toggle source
# File lib/rhino/wormhole.rb, line 37 def args_to_javascript(args, scope = nil) args.map { |arg| to_javascript(arg, scope) }.to_java end
args_to_ruby(args)
click to toggle source
# File lib/rhino/wormhole.rb, line 33 def args_to_ruby(args) args.map { |arg| to_ruby(arg) } end
to_javascript(object, scope = nil)
click to toggle source
# File lib/rhino/wormhole.rb, line 17 def to_javascript(object, scope = nil) case object when NilClass then object when String, Numeric then object when TrueClass, FalseClass then object when JS::Scriptable then object when Array then array_to_javascript(object, scope) when Hash then hash_to_javascript(object, scope) when Time then time_to_javascript(object, scope) when Method, UnboundMethod then Ruby::Function.wrap(object, scope) when Proc then Ruby::Function.wrap(object, scope) when Class then Ruby::Constructor.wrap(object, scope) else RubyObject.wrap(object, scope) end end
to_ruby(object)
click to toggle source
# File lib/rhino/wormhole.rb, line 5 def to_ruby(object) case object when JS::Scriptable::NOT_FOUND, JS::Undefined then nil when JS::Wrapper then object.unwrap when JS::NativeArray then array_to_ruby(object) when JS::NativeDate then Time.at(object.getJSTimeValue / 1000) # Rhino 1.7R4 added ConsString for optimized String + operations : when Java::JavaLang::CharSequence then object.toString else object end end
Private Instance Methods
array_to_javascript(rb_array, scope = nil)
click to toggle source
# File lib/rhino/wormhole.rb, line 47 def array_to_javascript(rb_array, scope = nil) # First convert all array elements to their javascript equivalents and # then invoke to_java below in order to create a Java array. This allows # arrays with nested hashes to be converted properly. converted_rb_array = rb_array.map do |rb_element| to_javascript(rb_element, scope) end if scope && context = JS::Context.getCurrentContext context.newArray(scope, converted_rb_array.to_java) else JS::NativeArray.new(converted_rb_array.to_java) end end
array_to_ruby(js_array)
click to toggle source
# File lib/rhino/wormhole.rb, line 43 def array_to_ruby(js_array) js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) } end
hash_to_javascript(rb_hash, scope = nil)
click to toggle source
# File lib/rhino/wormhole.rb, line 62 def hash_to_javascript(rb_hash, scope = nil) js_object = if scope && context = JS::Context.getCurrentContext context.newObject(scope) else JS::NativeObject.new end # JS::NativeObject implements Map put it's #put does : # throw new UnsupportedOperationException(); thus no []= rb_hash.each_pair do |key, val| js_val = to_javascript(val, scope) JS::ScriptableObject.putProperty(js_object, key.to_s, js_val) end js_object end
time_to_javascript(time, scope = nil)
click to toggle source
# File lib/rhino/wormhole.rb, line 78 def time_to_javascript(time, scope = nil) millis = time.to_f * 1000 if scope && context = JS::Context.getCurrentContext JS::ScriptRuntime.newObject(context, scope, 'Date', [ millis ].to_java) else # the pure reflection way - god I love Java's private : js_klass = JS::NativeObject.java_class.to_java new = js_klass.getDeclaredConstructor; new.setAccessible(true) js_date = new.newInstance date = js_klass.getDeclaredField(:date); date.setAccessible(true) date.setDouble(js_date, millis) js_date end end