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 if (IS_NULL_OR_UNDEFINED(thisArg)) {
30 thisArg = %GetDefaultReceiver(predicate) || thisArg;
31 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
32 thisArg = ToObject(thisArg);
35 for (var i = 0; i < length; i++) {
37 var element = array[i];
38 if (%_CallFunction(thisArg, element, i, array, predicate)) {
48 // ES6 draft 07-15-13, section 15.4.3.24
49 function ArrayFindIndex(predicate /* thisArg */) { // length == 1
50 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
52 var array = ToObject(this);
53 var length = ToInteger(array.length);
55 if (!IS_SPEC_FUNCTION(predicate)) {
56 throw MakeTypeError('called_non_callable', [predicate]);
60 if (%_ArgumentsLength() > 1) {
61 thisArg = %_Arguments(1);
64 if (IS_NULL_OR_UNDEFINED(thisArg)) {
65 thisArg = %GetDefaultReceiver(predicate) || thisArg;
66 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
67 thisArg = ToObject(thisArg);
70 for (var i = 0; i < length; i++) {
72 var element = array[i];
73 if (%_CallFunction(thisArg, element, i, array, predicate)) {
83 // ES6, draft 04-05-14, section 22.1.3.6
84 function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
85 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");
87 var array = ToObject(this);
88 var length = TO_UINT32(array.length);
93 if (%_ArgumentsLength() > 1) {
95 i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i);
96 if (%_ArgumentsLength() > 2) {
98 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
106 if (i > length) i = length;
111 if (end < 0) end = 0;
113 if (end > length) end = length;
116 if ((end - i) > 0 && ObjectIsFrozen(array)) {
117 throw MakeTypeError("array_functions_on_frozen",
118 ["Array.prototype.fill"]);
126 // -------------------------------------------------------------------
128 function HarmonyArrayExtendArrayPrototype() {
129 %CheckIsBootstrapping();
131 // Set up the non-enumerable functions on the Array prototype object.
132 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
134 "findIndex", ArrayFindIndex,
139 HarmonyArrayExtendArrayPrototype();