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 |