Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Array.prototype.copyWithin polyfill #2902

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions src/com/google/javascript/jscomp/js/es6/array/copywithin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
'require util/polyfill';

$jscomp.polyfill('Array.prototype.copyWithin', function(orig) {
// requires strict mode to throw for invalid `this` or params
'use strict';

if (orig) return orig;

/**
Expand All @@ -31,32 +34,46 @@ $jscomp.polyfill('Array.prototype.copyWithin', function(orig) {
*/
var polyfill = function(target, start, opt_end) {
var len = this.length;
target = Number(target);
start = Number(start);
opt_end = Number(opt_end != null ? opt_end : len);
if (target < start) {
opt_end = Math.min(opt_end, len);
while (start < opt_end) {
if (start in this) {
this[target++] = this[start++];
target = toInteger(target);
start = toInteger(start);
var end = opt_end === undefined ? len : toInteger(opt_end);
var to = target < 0 ? Math.max(len + target, 0) : Math.min(target, len);
shicks marked this conversation as resolved.
Show resolved Hide resolved
var from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
var final = end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
if (to < from) {
while (from < final) {
if (from in this) {
this[to++] = this[from++];
} else {
delete this[target++];
start++;
delete this[to++];
from++;
}
}
} else {
opt_end = Math.min(opt_end, len + start - target);
target += opt_end - start;
while (opt_end > start) {
if (--opt_end in this) {
this[--target] = this[opt_end];
final = Math.min(final, len + from - to);
to += final - from;
while (final > from) {
if (--final in this) {
this[--to] = this[final];
} else {
delete this[target];
delete this[--to];
}
}
}
return this;
};

/**
* @param {number} arg
* @return {number}
*/
function toInteger(arg) {
var n = Number(arg);
if (n === Infinity || n === -Infinity) {
return n;
}
return n | 0;
};

return polyfill;
}, 'es6', 'es3');
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,55 @@ testSuite({
arr = {length: 3, 1: 4, 3: 'unused'};
assertEquals(arr, Array.prototype.copyWithin.call(noCheck(arr), 0, 1));
assertObjectEquals({length: 3, 0: 4, 3: 'unused'}, arr);

arr = {length: 3, 1: 4, 3: 'unused'};
assertEquals(arr, Array.prototype.copyWithin.call(noCheck(arr), 1, 0));
assertObjectEquals({length: 3, 2: 4, 3: 'unused'}, arr);
},

testCopyWithin_coercingArgs() {
assertObjectEquals([1, 2, 3, 3], [0, 1, 2, 3].copyWithin(NaN, 1));
assertObjectEquals([1, 2, 3, 3], [0, 1, 2, 3].copyWithin(0.5, 1));
assertObjectEquals([0, 0, 1, 2], [0, 1, 2, 3].copyWithin(1.5, 0));

assertObjectEquals([0, 0, 1, 2], [0, 1, 2, 3].copyWithin(1, NaN));
assertObjectEquals([0, 0, 1, 2], [0, 1, 2, 3].copyWithin(1, 0.5));
assertObjectEquals([1, 2, 3, 3], [0, 1, 2, 3].copyWithin(0, 1.5));

assertObjectEquals([0, 1, 2, 3], [0, 1, 2, 3].copyWithin(1, 0, NaN));
assertObjectEquals([0, 0, 2, 3], [0, 1, 2, 3].copyWithin(1, 0, 1.5));
assertObjectEquals([0, 0, 1, 3], [0, 1, 2, 3].copyWithin(1, 0, -2.5));
assertObjectEquals([0, 0, 1, 2], [0, 1, 2, 3].copyWithin(1, 0, undefined));
},

testCopyWithin_negativeArgs() {
assertObjectEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(-2, 0));
assertObjectEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(1, -3));
assertObjectEquals([1, 3, 4, 4, 5], [1, 2, 3, 4, 5].copyWithin(1, 2, -1));
},

testCopyWithin_throwsIfNullish() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test no longer passes. Please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? It should be pass and I believe it passes.

this is null and this.length is referred on the top of the polyfill function, so it should throw an error.
https://github.com/google/closure-compiler/pull/2902/files#diff-a5e0f0788d56ad46f3db1223fd3c8b5cR36

Copy link
Contributor

@tjgq tjgq Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look close enough: it fails because the start and target arguments are not optional (unfortunately, I don't think there's a way to run the polyfill tests externally, so there's no way for you to reproduce it). The best you can do is something like this:

Array.prototype.copyWithin.call(null, some_other_array, 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjgq understood. It is not a runtime test, but a type check failed. I added the second and the third argument.

assertThrows(function() {
Array.prototype.copyWithin.call(null, 0, 0);
});
assertThrows(function() {
Array.prototype.copyWithin.call(undefined, 0, 0);
});
},

testCopyWithin_throwIfFailToDeleteProperty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjgq Probably type check in 2nd arg of Object.defineProperty() failed, so I fixed it.

var obj = {
length: 5
};

Object.defineProperty(obj, '4', {
value: 'a',
configurable: false,
writable: true
});

assertThrows(function() {
Array.prototype.copyWithin.call(noCheck(obj), 4, 0);
});
},
});