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 10-14-14, section 22.1.2.1
131 function ArrayFrom(arrayLike, mapfn, receiver) {
132 var items = ToObject(arrayLike);
133 var mapping = !IS_UNDEFINED(mapfn);
136 if (!IS_SPEC_FUNCTION(mapfn)) {
137 throw MakeTypeError('called_non_callable', [ mapfn ]);
138 } else if (IS_NULL_OR_UNDEFINED(receiver)) {
139 receiver = %GetDefaultReceiver(mapfn) || receiver;
140 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
141 receiver = ToObject(receiver);
145 var iterable = GetMethod(items, symbolIterator);
151 if (!IS_UNDEFINED(iterable)) {
152 result = %IsConstructor(this) ? new this() : [];
154 var iterator = GetIterator(items, iterable);
158 var next = iterator.next();
160 if (!IS_OBJECT(next)) {
161 throw MakeTypeError("iterator_result_not_an_object", [next]);
169 nextValue = next.value;
171 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
173 mappedValue = nextValue;
175 %AddElement(result, k++, mappedValue, NONE);
178 var len = ToLength(items.length);
179 result = %IsConstructor(this) ? new this(len) : new $Array(len);
181 for (k = 0; k < len; ++k) {
182 nextValue = items[k];
184 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
186 mappedValue = nextValue;
188 %AddElement(result, k, mappedValue, NONE);
196 // ES6, draft 05-22-14, section 22.1.2.3
198 var length = %_ArgumentsLength();
199 var constructor = this;
200 // TODO: Implement IsConstructor (ES6 section 7.2.5)
201 var array = %IsConstructor(constructor) ? new constructor(length) : [];
202 for (var i = 0; i < length; i++) {
203 %AddElement(array, i, %_Arguments(i), NONE);
205 array.length = length;
209 // -------------------------------------------------------------------
211 function HarmonyArrayExtendSymbolPrototype() {
212 %CheckIsBootstrapping();
214 InstallConstants(global.Symbol, $Array(
215 // TODO(dslomov, caitp): Move to symbol.js when shipping
216 "isConcatSpreadable", symbolIsConcatSpreadable
220 HarmonyArrayExtendSymbolPrototype();
222 function HarmonyArrayExtendArrayPrototype() {
223 %CheckIsBootstrapping();
225 %FunctionSetLength(ArrayFrom, 1);
227 // Set up non-enumerable functions on the Array object.
228 InstallFunctions($Array, DONT_ENUM, $Array(
233 // Set up the non-enumerable functions on the Array prototype object.
234 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
236 "findIndex", ArrayFindIndex,
241 HarmonyArrayExtendArrayPrototype();