1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16{
17    class MyArray extends Array {
18        static get [Symbol.species]() {
19            return this;
20        }
21    }
22    const wannabe = new MyArray();
23    const result = wannabe.flatMap(x => [x, x]);
24    print(result instanceof MyArray);
25}
26
27var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
28var arr2 = new Array();
29function testFunction(element, index, array) {
30    if (index == 0) {
31        array.length = 6;
32    }
33    return [element, element * element];
34}
35for (let i = 0; i < 10; i++) arr2[i] = i;
36
37var result1 = arr1.flatMap(testFunction);
38print(result1);
39var result2 = arr2.flatMap(testFunction);
40print(result2);
41
42var arr3 = [0, 1, , , 4, , 6, 7, 8, 9];
43arr3.__proto__.push(0);
44arr3.__proto__.push(1);
45arr3.__proto__.push(2);
46arr3.__proto__.push(3);
47function testFunction(element, index, array) {
48    if (index == 0) {
49        array.length = 6;
50    }
51    if (index < 3)
52        return 1;
53    else
54        return [element, element * element];
55}
56var result3 = arr3.flatMap(testFunction);
57print(result3);
58arr3.__proto__.pop(0);
59arr3.__proto__.pop(1);
60arr3.__proto__.pop(2);
61arr3.__proto__.pop(3);
62
63let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
64var result4 = arr4.flatMap(testFunction);
65print(result4);
66
67let arr = [1,,,,,5];
68let res = arr.flatMap((x)=>{
69    let ret=[x,x+1];
70    ret[105] = x+2;
71    Object.defineProperty(ret,2000,{value:x+3});
72    return ret;
73})
74print(res);
75print(res.length)
76
77
78