class Nashorn::Ruby::Function
Public Class Methods
new(callable)
click to toggle source
Calls superclass method
# File lib/nashorn/ruby.rb, line 196 def initialize(callable) super() @unwrap = callable end
wrap(callable)
click to toggle source
wrap a callable (Method/Proc)
# File lib/nashorn/ruby.rb, line 192 def self.wrap(callable) Ruby.cache(callable.to_s) { new(callable) } end
Public Instance Methods
==(other)
click to toggle source
# File lib/nashorn/ruby.rb, line 252 def ==(other) if other.is_a?(Object) unwrap == other.unwrap else unwrap == other end end
__call__(*args)
make sure redefined :call is aliased not the one “inherited” (@see ext.rb)
Alias for: call
arity()
click to toggle source
# File lib/nashorn/ruby.rb, line 242 def arity; length end
call(*args)
click to toggle source
@override
# 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
Also aliased as: __call__
equals(other)
click to toggle source
# 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
getClassName()
click to toggle source
@override ECMA [[Class]] property
# File lib/nashorn/ruby.rb, line 202 def getClassName @unwrap.to_s # to_s handles 'nameless' classes as well end
getDefaultValue(hint)
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 221 def getDefaultValue(hint) @unwrap.to_s end
isArray()
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 226 def isArray; false end
isFunction()
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 229 def isFunction; true end
isInstance(instance)
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 211 def isInstance(instance) instance.class.equal? @unwrap end
isInstanceOf(clazz)
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 216 def isInstanceOf(clazz) @unwrap.is_a?(clazz) end
isStrictFunction()
click to toggle source
@override
# File lib/nashorn/ruby.rb, line 232 def isStrictFunction; false end
length()
click to toggle source
@override
def newObject(args); fail end
# File lib/nashorn/ruby.rb, line 237 def length # getLength arity = @unwrap.arity arity < 0 ? ( arity + 1 ).abs : arity end