]> git.saurik.com Git - apple/javascriptcore.git/blob - builtins/ArrayConstructor.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / builtins / ArrayConstructor.js
1 /*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 function of(/* items... */)
27 {
28 "use strict";
29
30 var length = arguments.length;
31 // TODO: Need isConstructor(this) instead of typeof "function" check.
32 var array = typeof this === 'function' ? new this(length) : new @Array(length);
33 for (var k = 0; k < length; ++k)
34 @putByValDirect(array, k, arguments[k]);
35 array.length = length;
36 return array;
37 }
38
39 function from(items /*, mapFn, thisArg */) {
40 "use strict";
41
42 var thisObj = this;
43
44 var mapFn = arguments.length > 1 ? arguments[1] : undefined;
45
46 var thisArg;
47
48 if (mapFn !== undefined) {
49 if (typeof mapFn !== "function")
50 throw new @TypeError("Array.from requires that the second argument, when provided, be a function");
51
52 if (arguments.length > 2)
53 thisArg = arguments[2];
54 }
55
56 if (items == null)
57 throw new @TypeError("Array.from requires an array-like object - not null or undefined");
58
59 var iteratorMethod = items[@symbolIterator];
60 if (iteratorMethod != null) {
61 if (typeof iteratorMethod !== "function")
62 throw new @TypeError("Array.from requires that the property of the first argument, items[Symbol.iterator], when exists, be a function");
63
64 // TODO: Need isConstructor(thisObj) instead of typeof "function" check.
65 var result = (typeof thisObj === "function") ? @Object(new thisObj()) : [];
66
67 var k = 0;
68 var iterator = iteratorMethod.@call(items);
69
70 // Since for-of loop once more looks up the @@iterator property of a given iterable,
71 // it could be observable if the user defines a getter for @@iterator.
72 // To avoid this situation, we define a wrapper object that @@iterator just returns a given iterator.
73 var wrapper = {
74 [@symbolIterator]() {
75 return iterator;
76 }
77 };
78
79 for (var value of wrapper) {
80 if (mapFn)
81 @putByValDirect(result, k, thisArg === undefined ? mapFn(value, k) : mapFn.@call(thisArg, value, k));
82 else
83 @putByValDirect(result, k, value);
84 k += 1;
85 }
86
87 result.length = k;
88 return result;
89 }
90
91 var arrayLike = @Object(items);
92 var arrayLikeLength = @toLength(arrayLike.length);
93
94 // TODO: Need isConstructor(thisObj) instead of typeof "function" check.
95 var result = (typeof thisObj === "function") ? @Object(new thisObj(arrayLikeLength)) : new @Array(arrayLikeLength);
96
97 var k = 0;
98 while (k < arrayLikeLength) {
99 var value = arrayLike[k];
100 if (mapFn)
101 @putByValDirect(result, k, thisArg === undefined ? mapFn(value, k) : mapFn.@call(thisArg, value, k));
102 else
103 @putByValDirect(result, k, value);
104 k += 1;
105 }
106
107 result.length = arrayLikeLength;
108 return result;
109 }