Class: Nashorn::Ruby::Function

Inherits:
JS::AbstractJSObject
  • Object
show all
Includes:
Scriptable
Defined in:
lib/nashorn/ruby.rb

Direct Known Subclasses

Constructor

Instance Attribute Summary

Attributes included from Scriptable

#unwrap

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scriptable

#getMember, #getSlot, #hasMember, #hasSlot, #keySet, #removeMember, #setMember, #setSlot

Constructor Details

#initialize(callable) ⇒ Function

Returns a new instance of Function.



196
197
198
199
# File 'lib/nashorn/ruby.rb', line 196

def initialize(callable)
  super()
  @unwrap = callable
end

Class Method Details

.wrap(callable) ⇒ Object

wrap a callable (Method/Proc)



192
193
194
# File 'lib/nashorn/ruby.rb', line 192

def self.wrap(callable)
  Ruby.cache(callable.to_s) { new(callable) }
end

Instance Method Details

#==(other) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/nashorn/ruby.rb', line 252

def ==(other)
  if other.is_a?(Object)
    unwrap == other.unwrap
  else
    unwrap == other
  end
end

#arityObject



242
# File 'lib/nashorn/ruby.rb', line 242

def arity; length end

#call(*args) ⇒ Object Also known as: __call__



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/nashorn/ruby.rb', line 261

def call(*args) # call(Object thiz, Object... args)
  # unless args.first.is_a?(JS::Context)
  #   return super # assume a Ruby #call
  # end

  # NOTE: distinguish a Ruby vs Java call here :
  arr = args[1]
  if arr && args.size == 2 && # Java Function#call dispatch
     arr.respond_to?(:java_class) && arr.java_class.array?
    this = args[0]; args = arr.to_a; java_args = true
  end
  # this = args.shift # Java Function#call dispatch

  callable = @unwrap

  if callable.is_a?(UnboundMethod)
    this = args.shift unless java_args
    callable = callable.bind(Nashorn.to_rb(this)) # TODO wrap TypeError ?
  end
  # JS function style :
  if ( arity = callable.arity ) != -1 # (a1, *a).arity == -2
    if arity > -1 && args.size > arity # omit 'redundant' arguments
      args = args.slice(0, arity)
    elsif arity > args.size || # fill 'missing' arguments
        ( arity < -1 && (arity = arity.abs - 1) > args.size )
      (arity - args.size).times { args.push(nil) }
    end
  end
  rb_args = Nashorn.args_to_rb(args)
  begin
    result = callable.call(*rb_args)
  rescue StandardError, ScriptError => e
    raise e unless java_args
    # TODO is this wrapping needed with __Nashorn__ ?
    raise Ruby.wrap_error(e, e.backtrace) # thus `try { } catch (e)` works in JS
  end
  java_args ? Nashorn.to_js(result) : result
  # Nashorn.to_js(result) # TODO do not convert if java_args ?
end

#equals(other) ⇒ Object

JS == operator



244
245
246
247
248
249
250
# File 'lib/nashorn/ruby.rb', line 244

def equals(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

#getClassNameObject



202
203
204
# File 'lib/nashorn/ruby.rb', line 202

def getClassName
  @unwrap.to_s # to_s handles 'nameless' classes as well
end

#getDefaultValue(hint) ⇒ Object



221
222
223
# File 'lib/nashorn/ruby.rb', line 221

def getDefaultValue(hint)
  @unwrap.to_s
end

#isArrayObject



226
# File 'lib/nashorn/ruby.rb', line 226

def isArray; false end

#isFunctionObject



229
# File 'lib/nashorn/ruby.rb', line 229

def isFunction; true end

#isInstance(instance) ⇒ Object



211
212
213
# File 'lib/nashorn/ruby.rb', line 211

def isInstance(instance)
  instance.class.equal? @unwrap
end

#isInstanceOf(clazz) ⇒ Object



216
217
218
# File 'lib/nashorn/ruby.rb', line 216

def isInstanceOf(clazz)
  @unwrap.is_a?(clazz)
end

#isStrictFunctionObject



232
# File 'lib/nashorn/ruby.rb', line 232

def isStrictFunction; false end

#lengthObject

def newObject(args); fail end



237
238
239
240
# File 'lib/nashorn/ruby.rb', line 237

def length # getLength
  arity = @unwrap.arity
  arity < 0 ? ( arity + 1 ).abs : arity
end