2009-11-09 10:45:23 -05:00
|
|
|
|
|
|
|
module Rhino
|
|
|
|
class RubyObject < J::ScriptableObject
|
|
|
|
include J::Wrapper
|
|
|
|
|
|
|
|
def initialize(object)
|
|
|
|
super()
|
|
|
|
@ruby = object
|
|
|
|
end
|
|
|
|
|
|
|
|
def unwrap
|
|
|
|
@ruby
|
|
|
|
end
|
|
|
|
|
|
|
|
def getClassName()
|
|
|
|
@ruby.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def getPrototype()
|
2009-11-11 21:12:20 -05:00
|
|
|
Prototype::Generic
|
2009-11-09 10:45:23 -05:00
|
|
|
end
|
|
|
|
|
2009-11-13 12:09:54 -05:00
|
|
|
def getIds()
|
2010-01-18 08:35:34 -05:00
|
|
|
puts "getIds()"
|
|
|
|
require 'java'
|
|
|
|
@ruby.public_methods(false).map {|m| m.gsub(/(.)_(.)/) {::JavaLang::String.new("#{$1}#{$2.upcase}")}}.tap {|ids| puts "ids: #{ids.inspect}"}.to_java
|
2009-11-13 12:09:54 -05:00
|
|
|
end
|
|
|
|
|
2009-11-09 10:45:23 -05:00
|
|
|
def to_s
|
|
|
|
"[Native #{@ruby.class.name}]"
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :prototype, :getPrototype
|
2009-11-11 21:12:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Prototype < J::ScriptableObject
|
|
|
|
|
|
|
|
def get(name, start)
|
|
|
|
robject = To.ruby(start)
|
2009-11-13 12:09:54 -05:00
|
|
|
if name == "toString"
|
|
|
|
return RubyFunction.new(lambda { "[Ruby #{robject.class.name}]"})
|
|
|
|
end
|
2009-11-11 21:12:20 -05:00
|
|
|
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
|
|
|
|
if (robject.public_methods(false).include?(rb_name))
|
|
|
|
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
|
|
|
|
|
|
|
|
def has(name, start)
|
|
|
|
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
|
|
|
|
To.ruby(start).public_methods(false).respond_to?(rb_name) ? true : super(name,start)
|
|
|
|
end
|
2010-01-18 08:35:34 -05: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
|
|
|
|
end
|