]>
git.saurik.com Git - apple/javascriptcore.git/blob - builtins/ArrayConstructor.js
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 function of(/* items... */)
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
;
39 function from(items
/*, mapFn, thisArg */) {
44 var mapFn
= arguments
.length
> 1 ? arguments
[1] : undefined;
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");
52 if (arguments
.length
> 2)
53 thisArg
= arguments
[2];
57 throw new @TypeError("Array.from requires an array-like object - not null or undefined");
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");
64 // TODO: Need isConstructor(thisObj) instead of typeof "function" check.
65 var result
= (typeof thisObj
=== "function") ? @Object(new thisObj()) : [];
68 var iterator
= iteratorMethod
.@call(items
);
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.
79 for (var value
of wrapper
) {
81 @putByValDirect(result
, k
, thisArg
=== undefined ? mapFn(value
, k
) : mapFn
.@call(thisArg
, value
, k
));
83 @putByValDirect(result
, k
, value
);
91 var arrayLike
= @Object(items
);
92 var arrayLikeLength
= @toLength(arrayLike
.length
);
94 // TODO: Need isConstructor(thisObj) instead of typeof "function" check.
95 var result
= (typeof thisObj
=== "function") ? @Object(new thisObj(arrayLikeLength
)) : new @Array(arrayLikeLength
);
98 while (k
< arrayLikeLength
) {
99 var value
= arrayLike
[k
];
101 @putByValDirect(result
, k
, thisArg
=== undefined ? mapFn(value
, k
) : mapFn
.@call(thisArg
, value
, k
));
103 @putByValDirect(result
, k
, value
);
107 result
.length
= arrayLikeLength
;