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

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: fix expectations 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..ebb5c3c3585bf60e0da5f167e4abde26e0c68d98 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -10,6 +10,58 @@
// -------------------------------------------------------------------
+// ES6 draft 03-17-15, section 22.1.3.3
+function ArrayCopyWithin(target, start, end) {
+ var array = TO_OBJECT_INLINE(this);
+ var length = ToLength(array.length);
adamk 2015/04/20 17:26:14 Please add a test with an object whose length > 2^
caitp (gmail) 2015/04/20 17:53:54 Done.
+
+ 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);
+ if (from < to && to < (from + count)) {
+ var direction = -1;
+ from = from + count - 1;
+ to = to + count - 1;
+ } else {
+ var direction = 1;
adamk 2015/04/20 17:26:14 One more style nit: move this to just before the i
caitp (gmail) 2015/04/20 17:53:54 Done.
+ }
+
+ 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");
@@ -222,6 +274,7 @@ HarmonyArrayExtendSymbolPrototype();
function HarmonyArrayExtendArrayPrototype() {
%CheckIsBootstrapping();
+ %FunctionSetLength(ArrayCopyWithin, 2);
%FunctionSetLength(ArrayFrom, 1);
// Set up non-enumerable functions on the Array object.
@@ -232,6 +285,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