Skip to content

Commit

Permalink
[es6] implement Array.prototype.copyWithin()
Browse files Browse the repository at this point in the history
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.copywithin

BUG=v8:4039
R=adamk@chromium.org
LOG=N

Review URL: https://codereview.chromium.org/376623004

Cr-Commit-Position: refs/heads/master@{#27983}
  • Loading branch information
caitp authored and Junliang Yan committed Aug 18, 2015
1 parent f3aa3ce commit ecfb8d9
Show file tree
Hide file tree
Showing 2 changed files with 393 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/harmony-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,59 @@ var GlobalSymbol = global.Symbol;

// -------------------------------------------------------------------

// ES6 draft 03-17-15, section 22.1.3.3
function ArrayCopyWithin(target, start, end) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");

var array = TO_OBJECT_INLINE(this);
var length = ToLength(array.length);

target = TO_INTEGER(target);
var to;
if (target < 0) {
to = $max(length + target, 0);
} else {
to = $min(target, length);
}

start = TO_INTEGER(start);
var from;
if (start < 0) {
from = $max(length + start, 0);
} else {
from = $min(start, length);
}

end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
var final;
if (end < 0) {
final = $max(length + end, 0);
} else {
final = $min(end, length);
}

var count = $min(final - from, length - to);
var direction = 1;
if (from < to && to < (from + count)) {
direction = -1;
from = from + count - 1;
to = to + count - 1;
}

while (count > 0) {
if (from in array) {
array[to] = array[from];
} else {
delete array[to];
}
from = from + direction;
to = to + direction;
count--;
}

return array;
}

// ES6 draft 07-15-13, section 15.4.3.23
function ArrayFind(predicate /* thisArg */) { // length == 1
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
Expand Down Expand Up @@ -216,6 +269,7 @@ InstallConstants(GlobalSymbol, [
"isConcatSpreadable", symbolIsConcatSpreadable
]);

%FunctionSetLength(ArrayCopyWithin, 2);
%FunctionSetLength(ArrayFrom, 1);

// Set up non-enumerable functions on the Array object.
Expand All @@ -226,6 +280,7 @@ InstallFunctions(GlobalArray, DONT_ENUM, [

// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
"copyWithin", ArrayCopyWithin,
"find", ArrayFind,
"findIndex", ArrayFindIndex,
"fill", ArrayFill
Expand Down
Loading

0 comments on commit ecfb8d9

Please sign in to comment.