Module: Rhino::To

Extended by:
To
Included in:
To
Defined in:
lib/rhino/wormhole.rb,
lib/rhino/deprecations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.javascript(object, scope = nil) ⇒ Object

Deprecated.

use #to_javascript instead


46
47
48
49
# File 'lib/rhino/deprecations.rb', line 46

def self.javascript(object, scope = nil) 
  Rhino.warn "[DEPRECATION] `Rhino::To.javascript` is deprecated, use `Rhino.to_javascript` instead."
  to_javascript(object, scope)
end

.ruby(object) ⇒ Object

Deprecated.

use #to_ruby instead


40
41
42
43
# File 'lib/rhino/deprecations.rb', line 40

def self.ruby(object)
  Rhino.warn "[DEPRECATION] `Rhino::To.ruby` is deprecated, use `Rhino.to_ruby` instead."
  to_ruby(object)
end

Instance Method Details

#args_to_javascript(args, scope = nil) ⇒ Object


37
38
39
# 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) ⇒ Object


33
34
35
# File 'lib/rhino/wormhole.rb', line 33

def args_to_ruby(args)
  args.map { |arg| to_ruby(arg) }
end

#to_javascript(object, scope = nil) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
# 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