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

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: Created 6 years, 5 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') | test/mjsunit/harmony/array-copywithin.js » ('J')
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 dbcb292a0876842e8d3a6d4c0d21d997cdf6d977..62974793c5f0c6689a6aa20aa18de773c6d13426 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
adamk 2015/04/17 22:34:17 Another option here is to name the argument and us
caitp (gmail) 2015/04/18 04:50:29 Done, I prefer the %FunctionSetLength() approach
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
+
+ var array = ToObject(this);
adamk 2015/04/17 22:34:16 TO_OBJECT_INLINE
caitp (gmail) 2015/04/18 04:50:29 Done.
+ var length = ToInteger(array.length);
adamk 2015/04/17 22:34:17 TO_UINT32 (hmm, we should figure out this ToLength
caitp (gmail) 2015/04/18 04:50:30 We've had ToLength() for a while --- not sure how
+ var to;
+ var from;
+ var relativeEnd = length;
adamk 2015/04/17 22:34:16 I'd move this down to its initialization to make i
caitp (gmail) 2015/04/18 04:50:29 Done.
+ var end;
+ var final;
+ var count;
+ var direction = 1;
adamk 2015/04/17 22:34:16 Same here, moving this down will make it clearer I
caitp (gmail) 2015/04/18 04:50:30 Done.
+ var fromVal;
+
+ target = ToInteger(target);
adamk 2015/04/17 22:34:17 TO_INTEGER (and similar below)
caitp (gmail) 2015/04/18 04:50:30 Done.
+ if (target < 0) {
+ to = MathMax(length + target, 0);
adamk 2015/04/17 22:34:17 I think this is now spelled $max()
caitp (gmail) 2015/04/18 04:50:29 Done.
+ } else {
+ to = MathMin(target, length);
adamk 2015/04/17 22:34:17 and this is $min()
caitp (gmail) 2015/04/18 04:50:29 Done.
+ }
+
+ 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");
@@ -130,6 +196,7 @@ function HarmonyArrayExtendArrayPrototype() {
// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
+ "copyWithin", ArrayCopyWithin,
"find", ArrayFind,
"findIndex", ArrayFindIndex,
"fill", ArrayFill
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-copywithin.js » ('j') | test/mjsunit/harmony/array-copywithin.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698