2011-12-12 16:17:42 -05:00
|
|
|
|
|
|
|
module Rhino
|
2011-12-13 06:35:57 -05:00
|
|
|
module Ruby
|
2011-12-12 16:17:42 -05:00
|
|
|
|
2011-12-13 07:43:07 -05:00
|
|
|
# shared JS::Scriptable implementation
|
|
|
|
module Scriptable
|
|
|
|
|
2011-12-13 06:35:57 -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)
|
2011-12-13 07:43:07 -05:00
|
|
|
if unwrap.respond_to?(name)
|
|
|
|
method = unwrap.method(name)
|
2011-12-13 06:35:57 -05:00
|
|
|
if method.arity == 0 && # check if it is an attr_reader
|
2011-12-13 07:43:07 -05:00
|
|
|
( unwrap.respond_to?("#{name}=") || unwrap.instance_variables.include?("@#{name}") )
|
2011-12-13 06:35:57 -05:00
|
|
|
begin
|
|
|
|
return Rhino.to_javascript(method.call, self)
|
|
|
|
rescue => e
|
|
|
|
raise Function.wrap_error(e)
|
|
|
|
end
|
|
|
|
else
|
2011-12-13 07:43:07 -05:00
|
|
|
return Function.wrap(unwrap.method(name))
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
2011-12-13 11:46:04 -05:00
|
|
|
elsif unwrap.respond_to?("#{name}=")
|
|
|
|
return nil # it does have the property but is non readable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# try [](name) method :
|
|
|
|
if unwrap.respond_to?(:'[]') && unwrap.method(:'[]').arity == 1
|
|
|
|
if value = unwrap[name]
|
|
|
|
return Rhino.to_javascript(value, self)
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
super
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -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)
|
2011-12-13 07:43:07 -05:00
|
|
|
if unwrap.respond_to?(name) ||
|
|
|
|
unwrap.respond_to?("#{name}=") # might have a writer but no reader
|
2011-12-13 06:35:57 -05:00
|
|
|
return true
|
|
|
|
end
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
2011-12-13 11:46:04 -05:00
|
|
|
# try [](name) method :
|
|
|
|
if unwrap.respond_to?(:'[]') && unwrap.method(:'[]').arity == 1
|
|
|
|
return true if unwrap[name]
|
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
super
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -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)
|
2011-12-13 07:43:07 -05:00
|
|
|
if unwrap.respond_to?(set_name = "#{name}=")
|
|
|
|
return unwrap.send(set_name, Rhino.to_ruby(value))
|
2011-12-13 06:35:57 -05:00
|
|
|
end
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
2011-12-13 11:46:04 -05:00
|
|
|
# try []=(name, value) method :
|
|
|
|
if unwrap.respond_to?(:'[]=') && unwrap.method(:'[]=').arity == 2
|
|
|
|
return unwrap[name] = Rhino.to_ruby(value)
|
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
super
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
2011-12-13 07:43:07 -05:00
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
# override Object[] Scriptable#getIds();
|
|
|
|
def getIds
|
|
|
|
ids = []
|
2011-12-13 07:43:07 -05:00
|
|
|
unwrap.public_methods(false).each do |name|
|
2011-12-13 06:35:57 -05:00
|
|
|
name = name[0...-1] if name[-1, 1] == '=' # 'foo=' ... 'foo'
|
|
|
|
name = name.to_java # java.lang.String
|
|
|
|
ids << name unless ids.include?(name)
|
|
|
|
end
|
|
|
|
super.each { |id| ids.unshift(id) }
|
|
|
|
ids.to_java
|
|
|
|
end
|
2011-12-13 07:43:07 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Object < JS::ScriptableObject
|
|
|
|
include JS::Wrapper
|
|
|
|
include Scriptable
|
|
|
|
|
|
|
|
# wrap an arbitrary (ruby) object
|
|
|
|
def self.wrap(object, scope = nil)
|
|
|
|
Rhino::Ruby.cache(object) { new(object, scope) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(object, scope)
|
|
|
|
super()
|
|
|
|
@ruby = object
|
|
|
|
@scope = scope
|
|
|
|
end
|
|
|
|
|
|
|
|
# abstract Object Wrapper#unwrap();
|
|
|
|
def unwrap
|
|
|
|
@ruby
|
|
|
|
end
|
|
|
|
|
|
|
|
# abstract String Scriptable#getClassName();
|
|
|
|
def getClassName
|
2012-01-06 10:45:20 -05:00
|
|
|
@ruby.class.to_s # to_s handles 'nameless' classes as well
|
2011-12-13 07:43:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def toString
|
|
|
|
"[ruby #{getClassName}]" # [object User]
|
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
|
|
|
|
# protected Object ScriptableObject#equivalentValues(Object value)
|
|
|
|
def equivalentValues(other) # JS == operator
|
|
|
|
other.is_a?(Object) && unwrap.eql?(other.unwrap)
|
|
|
|
end
|
2012-01-06 10:45:20 -05:00
|
|
|
alias_method :'==', :equivalentValues
|
2011-12-13 06:35:57 -05:00
|
|
|
|
2011-12-13 07:43:07 -05:00
|
|
|
# override Scriptable Scriptable#getPrototype();
|
|
|
|
def getPrototype
|
|
|
|
# TODO needs to be revisited to that ruby.constructor works ...
|
|
|
|
if ! (proto = super) && @scope
|
|
|
|
JS::ScriptRuntime.setObjectProtoAndParent(self, @scope)
|
|
|
|
unless proto = super
|
|
|
|
setPrototype(proto = JS::ScriptableObject.getObjectPrototype(@scope))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
proto
|
|
|
|
end
|
|
|
|
|
2011-12-12 16:34:27 -05:00
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
|
|
|
|
class Function < JS::BaseFunction
|
2011-12-13 07:43:07 -05:00
|
|
|
include Scriptable
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
# wrap a callable (Method/Proc)
|
2012-01-06 10:45:20 -05:00
|
|
|
def self.wrap(callable, scope = nil)
|
2011-12-14 05:49:35 -05:00
|
|
|
# NOTE: === seems 'correctly' impossible without having multiple
|
|
|
|
# instances of the 'same' wrapper function (even with an UnboundMethod),
|
|
|
|
# suppose :
|
|
|
|
#
|
|
|
|
# var foo1 = one.foo;
|
|
|
|
# var foo2 = two.foo;
|
|
|
|
# foo1 === foo2; // expect 'same' reference
|
|
|
|
# foo1(); foo2(); // one ref but different implicit 'this' objects
|
|
|
|
#
|
|
|
|
# returns different instances as obj1.method(:foo) != obj2.method(:foo)
|
|
|
|
#
|
2011-12-13 06:35:57 -05:00
|
|
|
Rhino::Ruby.cache(callable) { new(callable, scope) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.wrap_error(e)
|
|
|
|
JS::WrappedException.new(org.jruby.exceptions.RaiseException.new(e))
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(callable, scope)
|
|
|
|
super()
|
|
|
|
@callable = callable
|
|
|
|
JS::ScriptRuntime.setFunctionProtoAndParent(self, scope) if scope
|
|
|
|
end
|
|
|
|
|
2011-12-13 11:46:04 -05:00
|
|
|
def unwrap
|
|
|
|
@callable
|
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
# override int BaseFunction#getLength()
|
|
|
|
def getLength
|
2011-12-14 07:22:46 -05:00
|
|
|
arity = @callable.arity
|
2012-01-06 10:45:20 -05:00
|
|
|
arity < 0 ? ( arity + 1 ).abs : arity
|
2011-12-13 06:35:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# #deprecated int BaseFunction#getArity()
|
|
|
|
def getArity
|
|
|
|
getLength
|
|
|
|
end
|
|
|
|
|
|
|
|
# override String BaseFunction#getFunctionName()
|
|
|
|
def getFunctionName
|
|
|
|
@callable.is_a?(Proc) ? "" : @callable.name
|
|
|
|
end
|
|
|
|
|
|
|
|
# protected Object ScriptableObject#equivalentValues(Object value)
|
|
|
|
def equivalentValues(other) # JS == operator
|
|
|
|
return false unless other.is_a?(Function)
|
|
|
|
return true if unwrap == other.unwrap
|
|
|
|
# Method.== does check if their bind to the same object
|
|
|
|
# JS == means they might be bind to different objects :
|
|
|
|
unwrap.to_s == other.unwrap.to_s # "#<Method: Foo#bar>"
|
|
|
|
end
|
2012-01-06 10:45:20 -05:00
|
|
|
alias_method :'==', :equivalentValues
|
2011-12-13 06:35:57 -05:00
|
|
|
|
|
|
|
# override Object BaseFunction#call(Context context, Scriptable scope,
|
|
|
|
# Scriptable thisObj, Object[] args)
|
|
|
|
def call(context, scope, this, args)
|
2011-12-14 07:22:46 -05:00
|
|
|
args = args.to_a # java.lang.Object[] -> Array
|
|
|
|
# JS function style :
|
2012-01-06 10:45:20 -05:00
|
|
|
if ( arity = @callable.arity ) != -1 # (a1, *a).arity == -2
|
|
|
|
if arity > -1 && args.size > arity # omit 'redundant' arguments
|
2011-12-14 07:22:46 -05:00
|
|
|
args = args.slice(0, arity)
|
2012-01-06 10:45:20 -05:00
|
|
|
elsif arity > args.size || # fill 'missing' arguments
|
|
|
|
( arity < -1 && (arity = arity.abs - 1) > args.size )
|
2011-12-14 07:22:46 -05:00
|
|
|
(arity - args.size).times { args.push(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rb_args = Rhino.args_to_ruby(args)
|
2011-12-13 06:35:57 -05:00
|
|
|
begin
|
|
|
|
result = @callable.call(*rb_args)
|
|
|
|
rescue => e
|
|
|
|
# ... correct wrapping thus it's try { } catch (e) works in JS :
|
|
|
|
raise self.class.wrap_error(e)
|
|
|
|
end
|
|
|
|
Rhino.to_javascript(result, scope)
|
|
|
|
end
|
|
|
|
|
2011-12-12 16:34:27 -05:00
|
|
|
end
|
2011-12-13 06:35:57 -05:00
|
|
|
|
|
|
|
class Constructor < Function
|
|
|
|
include JS::Wrapper
|
|
|
|
|
|
|
|
# wrap a ruby class as as constructor function
|
|
|
|
def self.wrap(klass, scope = nil)
|
|
|
|
new(klass, scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(klass, scope)
|
|
|
|
super(klass.method(:new), scope)
|
|
|
|
@klass = klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def unwrap
|
|
|
|
@klass
|
|
|
|
end
|
|
|
|
|
2011-12-13 11:46:04 -05:00
|
|
|
# override int BaseFunction#getLength()
|
|
|
|
def getLength
|
2011-12-14 07:22:46 -05:00
|
|
|
arity = @klass.instance_method(:initialize).arity
|
2012-01-06 10:45:20 -05:00
|
|
|
arity < 0 ? ( arity + 1 ).abs : arity
|
2011-12-13 11:46:04 -05:00
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
# override boolean Scriptable#hasInstance(Scriptable instance);
|
|
|
|
def hasInstance(instance)
|
|
|
|
return false unless instance
|
|
|
|
return true if instance.is_a?(@klass)
|
|
|
|
instance.is_a?(Object) && instance.unwrap.is_a?(@klass)
|
|
|
|
end
|
|
|
|
|
2011-12-12 16:34:27 -05:00
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
def self.cache(key)
|
2012-01-06 10:45:20 -05:00
|
|
|
return yield unless @@cache
|
2011-12-14 07:22:46 -05:00
|
|
|
fetch(key) || store(key, yield)
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
private
|
2011-12-12 16:17:42 -05:00
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
# NOTE: just to get === comparison's working ...
|
|
|
|
# if == is enough might be disabled by setting to nil
|
|
|
|
@@cache = java.util.WeakHashMap.new
|
2011-12-12 16:17:42 -05:00
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
def self.fetch(key)
|
2012-01-06 10:45:20 -05:00
|
|
|
ref = @@cache.get(key)
|
2011-12-13 06:35:57 -05:00
|
|
|
ref ? ref.get : nil
|
|
|
|
end
|
|
|
|
|
2011-12-14 07:22:46 -05:00
|
|
|
def self.store(key, value)
|
2012-01-06 10:45:20 -05:00
|
|
|
@@cache.put(key, java.lang.ref.WeakReference.new(value))
|
2011-12-13 06:35:57 -05:00
|
|
|
value
|
2011-12-12 16:17:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-12-13 06:35:57 -05:00
|
|
|
RubyObject = Ruby::Object
|
|
|
|
RubyFunction = Ruby::Function
|
|
|
|
RubyConstructor = Ruby::Constructor
|
2011-12-12 16:17:42 -05:00
|
|
|
|
|
|
|
end
|