Class: Nashorn::Ruby::Function
- Inherits:
-
JS::AbstractJSObject
- Object
- JS::AbstractJSObject
- Nashorn::Ruby::Function
show all
- Includes:
- Scriptable
- Defined in:
- lib/nashorn/ruby.rb
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
|
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)
arr = args[1]
if arr && args.size == 2 && arr.respond_to?(:java_class) && arr.java_class.array?
this = args[0]; args = arr.to_a; java_args = true
end
callable = @unwrap
if callable.is_a?(UnboundMethod)
this = args.shift unless java_args
callable = callable.bind(Nashorn.to_rb(this)) end
if ( arity = callable.arity ) != -1 if arity > -1 && args.size > arity args = args.slice(0, arity)
elsif arity > args.size || ( 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
raise Ruby.wrap_error(e, e.backtrace) end
java_args ? Nashorn.to_js(result) : result
end
|
#equals(other) ⇒ Object
244
245
246
247
248
249
250
|
# File 'lib/nashorn/ruby.rb', line 244
def equals(other) return false unless other.is_a?(Function)
return true if unwrap == other.unwrap
unwrap.to_s == other.unwrap.to_s end
|
#getClassName ⇒ Object
202
203
204
|
# File 'lib/nashorn/ruby.rb', line 202
def getClassName
@unwrap.to_s end
|
#getDefaultValue(hint) ⇒ Object
221
222
223
|
# File 'lib/nashorn/ruby.rb', line 221
def getDefaultValue(hint)
@unwrap.to_s
end
|
226
|
# File 'lib/nashorn/ruby.rb', line 226
def isArray; false end
|
#isFunction ⇒ Object
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
|
#isStrictFunction ⇒ Object
232
|
# File 'lib/nashorn/ruby.rb', line 232
def isStrictFunction; false end
|
def newObject(args); fail end
237
238
239
240
|
# File 'lib/nashorn/ruby.rb', line 237
def length arity = @unwrap.arity
arity < 0 ? ( arity + 1 ).abs : arity
end
|