Revision: 21074
Author: [email protected]
Date: Wed Apr 30 08:28:29 2014 UTC
Log: ES6: Add support for Array.prototype.fill()
BUG=v8:3273
LOG=Y
[email protected]
Review URL: https://codereview.chromium.org/240873002
Patch from Adrian Perez <[email protected]>.
http://code.google.com/p/v8/source/detail?r=21074
Added:
/branches/bleeding_edge/test/mjsunit/harmony/array-fill.js
Modified:
/branches/bleeding_edge/src/harmony-array.js
=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/mjsunit/harmony/array-fill.js Wed Apr 30
08:28:29 2014 UTC
@@ -0,0 +1,32 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-arrays
+
+assertEquals(1, Array.prototype.find.length);
+
+assertArrayEquals([].fill(8), []);
+assertArrayEquals([0, 0, 0, 0, 0].fill(), [undefined, undefined,
undefined, undefined, undefined]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8), [8, 8, 8, 8, 8]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, 1), [0, 8, 8, 8, 8]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, 10), [0, 0, 0, 0, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, -5), [8, 8, 8, 8, 8]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, 1, 4), [0, 8, 8, 8, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, 1, -1), [0, 8, 8, 8, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, 1, 42), [0, 8, 8, 8, 8]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, -3, 42), [0, 0, 8, 8, 8]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, -3, 4), [0, 0, 8, 8, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, -2, -1), [0, 0, 0, 8, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, -1, -3), [0, 0, 0, 0, 0]);
+assertArrayEquals([0, 0, 0, 0, 0].fill(8, undefined, 4), [8, 8, 8, 8, 0]);
+assertArrayEquals([ , , , , 0].fill(8, 1, 3), [, 8, 8, , 0]);
+
+// If the range if empty, the array is not actually modified and
+// should not throw, even when applied to a frozen object.
+assertArrayEquals(Object.freeze([1, 2, 3]).fill(0, 0, 0), [1, 2, 3]);
+
+// Test exceptions
+assertThrows('Object.freeze([0]).fill()', TypeError);
+assertThrows('Array.prototype.fill.call(null)', TypeError);
+assertThrows('Array.prototype.fill.call(undefined)', TypeError);
=======================================
--- /branches/bleeding_edge/src/harmony-array.js Tue Apr 29 06:42:26 2014
UTC
+++ /branches/bleeding_edge/src/harmony-array.js Wed Apr 30 08:28:29 2014
UTC
@@ -79,6 +79,49 @@
return -1;
}
+
+// ES6, draft 04-05-14, section 22.1.3.6
+function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");
+
+ var array = ToObject(this);
+ var length = TO_UINT32(array.length);
+
+ var i = 0;
+ var end = length;
+
+ if (%_ArgumentsLength() > 1) {
+ i = %_Arguments(1);
+ i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i);
+ if (%_ArgumentsLength() > 2) {
+ end = %_Arguments(2);
+ end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
+ }
+ }
+
+ if (i < 0) {
+ i += length;
+ if (i < 0) i = 0;
+ } else {
+ if (i > length) i = length;
+ }
+
+ if (end < 0) {
+ end += length;
+ if (end < 0) end = 0;
+ } else {
+ if (end > length) end = length;
+ }
+
+ if ((end - i) > 0 && ObjectIsFrozen(array)) {
+ throw MakeTypeError("array_functions_on_frozen",
+ ["Array.prototype.fill"]);
+ }
+
+ for (; i < end; i++)
+ array[i] = value;
+ return array;
+}
// -------------------------------------------------------------------
@@ -88,7 +131,8 @@
// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
"find", ArrayFind,
- "findIndex", ArrayFindIndex
+ "findIndex", ArrayFindIndex,
+ "fill", ArrayFill
));
}
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.