2009-11-09 10:45:23 -05:00
|
|
|
|
|
|
|
module Rhino
|
2011-12-07 06:41:18 -05:00
|
|
|
class RubyObject < JS::ScriptableObject
|
|
|
|
include JS::Wrapper
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def initialize(object)
|
|
|
|
super()
|
|
|
|
@ruby = object
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# abstract Object Wrapper#unwrap();
|
2009-11-09 10:45:23 -05:00
|
|
|
def unwrap
|
|
|
|
@ruby
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# abstract String Scriptable#getClassName();
|
|
|
|
def getClassName
|
2009-11-09 10:45:23 -05:00
|
|
|
@ruby.class.name
|
|
|
|
end
|
2011-12-08 11:28:23 -05:00
|
|
|
|
|
|
|
def toString
|
|
|
|
"[ruby #{getClassName}]" # [object User]
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# override Object Scriptable#get(String name, Scriptable start);
|
|
|
|
# override Object Scriptable#get(int index, Scriptable start);
|
|
|
|
def get(name, start)
|
|
|
|
if name.is_a?(String)
|
|
|
|
# NOTE: preferrably when using a ruby object in JS methods should
|
|
|
|
# be used but instance variables will work as well but if there's
|
|
|
|
# a attr reader it is given a preference e.g. :
|
|
|
|
#
|
|
|
|
# class Foo
|
|
|
|
# attr_reader :bar2
|
|
|
|
# def initialize
|
|
|
|
# @bar1 = 'bar1'
|
|
|
|
# @bar2 = 'bar2'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# fooObj.bar1; // 'bar1'
|
|
|
|
# fooObj.bar2; // function
|
|
|
|
# fooObj.bar2(); // 'bar2'
|
|
|
|
#
|
|
|
|
if @ruby.respond_to?(name)
|
|
|
|
return RubyFunction.new(@ruby.method(name))
|
|
|
|
elsif @ruby.instance_variables.include?(var_name = "@#{name}")
|
|
|
|
var_value = @ruby.instance_variable_get(var_name)
|
2011-12-09 01:52:47 -05:00
|
|
|
return Rhino.to_javascript(var_value, self)
|
2011-12-08 11:28:23 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
end
|
2011-12-08 11:28:23 -05:00
|
|
|
super
|
2011-10-06 07:09:20 -04:00
|
|
|
end
|
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# override boolean Scriptable#has(String name, Scriptable start);
|
|
|
|
# override boolean Scriptable#has(int index, Scriptable start);
|
|
|
|
def has(name, start)
|
|
|
|
if name.is_a?(String)
|
|
|
|
if @ruby.respond_to?(name) ||
|
|
|
|
@ruby.instance_variables.include?("@#{name}")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
super
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# override void Scriptable#put(String name, Scriptable start, Object value);
|
|
|
|
# override void Scriptable#put(int index, Scriptable start, Object value);
|
|
|
|
def put(name, start, value)
|
|
|
|
if name.is_a?(String)
|
|
|
|
if @ruby.respond_to?(set_name = "#{name}=")
|
2011-12-09 01:52:47 -05:00
|
|
|
return @ruby.send(set_name, Rhino.to_ruby(value))
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-08 11:28:23 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# override boolean Scriptable#hasInstance(Scriptable instance);
|
|
|
|
def hasInstance(instance)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# override Object[] Scriptable#getIds();
|
|
|
|
def getIds
|
|
|
|
ids = @ruby.instance_variables.map { |ivar| ivar[1..-1].to_java }
|
|
|
|
@ruby.public_methods(false).each do |name|
|
|
|
|
name = name[0...-1] if name[-1, 1] == '=' # 'foo=' ... 'foo'
|
|
|
|
name = name.to_java
|
|
|
|
ids << name unless ids.include?(name)
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-12-08 11:28:23 -05:00
|
|
|
super.each { |id| ids.unshift(id) }
|
|
|
|
ids.to_java
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-12-08 11:28:23 -05:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
2011-06-08 16:10:51 -04:00
|
|
|
end
|