Skip to content

Commit

Permalink
Revert "Use ES.ArraySpeciesCreate properly." and "Overwrite `Array#sl…
Browse files Browse the repository at this point in the history
…ice` so that it supports Array subclasses."

This reverts commits b5731c5505fdae884f6d78ce41928b7307f2b39f6 and dd9804ddede0515b34646959d409c3c7a259e61.

Fixes #322
  • Loading branch information
ljharb committed Mar 5, 2015
1 parent 64909cf commit b9afbde
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 89 deletions.
51 changes: 0 additions & 51 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
var _hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
var ArrayIterator; // make our implementation private
var noop = function () {};
var arraySlice = Array.prototype.slice;

var Symbol = globals.Symbol || {};
var symbolSpecies = Symbol.species || '@@species';
Expand Down Expand Up @@ -318,28 +317,6 @@
return result;
},

ArraySpeciesCreate: function (originalArray, length) {
if (!Number.isInteger(length) || length < 0) {
throw new TypeError('length must be 0 or greater');
}
var len = length === 0 ? 0 : length; // eliminate negative zero
var C;
var isArray = Array.isArray(originalArray);
if (isArray) {
C = originalArray.constructor;
if (ES.TypeIsObject(C)) {
C = C[symbolSpecies];
if (C === null) {
C = undefined;
}
}
}
if (typeof C === 'undefined') {
return new Array(len);
}
return ES.Construct(C, len);
},

Construct: function (C, args) {
// CreateFromConstructor
var obj;
Expand Down Expand Up @@ -1000,34 +977,6 @@
}
defineProperties(Array.prototype, ArrayPrototypeShims);

var Empty = function Empty() {};
if (!(Array.prototype.slice.call(new Empty()) instanceof Empty)) {
defineProperty(Array.prototype, 'slice', function slice(start, end) {
if (this instanceof Array || isArguments(this)) {
return arraySlice.apply(this, arguments);
}
var O = ES.ToObject(this);
var len = ES.ToLength(this.length);
var relativeStart = ES.ToInteger(start);
var k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
var relativeEnd = typeof end === 'undefined' ? len : ES.ToInteger(end);
var finalEnd = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
var count = Math.max(finalEnd - k, 0);
var A = ES.ArraySpeciesCreate(O, count);
var n = 0;
while (k < finalEnd) {
if (_hasOwnProperty(O, k)) {
A[n] = O[k];
}
k += 1;
n += 1;
}
A.length = n;
return A;
}, true);
Value.preserveToString(Array.prototype.slice, arraySlice);
}

addIterator(Array.prototype, function () { return this.values(); });
// Chrome defines keys/values/entries on Array, but doesn't give us
// any way to identify its iterator. So add our own shimmed field.
Expand Down
38 changes: 0 additions & 38 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var runArrayTests = function () {
'use strict';

var Sym = typeof Symbol !== 'undefined' ? Symbol : {};
var symbolSpecies = Sym.species || '@@species';
var isSymbol = function (sym) {
/*jshint notypeof: true */
return typeof Sym === 'function' && typeof sym === 'symbol';
Expand All @@ -22,43 +21,6 @@ var runArrayTests = function () {
expect(exported.Array).to.equal(Array);
});

var assertArrayLike = function (a, array) {
expect(a.length).to.equal(array.length);
expect(Array.from(a)).to.eql(array);
};

describe('#slice()', function () {
var C;
beforeEach(function () {
C = function C() {
var arr = Array.apply(this, arguments);
Object.assign(this, arr);
this.length = arr.length;
};
C[symbolSpecies] = C;
});

it('works on non-Arrays', function () {
var c = new C(1, 2, 3, 4);
assertArrayLike(c, [1, 2, 3, 4]);
expect(c).to.be.an.instanceOf(C);
var c2 = Array.prototype.slice.call(c, 1, -1);
assertArrayLike(c2, [2, 3]);
expect(c2).to.be.an.instanceOf(Array);
});

it('works on Arrays', function () {
C.prototype = Array.prototype;
var c = new C(1, 2, 3, 4);
assertArrayLike(c, [1, 2, 3, 4]);
expect(c).to.be.an.instanceOf(C);
expect(Array.isArray(c)).to.equal(true);
var c2 = Array.prototype.slice.call(c, 1, -1);
assertArrayLike(c2, [2, 3]);
expect(c2).to.be.an.instanceOf(C);
});
});

describe('@@iterator', function () {
it('uses Symbol.iterator if available', function () {
var a = [];
Expand Down

0 comments on commit b9afbde

Please sign in to comment.