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 16declare function print(arg:any):string; 17var x = [1,2.5,NaN,undefined,null,false,true] 18function foo(ele){ 19 print(ele) 20 if (ele != 1 || ele != true){ 21 return true 22 } 23 return false 24} 25//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 26x.forEach(ele=>{ 27 print(ele) 28 if (ele != 1 || ele != true){ 29 return true 30 } 31}) 32//: 1 33//: 2.5 34//: NaN 35//: undefined 36//: null 37//: false 38//: true 39//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 40x.forEach(foo) 41//: 1 42//: 2.5 43//: NaN 44//: undefined 45//: null 46//: false 47//: true 48 49// Check without args 50try { 51 print(x.forEach()) 52} catch(e) { 53 print(e) //: TypeError: the callbackfun is not callable. 54} 55 56//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 57print(x.forEach(() => {})) //: undefined 58//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 59print(x.forEach(x => true)) //: undefined 60//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 61print(x.forEach(x => false)) //: undefined 62//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 63print(x.forEach((x,y) => {})) //: undefined 64//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 65print(x.forEach((x,y) => x == true)) //: undefined 66//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 67print(x.forEach(x => x == true, 0)) //: undefined 68 69// Check inside try-block 70try { 71 //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach 72 print(x.forEach(x => true)) //: undefined 73} catch(e) { 74} 75 76// Replace standard builtin 77function replace(a : any) { 78 return a; 79} 80 81let newArr = [1, 2] 82let true_forEach = newArr.forEach 83newArr.forEach = replace 84 85print(newArr.forEach(x => {})); //: Cannot get source code of funtion 86newArr.forEach = true_forEach 87 88//aot: [trace] Check Type: BuiltinInstanceHClassMismatch 89print(newArr.forEach(x => true)); //: undefined 90 91function testForeachEndHoleArray() { 92 let y = [1,2,3,,,] 93 y.forEach(x=>{ 94 print(x) 95 }) 96} 97//aot: [trace] Check Type: InconsistentElementsKind 98//: 1 99//: 2 100//: 3 101testForeachEndHoleArray() 102 103function forEachCase1() { 104 print('case 1 forEach') //: case 1 forEach 105 let arr1 = [1, 2] 106 let arr2 = [1, 2] 107 arr2.garbage = function(x: any): any { 108 return undefined; 109 } 110 //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase1@builtinArrayForEach 111 print(arr1.forEach(x => x == 1)); //: undefined 112 print(arr2.forEach(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch 113 //: undefined 114} 115forEachCase1() 116 117 118function forEachCase2() { 119 print('case 2 forEach') //: case 2 forEach 120 let arr1 = [1, 2] 121 let arr2 = [1, 2] 122 arr2.forEach = function(x: any) { 123 return x 124 } 125 126 //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#forEachCase2@builtinArrayForEach 127 print(Object.getPrototypeOf(arr2) === Array.prototype) //: true 128 129 //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase2@builtinArrayForEach 130 print(arr1.forEach(x => x == 1)); //: undefined 131 print(arr2.forEach(x => x == 1)); //: Cannot get source code of funtion 132} 133forEachCase2() 134 135 136function forEachCase3() { 137 print('case 3 forEach') //: case 3 forEach 138 let marr = [1, 2] 139 let true_forEach = marr.forEach 140 let mimicArray = { 141 forEach: true_forEach, 142 } 143 144 //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase3@builtinArrayForEach 145 print(marr.forEach(x => x == 1)); //: undefined 146 Object.setPrototypeOf(marr, mimicArray) 147 148 print(marr.forEach(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch 149 //: undefined 150} 151forEachCase3() 152 153 154function forEachCase4() { 155 print('case 4 forEach') //: case 4 forEach 156 let arr1 = [1, 2] 157 let arr2 = [1, 2] 158 let notArray = { 159 forEach(x: any) { 160 return x(0) 161 } 162 } 163 Object.setPrototypeOf(arr2, notArray) 164 165 //aot: [trace] Check Type: NotStableArray1 166 print(arr1.forEach(x => x == 1)); //: undefined 167 //aot: [trace] aot inline function name: #*@4*#^1@builtinArrayForEach caller function name: #*@4*#forEach@builtinArrayForEach 168 print(arr2.forEach(x => x == 1)); //: false 169} 170forEachCase4() 171 172 173function forEachCase5() { 174 print('case 5 forEach') //: case 5 forEach 175 let arr1 = [1, 2] 176 Array.prototype.foreach = function(x: any) { 177 return x(1) 178 } 179 180 //aot: [trace] Check Type: BuiltinPrototypeHClassMismatch1 181 print(arr1.forEach(x => x == 1)); //: undefined 182} 183forEachCase5() 184