Skip to content

Commit

Permalink
Fix Array#splice with large sparse arrays in Safari 7/8, and Opera …
Browse files Browse the repository at this point in the history
…12.15

Fixes #295
  • Loading branch information
ljharb committed Jun 20, 2015
1 parent e3ab60d commit 061787f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
80 changes: 80 additions & 0 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,86 @@ defineProperties(ArrayPrototype, {
return array_splice.apply(this, args);
}
}, !spliceWorksWithEmptyObject);
var spliceWorksWithLargeSparseArrays = (function () {
// Per https://github.com/es-shims/es5-shim/issues/295
// Safari 7/8 breaks with sparse arrays of size 1e5 or greater
var arr = new $Array(1e5);
// note: the index MUST be 8 or larger or the test will false pass
arr[8] = 'x';
arr.splice(1, 1);
return arr.indexOf('x') === 7;
}());
var spliceWorksWithSmallSparseArrays = (function () {
// Per https://github.com/es-shims/es5-shim/issues/295
// Opera 12.15 breaks on this, no idea why.
var n = 256;
var arr = [];
arr[n] = 'a';
arr.splice(n + 1, 0, 'b');
return arr[n] === 'a';
}());
defineProperties(ArrayPrototype, {
splice: function splice(start, deleteCount) {
var O = ES.ToObject(this);
var A = [];
var len = ES.ToUint32(O.length);
var relativeStart = ES.ToInteger(start);
var actualStart = relativeStart < 0 ? max((len + relativeStart), 0) : min(relativeStart, len);
var actualDeleteCount = min(max(ES.ToInteger(deleteCount), 0), len - actualStart);

var k = 0;
var from;
while (k < actualDeleteCount) {
from = $String(actualStart + k);
if (owns(O, from)) {
A[k] = O[from];
}
k += 1;
}

var items = array_slice.call(arguments, 2);
var itemCount = items.length;
var to;
if (itemCount < actualDeleteCount) {
k = actualStart;
while (k < (len - actualDeleteCount)) {
from = $String(k + actualDeleteCount);
to = $String(k + itemCount);
if (owns(O, from)) {
O[to] = O[from];
} else {
delete O[to];
}
k += 1;
}
k = len;
while (k > (len - actualDeleteCount + itemCount)) {
delete O[k - 1];
k -= 1;
}
} else if (itemCount > actualDeleteCount) {
k = len - actualDeleteCount;
while (k > actualStart) {
from = $String(k + actualDeleteCount - 1);
to = $String(k + itemCount - 1);
if (owns(O, from)) {
O[to] = O[from];
} else {
delete O[to];
}
k -= 1;
}
}
k = actualStart;
for (var i = 0; i < items.length; ++i) {
O[k] = items[i];
k += 1;
}
O.length = len - actualDeleteCount + itemCount;

return A;
}
}, !spliceWorksWithLargeSparseArrays || !spliceWorksWithSmallSparseArrays);

// ES5 15.4.4.12
// http://es5.github.com/#x15.4.4.13
Expand Down
24 changes: 24 additions & 0 deletions tests/spec/s-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,30 @@ describe('Array', function () {
expect(obj.length).toBe(3);
expect(obj[0]).toBe(1);
});

it('should not break on sparse arrays in Opera', function () {
// test from https://github.com/wikimedia/VisualEditor/blob/d468b00311e69c2095b9da360c5745153342a5c3/src/ve.utils.js#L182-L196
var n = 256;
var arr = [];
arr[n] = 'a';
arr.splice(n + 1, 0, 'b');
expect(arr[n]).toBe('a');
});

it('should not break on sparse arrays in Safari 7/8', function () {
// test from https://github.com/wikimedia/VisualEditor/blob/d468b00311e69c2095b9da360c5745153342a5c3/src/ve.utils.js#L182-L196
var justFine = new Array(1e5 - 1);
justFine[10] = 'x';
var tooBig = new Array(1e5);
tooBig[8] = 'x';

justFine.splice(1, 1);
expect(8 in justFine).toBe(false);
expect(justFine.indexOf('x')).toBe(9);
tooBig.splice(1, 1);
expect(6 in tooBig).toBe(false);
expect(tooBig.indexOf('x')).toBe(7);
});
});

});

0 comments on commit 061787f

Please sign in to comment.