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 16class CustomArray extends Array {} 17 18// define Symbol.species, then the ArraySpeciesCreate for Array will use CustomArray 19Object.defineProperty(Array, Symbol.species, {value: CustomArray}) 20 21function testSpeciesEffect() { 22 // test unstable array, fastpath in ir will be fixed later. 23 let custom = new Array(2000); 24 25 print("Testing methods affected by symbol.species:"); 26 27 // Array.prototype.concat 28 let result = custom.concat([4, 5]); 29 print("concat:", result instanceof CustomArray); // true 30 31 // Array.prototype.map 32 result = custom.map(x => x * 2); 33 print("map:", result instanceof CustomArray); // true 34 35 // Array.prototype.filter 36 result = custom.filter(x => x > 1); 37 print("filter:", result instanceof CustomArray); // true 38 39 // Array.prototype.slice 40 result = custom.slice(1); 41 print("slice:", result instanceof CustomArray); // true 42 43 // Array.prototype.flatMap 44 result = custom.flatMap(x => [x, x * 2]); 45 print("flatMap:", result instanceof CustomArray); // true 46 47 // Array.prototype.flat 48 result = custom.flat(); 49 print("flat:", result instanceof CustomArray); // true 50 51 // Array.prototype.splice 52 result = custom.splice(1,2,3); 53 print("splice:", result instanceof CustomArray); // true 54 55 // Array.prototype.toReversed 56 result = custom.toReversed(); 57 print("toReversed:", result instanceof Array); // true 58 59 // Array.prototype.toSorted 60 result = custom.toSorted(); 61 print("toSorted:", result instanceof Array); // true 62 63 // Array.prototype.toSpliced 64 result = custom.toSpliced(0,1); 65 print("toSpliced:", result instanceof Array); // true 66 67 // Array.prototype.toSpliced 68 result = custom.with(0,1); 69 print("with:", result instanceof Array); // true 70} 71 72testSpeciesEffect() 73