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
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def unwrap
|
|
|
|
@ruby
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def getClassName()
|
|
|
|
@ruby.class.name
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def getPrototype()
|
2009-11-11 21:12:20 -05:00
|
|
|
Prototype::Generic
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
|
|
|
def put(key, start, value)
|
|
|
|
if @ruby.respond_to?("#{key}=")
|
|
|
|
@ruby.send("#{key}=", To.ruby(value))
|
|
|
|
value
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-13 12:09:54 -05:00
|
|
|
def getIds()
|
2011-06-09 17:03:33 -04:00
|
|
|
@ruby.public_methods(false).map {|m| m.gsub(/(.)_(.)/) {java.lang.String.new("#{$1}#{$2.upcase}")}}.to_java
|
2009-11-13 12:09:54 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def to_s
|
|
|
|
"[Native #{@ruby.class.name}]"
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
alias_method :prototype, :getPrototype
|
2011-10-06 07:09:20 -04:00
|
|
|
|
|
|
|
|
2011-12-07 06:41:18 -05:00
|
|
|
class Prototype < JS::ScriptableObject
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
def get(name, start)
|
|
|
|
robject = To.ruby(start)
|
2011-10-06 07:09:20 -04:00
|
|
|
if name == "toString"
|
2009-11-13 12:09:54 -05:00
|
|
|
return RubyFunction.new(lambda { "[Ruby #{robject.class.name}]"})
|
|
|
|
end
|
2011-09-27 10:57:40 -04:00
|
|
|
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}.to_sym
|
2011-10-06 07:13:09 -04:00
|
|
|
if (robject.public_methods(false).collect(&:to_sym).include?(rb_name))
|
2009-11-11 21:12:20 -05:00
|
|
|
method = robject.method(rb_name)
|
2010-01-18 08:35:34 -05:00
|
|
|
if method.arity == 0
|
|
|
|
To.javascript(method.call)
|
|
|
|
else
|
|
|
|
RubyFunction.new(method)
|
|
|
|
end
|
2009-11-11 21:12:20 -05:00
|
|
|
else
|
|
|
|
super(name, start)
|
|
|
|
end
|
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
def has(name, start)
|
2011-09-27 10:57:40 -04:00
|
|
|
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}.to_sym
|
2011-09-29 16:59:48 -04:00
|
|
|
To.ruby(start).public_methods(false).collect(&:to_sym).include?(rb_name) ? true : super(name,start)
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-10-06 07:09:20 -04:00
|
|
|
|
2009-11-13 12:09:54 -05:00
|
|
|
Generic = new
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
2011-06-08 16:10:51 -04:00
|
|
|
end
|