Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Unified Diff: src/harmony-array.js

Issue 376623004: [es6] implement Array.prototype.copyWithin() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-copywithin.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-array.js
diff --git a/src/harmony-array.js b/src/harmony-array.js
index dc7d71fcb4dca34110756793db4953325ca95b8e..d5dde144657494ff1fe34e65d323ea3e1090bbde 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -10,6 +10,72 @@
// -------------------------------------------------------------------
+// ES6 draft 05-22-14, section 22.1.3.3
+function ArrayCopyWithin(target, start /* [ , end ] */ ) { // length == 2
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
adamk 2015/04/20 17:26:14 For consistency I feel like we might as well leave
+
+ var array = ToObject(this);
+ var length = ToInteger(array.length);
+ var to;
+ var from;
+ var relativeEnd = length;
+ var end;
+ var final;
+ var count;
+ var direction = 1;
+ var fromVal;
+
+ target = ToInteger(target);
+ if (target < 0) {
+ to = MathMax(length + target, 0);
+ } else {
+ to = MathMin(target, length);
+ }
+
+ start = ToInteger(start);
+
+ if (start < 0) {
+ from = MathMax(length + start, 0);
+ } else {
+ from = MathMin(start, length);
+ }
+
+ if (%_ArgumentsLength() > 2) {
+ end = %_Arguments(2);
+ if (!IS_UNDEFINED(end)) {
+ relativeEnd = ToInteger(end);
+ }
+ }
+
+ if (relativeEnd < 0) {
+ final = MathMax(length + relativeEnd, 0);
+ } else {
+ final = MathMin(relativeEnd, length);
+ }
+
+ count = MathMin(final - from, length - to);
+
+ if (from < to && to < (from + count)) {
+ direction = -1;
+ from = from + count - 1;
+ to = to + count - 1;
+ }
+
+ while (count > 0) {
+ if (from in array) {
+ fromVal = array[from];
+ array[to] = fromVal;
+ } 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");
@@ -232,6 +298,7 @@ function HarmonyArrayExtendArrayPrototype() {
// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions($Array.prototype, DONT_ENUM, [
+ "copyWithin", ArrayCopyWithin,
"find", ArrayFind,
"findIndex", ArrayFindIndex,
"fill", ArrayFill
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-copywithin.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698