mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
minor fix for time_to_js when scope not set - java_class needs a to_java
This commit is contained in:
parent
d128fee525
commit
cac2b9512a
1 changed files with 11 additions and 13 deletions
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
module Rhino
|
module Rhino
|
||||||
module To
|
module To
|
||||||
|
|
||||||
def to_ruby(object)
|
def to_ruby(object)
|
||||||
case object
|
case object
|
||||||
when JS::Scriptable::NOT_FOUND, JS::Undefined then nil
|
when JS::Scriptable::NOT_FOUND, JS::Undefined then nil
|
||||||
|
@ -33,13 +33,13 @@ module Rhino
|
||||||
def args_to_ruby(args)
|
def args_to_ruby(args)
|
||||||
args.map { |arg| to_ruby(arg) }
|
args.map { |arg| to_ruby(arg) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def args_to_javascript(args, scope = nil)
|
def args_to_javascript(args, scope = nil)
|
||||||
args.map { |arg| to_javascript(arg, scope) }.to_java
|
args.map { |arg| to_javascript(arg, scope) }.to_java
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def array_to_ruby(js_array)
|
def array_to_ruby(js_array)
|
||||||
js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) }
|
js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) }
|
||||||
end
|
end
|
||||||
|
@ -60,7 +60,7 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash_to_javascript(rb_hash, scope = nil)
|
def hash_to_javascript(rb_hash, scope = nil)
|
||||||
js_object =
|
js_object =
|
||||||
if scope && context = JS::Context.getCurrentContext
|
if scope && context = JS::Context.getCurrentContext
|
||||||
context.newObject(scope)
|
context.newObject(scope)
|
||||||
else
|
else
|
||||||
|
@ -68,29 +68,27 @@ module Rhino
|
||||||
end
|
end
|
||||||
# JS::NativeObject implements Map put it's #put does :
|
# JS::NativeObject implements Map put it's #put does :
|
||||||
# throw new UnsupportedOperationException(); thus no []=
|
# throw new UnsupportedOperationException(); thus no []=
|
||||||
rb_hash.each_pair do |key, val|
|
rb_hash.each_pair do |key, val|
|
||||||
js_val = to_javascript(val, scope)
|
js_val = to_javascript(val, scope)
|
||||||
JS::ScriptableObject.putProperty(js_object, key.to_s, js_val)
|
JS::ScriptableObject.putProperty(js_object, key.to_s, js_val)
|
||||||
end
|
end
|
||||||
js_object
|
js_object
|
||||||
end
|
end
|
||||||
|
|
||||||
def time_to_javascript(time, scope = nil)
|
def time_to_javascript(time, scope = nil)
|
||||||
millis = time.to_f * 1000
|
millis = time.to_f * 1000
|
||||||
if scope && context = JS::Context.getCurrentContext
|
if scope && context = JS::Context.getCurrentContext
|
||||||
JS::ScriptRuntime.newObject(context, scope, 'Date', [ millis ].to_java)
|
JS::ScriptRuntime.newObject(context, scope, 'Date', [ millis ].to_java)
|
||||||
else
|
else
|
||||||
# the pure reflection way - god I love Java's private :
|
# the pure reflection way - god I love Java's private :
|
||||||
js_klass = JS::NativeObject.java_class
|
js_klass = JS::NativeObject.java_class.to_java
|
||||||
new = js_klass.getDeclaredConstructor
|
new = js_klass.getDeclaredConstructor; new.setAccessible(true)
|
||||||
new.setAccessible(true)
|
|
||||||
js_date = new.newInstance
|
js_date = new.newInstance
|
||||||
date = js_klass.getDeclaredField(:date)
|
date = js_klass.getDeclaredField(:date); date.setAccessible(true)
|
||||||
date.setAccessible(true)
|
|
||||||
date.setDouble(js_date, millis)
|
date.setDouble(js_date, millis)
|
||||||
js_date
|
js_date
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue