Sha256: 2883fb9706dc95f85f992ffc80ad7f443d10d40d83e4f5d65cf674484fed58e4

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

class Method
  attr_reader :owner, :receiver, :name

  def initialize(receiver, method, name)
    @receiver = receiver
    @owner    = receiver.class
    @name     = name
    @method   = method
  end

  def arity
    @method.arity
  end

  def call(*args, &block)
    %x{
      #@method.$$p = block;

      return #@method.apply(#@receiver, args);
    }
  end

  alias [] call

  def unbind
    UnboundMethod.new(@owner, @method, @name)
  end

  def to_proc
    %x{
      var proc = function () { return self.$call.apply(self, $slice.call(arguments)); };
      proc.$$unbound = #@method;
      proc.$$is_lambda = true;
      return proc;
    }
  end

  def inspect
    "#<Method: #{@receiver.class}##@name>"
  end
end

class UnboundMethod
  attr_reader :owner, :name

  def initialize(owner, method, name)
    @owner  = owner
    @method = method
    @name   = name
  end

  def arity
    @method.arity
  end

  def bind(object)
    # TODO: re-enable when Module#< is fixed
    # unless object.class <= @owner
    #   raise TypeError, "can't bind singleton method to a different class"
    # end
    Method.new(object, @method, @name)
  end

  def inspect
    "#<UnboundMethod: #{@owner.name}##@name>"
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
opal-0.9.4 opal/corelib/method.rb
opal-0.9.3 opal/corelib/method.rb
opal-0.10.0.beta2 opal/corelib/method.rb
opal-0.10.0.beta1 opal/corelib/method.rb
opal-0.9.2 opal/corelib/method.rb
opal-0.9.0 opal/corelib/method.rb
opal-0.9.0.rc1 opal/corelib/method.rb
opal-0.9.0.beta2 opal/corelib/method.rb
opal-0.9.0.beta1 opal/corelib/method.rb