class Rhino::Ruby::Function

Public Class Methods

new(callable, scope) click to toggle source
Calls superclass method
# File lib/rhino/ruby.rb, line 141
def initialize(callable, scope)
  super()
  @callable = callable
  JS::ScriptRuntime.setFunctionProtoAndParent(self, scope) if scope
end
wrap(callable, scope = nil) click to toggle source

wrap a callable (Method/Proc)

# File lib/rhino/ruby.rb, line 135
def self.wrap(callable, scope = nil)
  # NOTE: include JS::Wrapper & Ruby.cache(callable.to_s) guarantees ===
  # in Rhino although if a bind Method gets passed it might get confusing
  Ruby.cache(callable.to_s) { new(callable, scope) }
end

Public Instance Methods

'=='(other)
Alias for: equivalentValues
__call__(*args)

make sure redefined :call is aliased not the one “inherited” from JS::BaseFunction#call when invoking __call__ (@see rhino_ext.rb)

Alias for: call
call(*args) click to toggle source

override Object BaseFunction#call(Context context, Scriptable scope,

Scriptable thisObj, Object[] args)
Calls superclass method
# File lib/rhino/ruby.rb, line 179
def call(*args)
  unless args.first.is_a?(JS::Context)
    return super # assume a Ruby #call
  end
  _, scope, this, args = *args # Java Function#call dispatch
  args = args.to_a # java.lang.Object[] -> Array
  # 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 = Rhino.args_to_ruby(args)
  begin
    callable = 
      if @callable.is_a?(UnboundMethod)
        @callable.bind(Rhino.to_ruby(this)) # might end up as TypeError
      else
        @callable
      end
    result = callable.call(*rb_args)
  rescue StandardError, ScriptError => e
    raise Ruby.wrap_error(e) # thus `try { } catch (e)` works in JS
  end
  Rhino.to_javascript(result, scope)
end
Also aliased as: __call__
equivalentValues(other) click to toggle source

protected Object ScriptableObject#equivalentValues(Object value)

# File lib/rhino/ruby.rb, line 168
def equivalentValues(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
Also aliased as: '=='
getArity() click to toggle source

deprecated int BaseFunction#getArity()

# File lib/rhino/ruby.rb, line 158
def getArity
  getLength
end
getFunctionName() click to toggle source

override String BaseFunction#getFunctionName()

# File lib/rhino/ruby.rb, line 163
def getFunctionName
  @callable.is_a?(Proc) ? "" : @callable.name
end
getLength() click to toggle source

override int BaseFunction#getLength()

# File lib/rhino/ruby.rb, line 152
def getLength
  arity = @callable.arity
  arity < 0 ? ( arity + 1 ).abs : arity
end
unwrap() click to toggle source
# File lib/rhino/ruby.rb, line 147
def unwrap
  @callable
end