• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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// Test map Symbol.iterator change.
17{
18    let mapPrototype = Object.getPrototypeOf(new Map());
19    let iteratorSymbol = Symbol.iterator;
20
21    function customMapIterator() {
22        let mapEntries = this.entries();
23        let entryIndex = 0;
24
25        return {
26            next: function () {
27                if (entryIndex < 2) {
28                    let entry = mapEntries.next();
29                    entryIndex++;
30                    // 这里只返回键,与标准的返回键值对不同
31                    return { done: false, value: entry.value[0] };
32                }
33                return { done: true, value: undefined };
34            },
35            customProperty: "This is a custom property for Map iterator"
36        };
37    }
38
39    mapPrototype[iteratorSymbol] = customMapIterator;
40    let myMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
41    print("Testing methods affected by map Symbol.iterator:");
42    for (let item of myMap) {
43        print(item);
44    }
45}
46
47// Test set Symbol.iterator change.
48{
49    let setPrototype = Object.getPrototypeOf(new Set());
50    let iteratorSymbol = Symbol.iterator;
51
52    function customSetIterator() {
53        let setValues = this.values();
54        let valueIndex = 0;
55
56        return {
57            next: function () {
58                if (valueIndex < 2) {
59                    print(valueIndex);
60                    let value = setValues.next();
61                    valueIndex++;
62                    // 将元素重复两次返回,与标准不同
63                    return { done: false, value: [value.value, value.value] };
64                }
65                return { done: true, value: undefined };
66            },
67            customProperty: "This is a custom property for Set iterator"
68        };
69    }
70
71    setPrototype[iteratorSymbol] = customSetIterator;
72    let mySet = new Set(['value1', 'value2']);
73
74    print("Testing methods affected by set Symbol.iterator:");
75    for (let item of mySet) {
76        print(item);
77    }
78}
79
80// Test array Symbol.iterator change.
81{
82    let arrayPrototype = Object.getPrototypeOf([]);
83    let iteratorSymbol = Symbol.iterator;
84
85    function customArrayIterator() {
86        let array = this;
87        let index = 0;
88
89        return {
90            next: function () {
91                if (index < array.length) {
92                    // 倒序返回数组元素,与标准顺序不同
93                    let value = array[array.length - 1 - index];
94                    index++;
95                    return { done: false, value: value };
96                }
97                return { done: true, value: undefined };
98            },
99            customProperty: "This is a custom property for Array iterator"
100        };
101    }
102
103    arrayPrototype[iteratorSymbol] = customArrayIterator;
104    let myArray = ['value1', 'value2'];
105
106    print("Testing methods affected by array Symbol.iterator:");
107    for (let item of myArray) {
108        print(item);
109    }
110}
111
112// -----------------------Test inheritance---------------------------------
113
114// Test map Symbol.iterator change using inheritance (new implementation).
115{
116    class CustomMapInherit extends Map {
117        constructor(initEntries) {
118            super(initEntries);
119        }
120    };
121    function customMapIterator() {
122        let mapEntries = this.entries();
123        let entryIndex = 0;
124
125        return {
126            next: function () {
127                if (entryIndex < 2) {
128                    print("CustomMapInherit");
129                    let entry = mapEntries.next();
130                    entryIndex++;
131                    // 这里只返回键,与标准的返回键值对不同
132                    return { done: false, value: entry.value[0] };
133                }
134                return { done: true, value: undefined };
135            },
136            customProperty: "This is a custom property for CustomMapInherit iterator"
137        };
138    }
139    let CustomMapPrototype = Object.getPrototypeOf(new CustomMapInherit());
140    let iteratorSymbol = Symbol.iterator;
141    CustomMapPrototype[iteratorSymbol] = customMapIterator;
142    print("Testing methods affected by map Symbol.iterator (inherited way):");
143    let customMap = new CustomMapInherit([['key1', 'value1'], ['key2', 'value2']]);
144    for (let item of customMap) {
145        print(item);
146    }
147}
148
149// Test set Symbol.iterator change using inheritance (new implementation).
150{
151    class CustomSetInherit extends Set {
152        constructor(initValues) {
153            super(initValues);
154        }
155    }
156    function customSetIterator() {
157        let setValues = this.values();
158        let valueIndex = 0;
159
160        return {
161            next: function () {
162                if (valueIndex < 2) {
163                    print("CustomSetInherit");
164                    let value = setValues.next();
165                    valueIndex++;
166                    // 将元素重复两次返回,与标准不同(这里可按需调整返回逻辑,保持或修改重复行为等)
167                    return { done: false, value: [value.value, value.value] };
168                }
169                return { done: true, value: undefined };
170            },
171            customProperty: "This is a custom property for CustomSetInherit iterator"
172        };
173    }
174    let CustomSetPrototype = Object.getPrototypeOf(new CustomSetInherit());
175    let iteratorSymbol = Symbol.iterator;
176    CustomSetPrototype[iteratorSymbol] = customSetIterator;
177    print("Testing methods affected by custom Set Symbol.iterator (inherited way):");
178    let customSet = new CustomSetInherit(['value1', 'value2']);
179    for (const item of customSet) {
180        print(item);
181    }
182}
183
184// Test array Symbol.iterator change using inheritance (new implementation).
185{
186    class CustomArrayInherit extends Array {
187        constructor(...args) {
188            super(...args);
189        }
190    }
191    function customArrayIterator() {
192        let array = this;
193        let index = 0;
194
195        return {
196            next: function () {
197                if (index < array.length) {
198                    print("CustomArrayInherit");
199                    let value = array[array.length - 1 - index];
200                    index++;
201                    return { done: false, value: value };
202                }
203                return { done: true, value: undefined };
204            },
205            customProperty: "This is a custom property for CustomArrayInherit iterator"
206        };
207    }
208    let CustomArrayPrototype = Object.getPrototypeOf(new CustomArrayInherit());
209    let iteratorSymbol = Symbol.iterator;
210    CustomArrayPrototype[iteratorSymbol] = customArrayIterator;
211    print("Testing methods affected by custom Array Symbol.iterator (inherited way):");
212    let customArray = new CustomArrayInherit('value1', 'value2');
213    for (const item of customArray) {
214        print(item);
215    }
216}
217
218// This case aims to test the logic which check the undefined result of GetIterator.
219{
220    class c2 extends Object {}
221    Array.prototype[Symbol.iterator] = function () {};
222    try {
223        let myC2 = new c2();
224    } catch (error) {
225        print(error);
226    }
227}