1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 // This file relies on the fact that the following declaration has been made
9 // var $Array = global.Array;
11 // -------------------------------------------------------------------
13 // ES6 draft 07-15-13, section 15.4.3.23
14 function ArrayFind(predicate /* thisArg */) { // length == 1
15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
17 var array = ToObject(this);
18 var length = ToInteger(array.length);
20 if (!IS_SPEC_FUNCTION(predicate)) {
21 throw MakeTypeError('called_non_callable', [predicate]);
25 if (%_ArgumentsLength() > 1) {
26 thisArg = %_Arguments(1);
29 var needs_wrapper = false;
30 if (IS_NULL_OR_UNDEFINED(thisArg)) {
31 thisArg = %GetDefaultReceiver(predicate) || thisArg;
33 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
36 for (var i = 0; i < length; i++) {
38 var element = array[i];
39 var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg;
40 if (%_CallFunction(newThisArg, element, i, array, predicate)) {
50 // ES6 draft 07-15-13, section 15.4.3.24
51 function ArrayFindIndex(predicate /* thisArg */) { // length == 1
52 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
54 var array = ToObject(this);
55 var length = ToInteger(array.length);
57 if (!IS_SPEC_FUNCTION(predicate)) {
58 throw MakeTypeError('called_non_callable', [predicate]);
62 if (%_ArgumentsLength() > 1) {
63 thisArg = %_Arguments(1);
66 var needs_wrapper = false;
67 if (IS_NULL_OR_UNDEFINED(thisArg)) {
68 thisArg = %GetDefaultReceiver(predicate) || thisArg;
70 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
73 for (var i = 0; i < length; i++) {
75 var element = array[i];
76 var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg;
77 if (%_CallFunction(newThisArg, element, i, array, predicate)) {
87 // ES6, draft 04-05-14, section 22.1.3.6
88 function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
89 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");
91 var array = ToObject(this);
92 var length = TO_UINT32(array.length);
97 if (%_ArgumentsLength() > 1) {
99 i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i);
100 if (%_ArgumentsLength() > 2) {
101 end = %_Arguments(2);
102 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
110 if (i > length) i = length;
115 if (end < 0) end = 0;
117 if (end > length) end = length;
120 if ((end - i) > 0 && ObjectIsFrozen(array)) {
121 throw MakeTypeError("array_functions_on_frozen",
122 ["Array.prototype.fill"]);
130 // ES6, draft 05-22-14, section 22.1.2.3
132 var length = %_ArgumentsLength();
133 var constructor = this;
134 // TODO: Implement IsConstructor (ES6 section 7.2.5)
135 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : [];
136 for (var i = 0; i < length; i++) {
137 %AddElement(array, i, %_Arguments(i), NONE);
139 array.length = length;
143 // -------------------------------------------------------------------
145 function HarmonyArrayExtendArrayPrototype() {
146 %CheckIsBootstrapping();
148 // Set up non-enumerable functions on the Array object.
149 InstallFunctions($Array, DONT_ENUM, $Array(
153 // Set up the non-enumerable functions on the Array prototype object.
154 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
156 "findIndex", ArrayFindIndex,
161 HarmonyArrayExtendArrayPrototype();