Class: Rhino::Ruby::Constructor

Inherits:
Function show all
Includes:
JS::Wrapper
Defined in:
lib/rhino/ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#call, #equivalentValues, #getArity, #getFunctionName

Methods included from Scriptable

access, access=, #get, #getIds, #has, #put

Constructor Details

#initialize(klass, scope) ⇒ Constructor

Returns a new instance of Constructor.



190
191
192
193
# File 'lib/rhino/ruby.rb', line 190

def initialize(klass, scope)
  super(klass.method(:new), scope)
  @klass = klass
end

Class Method Details

.wrap(klass, scope = nil) ⇒ Object

wrap a ruby class as as constructor function



184
185
186
187
188
# File 'lib/rhino/ruby.rb', line 184

def self.wrap(klass, scope = nil)
  # NOTE: caching here seems redundant since we implemented JS::Wrapper 
  # and a ruby class objects seems always the same ref under JRuby ...
  Ruby.cache(klass) { new(klass, scope) }
end

Instance Method Details

#getLengthObject

override int BaseFunction#getLength()



200
201
202
203
# File 'lib/rhino/ruby.rb', line 200

def getLength
  arity = @klass.instance_method(:initialize).arity
  arity < 0 ? ( arity + 1 ).abs : arity
end

#hasInstance(instance) ⇒ Object

override boolean Scriptable#hasInstance(Scriptable instance);



206
207
208
209
210
# File 'lib/rhino/ruby.rb', line 206

def hasInstance(instance)
  return false unless instance
  return true if instance.is_a?(@klass)
  instance.is_a?(Object) && instance.unwrap.is_a?(@klass)
end

#unwrapObject



195
196
197
# File 'lib/rhino/ruby.rb', line 195

def unwrap
  @klass
end