Sha256: 1e31aff777edefae74b8dda5f5b15936bef5006c8a8ff6d926c40868b7a4093e

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

class Array
  def self.inherited(klass)
    replace = Class.new(Array::Wrapper)

    %x{
      klass.$$proto         = replace.$$proto;
      klass.$$proto.$$class = klass;
      klass.$$alloc         = replace.$$alloc;
      klass.$$parent        = #{Array::Wrapper};

      klass.$allocate = replace.$allocate;
      klass.$new      = replace.$new;
      klass["$[]"]    = replace["$[]"];
    }
  end
end

class Array::Wrapper
  def self.allocate(array = [])
    obj = super()
    `obj.literal = array`
    obj
  end

  def self.new(*args, &block)
    obj = allocate
    obj.initialize(*args, &block)
    obj
  end

  def self.[](*objects)
    allocate(objects)
  end

  def initialize(*args, &block)
    @literal = Array.new(*args, &block)
  end

  def method_missing(*args, &block)
    result = @literal.__send__(*args, &block)

    if `result === #@literal`
      self
    else
      result
    end
  end

  def initialize_copy(other)
    @literal = `other.literal`.clone
  end

  def respond_to?(name, *)
    super || @literal.respond_to?(name)
  end

  def ==(other)
    @literal == other
  end

  def eql?(other)
    @literal.eql?(other)
  end

  def to_a
    @literal
  end

  def to_ary
    self
  end

  def inspect
    @literal.inspect
  end

  # wrapped results
  def *(other)
    %x{
      var result = #{@literal * other};

      if (result.$$is_array) {
        return #{self.class.allocate(`result`)}
      }
      else {
        return result;
      }
    }
  end

  def [](index, length = undefined)
    %x{
      var result = #{@literal.slice(index, length)};

      if (result.$$is_array && (index.$$is_range || length !== undefined)) {
        return #{self.class.allocate(`result`)}
      }
      else {
        return result;
      }
    }
  end

  alias slice []

  def uniq
    self.class.allocate(@literal.uniq)
  end

  def flatten(level = undefined)
    self.class.allocate(@literal.flatten(level))
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opal-0.7.2 opal/corelib/array/inheritance.rb
opal-0.7.1 opal/corelib/array/inheritance.rb
opal-0.7.0 opal/corelib/array/inheritance.rb
opal-0.7.0.rc1 opal/corelib/array/inheritance.rb
opal-0.7.0.beta3 opal/corelib/array/inheritance.rb
opal-0.7.0.beta2 opal/corelib/array/inheritance.rb