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.
5 (function(global, utils) {
9 %CheckIsBootstrapping();
11 // -------------------------------------------------------------------
14 var GlobalArray = global.Array;
15 var GlobalSymbol = global.Symbol;
22 var ObjectDefineProperty;
24 utils.Import(function(from) {
25 GetIterator = from.GetIterator;
26 GetMethod = from.GetMethod;
27 MathMax = from.MathMax;
28 MathMin = from.MathMin;
29 ObjectIsFrozen = from.ObjectIsFrozen;
30 ObjectDefineProperty = from.ObjectDefineProperty;
33 // -------------------------------------------------------------------
35 function InnerArrayCopyWithin(target, start, end, array, length) {
36 target = TO_INTEGER(target);
39 to = MathMax(length + target, 0);
41 to = MathMin(target, length);
44 start = TO_INTEGER(start);
47 from = MathMax(length + start, 0);
49 from = MathMin(start, length);
52 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
55 final = MathMax(length + end, 0);
57 final = MathMin(end, length);
60 var count = MathMin(final - from, length - to);
62 if (from < to && to < (from + count)) {
64 from = from + count - 1;
70 array[to] = array[from];
74 from = from + direction;
82 // ES6 draft 03-17-15, section 22.1.3.3
83 function ArrayCopyWithin(target, start, end) {
84 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
86 var array = TO_OBJECT_INLINE(this);
87 var length = $toLength(array.length);
89 return InnerArrayCopyWithin(target, start, end, array, length);
92 function InnerArrayFind(predicate, thisArg, array, length) {
93 if (!IS_SPEC_FUNCTION(predicate)) {
94 throw MakeTypeError(kCalledNonCallable, predicate);
97 var needs_wrapper = false;
98 if (IS_NULL(thisArg)) {
99 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED;
100 } else if (!IS_UNDEFINED(thisArg)) {
101 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
104 for (var i = 0; i < length; i++) {
105 var element = array[i];
106 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
107 if (%_CallFunction(newThisArg, element, i, array, predicate)) {
115 // ES6 draft 07-15-13, section 15.4.3.23
116 function ArrayFind(predicate, thisArg) {
117 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
119 var array = $toObject(this);
120 var length = $toInteger(array.length);
122 return InnerArrayFind(predicate, thisArg, array, length);
125 function InnerArrayFindIndex(predicate, thisArg, array, length) {
126 if (!IS_SPEC_FUNCTION(predicate)) {
127 throw MakeTypeError(kCalledNonCallable, predicate);
130 var needs_wrapper = false;
131 if (IS_NULL(thisArg)) {
132 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED;
133 } else if (!IS_UNDEFINED(thisArg)) {
134 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
137 for (var i = 0; i < length; i++) {
138 var element = array[i];
139 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
140 if (%_CallFunction(newThisArg, element, i, array, predicate)) {
148 // ES6 draft 07-15-13, section 15.4.3.24
149 function ArrayFindIndex(predicate, thisArg) {
150 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
152 var array = $toObject(this);
153 var length = $toInteger(array.length);
155 return InnerArrayFindIndex(predicate, thisArg, array, length);
158 // ES6, draft 04-05-14, section 22.1.3.6
159 function InnerArrayFill(value, start, end, array, length) {
160 var i = IS_UNDEFINED(start) ? 0 : TO_INTEGER(start);
161 var end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
167 if (i > length) i = length;
172 if (end < 0) end = 0;
174 if (end > length) end = length;
177 if ((end - i) > 0 && ObjectIsFrozen(array)) {
178 throw MakeTypeError(kArrayFunctionsOnFrozen);
186 // ES6, draft 04-05-14, section 22.1.3.6
187 function ArrayFill(value, start, end) {
188 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");
190 var array = $toObject(this);
191 var length = TO_UINT32(array.length);
193 return InnerArrayFill(value, start, end, array, length);
196 function AddArrayElement(constructor, array, i, value) {
197 if (constructor === GlobalArray) {
198 %AddElement(array, i, value);
200 ObjectDefineProperty(array, i, {
201 value: value, writable: true, configurable: true, enumerable: true
206 // ES6, draft 10-14-14, section 22.1.2.1
207 function ArrayFrom(arrayLike, mapfn, receiver) {
208 var items = $toObject(arrayLike);
209 var mapping = !IS_UNDEFINED(mapfn);
212 if (!IS_SPEC_FUNCTION(mapfn)) {
213 throw MakeTypeError(kCalledNonCallable, mapfn);
214 } else if (%IsSloppyModeFunction(mapfn)) {
215 if (IS_NULL(receiver)) {
216 receiver = UNDEFINED;
217 } else if (!IS_UNDEFINED(receiver)) {
218 receiver = TO_OBJECT_INLINE(receiver);
223 var iterable = GetMethod(items, symbolIterator);
229 if (!IS_UNDEFINED(iterable)) {
230 result = %IsConstructor(this) ? new this() : [];
232 var iterator = GetIterator(items, iterable);
236 var next = iterator.next();
238 if (!IS_OBJECT(next)) {
239 throw MakeTypeError(kIteratorResultNotAnObject, next);
247 nextValue = next.value;
249 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
251 mappedValue = nextValue;
253 AddArrayElement(this, result, k, mappedValue);
257 var len = $toLength(items.length);
258 result = %IsConstructor(this) ? new this(len) : new GlobalArray(len);
260 for (k = 0; k < len; ++k) {
261 nextValue = items[k];
263 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
265 mappedValue = nextValue;
267 AddArrayElement(this, result, k, mappedValue);
275 // ES6, draft 05-22-14, section 22.1.2.3
277 var length = %_ArgumentsLength();
278 var constructor = this;
279 // TODO: Implement IsConstructor (ES6 section 7.2.5)
280 var array = %IsConstructor(constructor) ? new constructor(length) : [];
281 for (var i = 0; i < length; i++) {
282 AddArrayElement(constructor, array, i, %_Arguments(i));
284 array.length = length;
288 // -------------------------------------------------------------------
290 %FunctionSetLength(ArrayCopyWithin, 2);
291 %FunctionSetLength(ArrayFrom, 1);
292 %FunctionSetLength(ArrayFill, 1);
293 %FunctionSetLength(ArrayFind, 1);
294 %FunctionSetLength(ArrayFindIndex, 1);
296 // Set up non-enumerable functions on the Array object.
297 utils.InstallFunctions(GlobalArray, DONT_ENUM, [
302 // Set up the non-enumerable functions on the Array prototype object.
303 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
304 "copyWithin", ArrayCopyWithin,
306 "findIndex", ArrayFindIndex,
310 // -------------------------------------------------------------------
313 utils.Export(function(to) {
314 to.ArrayFrom = ArrayFrom;
315 to.InnerArrayCopyWithin = InnerArrayCopyWithin;
316 to.InnerArrayFill = InnerArrayFill;
317 to.InnerArrayFind = InnerArrayFind;
318 to.InnerArrayFindIndex = InnerArrayFindIndex;