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.



225
226
227
228
# File 'lib/rhino/ruby.rb', line 225

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



219
220
221
222
223
# File 'lib/rhino/ruby.rb', line 219

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()



235
236
237
238
# File 'lib/rhino/ruby.rb', line 235

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

#hasInstance(instance) ⇒ Object

override boolean Scriptable#hasInstance(Scriptable instance);



241
242
243
244
245
# File 'lib/rhino/ruby.rb', line 241

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



230
231
232
# File 'lib/rhino/ruby.rb', line 230

def unwrap
  @klass
end