From b9afbde402212921d57a15c5f9d5b7ce03908c71 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 5 Mar 2015 01:25:45 -0800 Subject: [PATCH] Revert "Use ES.ArraySpeciesCreate properly." and "Overwrite `Array#slice` so that it supports Array subclasses." This reverts commits b5731c5505fdae884f6d78ce41928b7307f2b39f6 and dd9804ddede0515b34646959d409c3c7a259e61. Fixes #322 --- es6-shim.js | 51 --------------------------------------------------- test/array.js | 38 -------------------------------------- 2 files changed, 89 deletions(-) diff --git a/es6-shim.js b/es6-shim.js index 3db0fb74..d99c7627 100644 --- a/es6-shim.js +++ b/es6-shim.js @@ -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'; @@ -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; @@ -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. diff --git a/test/array.js b/test/array.js index 6939aa21..299f6613 100644 --- a/test/array.js +++ b/test/array.js @@ -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'; @@ -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 = [];