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; 17 18function testNumberParseFloat(a : any, shouldThrow = 0): any 19{ 20 try { 21 if (shouldThrow == 1) { 22 throw Error("thr 1"); 23 } 24 let x = Number.parseFloat(a); 25 if (shouldThrow == 2) { 26 throw Error("thr 2"); 27 } 28 return x; 29 } catch (e) { 30 print("catch", "'" + e + "'", "in testNumberParseFloat"); 31 throw (e) 32 } finally { 33 print("exit testNumberParseFloat"); 34 } 35} 36 37function testParseFloat(a : any, shouldThrow = 0): any 38{ 39 try { 40 if (shouldThrow == 1) { 41 throw Error("thr 1"); 42 } 43 let x = parseFloat(a); 44 if (shouldThrow == 2) { 45 throw Error("thr 2"); 46 } 47 return x; 48 } catch (e) { 49 print("catch", "'" + e + "'", "in testParseFloat"); 50 throw (e) 51 } finally { 52 print("exit testParseFloat"); 53 } 54} 55 56print(Number.parseFloat === parseFloat); 57//: true 58 59function test(a : any) 60{ 61 let checks = [typeof a, a]; 62 print(checks) 63} 64 65function valueTest(testFunc : any) { 66 test(testFunc("12")); 67 test(testFunc("1.2")); 68 test(testFunc(".2")); 69 test(testFunc("-.2")); 70 test(testFunc("2.")); 71 test(testFunc("-2.")); 72 test(testFunc(" \t \n-.2")); 73 test(testFunc("-.2+2")); 74 test(testFunc("12e-2")); 75 test(testFunc("0.012E+2")); 76 test(testFunc(".")); 77 test(testFunc("1.7976931348623159e+308")); 78 test(testFunc("Infinity")); 79 test(testFunc("-1.7976931348623159e+308")); 80 test(testFunc("-Infinity")); 81 test(testFunc("900719925474099267n")); 82} 83 84valueTest(testParseFloat) 85//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 86//: exit testParseFloat 87//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 88//: number,12 89//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 90//: exit testParseFloat 91//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 92//: number,1.2 93//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 94//: exit testParseFloat 95//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 96//: number,0.2 97//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 98//: exit testParseFloat 99//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 100//: number,-0.2 101//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 102//: exit testParseFloat 103//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 104//: number,2 105//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 106//: exit testParseFloat 107//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 108//: number,-2 109//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 110//: exit testParseFloat 111//: number,-0.2 112//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 113//: exit testParseFloat 114//: number,-0.2 115//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 116//: exit testParseFloat 117//: number,0.12 118//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 119//: exit testParseFloat 120//: number,1.2 121//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 122//: exit testParseFloat 123//: number,NaN 124//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 125//: exit testParseFloat 126//: number,Infinity 127//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 128//: exit testParseFloat 129//: number,Infinity 130//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 131//: exit testParseFloat 132//: number,-Infinity 133//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 134//: exit testParseFloat 135//: number,-Infinity 136//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 137//: exit testParseFloat 138//: number,900719925474099300 139 140valueTest(testNumberParseFloat) 141//: exit testNumberParseFloat 142//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 143//: number,12 144//: exit testNumberParseFloat 145//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 146//: number,1.2 147//: exit testNumberParseFloat 148//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 149//: number,0.2 150//: exit testNumberParseFloat 151//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 152//: number,-0.2 153//: exit testNumberParseFloat 154//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 155//: number,2 156//: exit testNumberParseFloat 157//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: #*#valueTest@builtinNumberParseFloat 158//: number,-2 159//: exit testNumberParseFloat 160//: number,-0.2 161//: exit testNumberParseFloat 162//: number,-0.2 163//: exit testNumberParseFloat 164//: number,0.12 165//: exit testNumberParseFloat 166//: number,1.2 167//: exit testNumberParseFloat 168//: number,NaN 169//: exit testNumberParseFloat 170//: number,Infinity 171//: exit testNumberParseFloat 172//: number,Infinity 173//: exit testNumberParseFloat 174//: number,-Infinity 175//: exit testNumberParseFloat 176//: number,-Infinity 177//: exit testNumberParseFloat 178//: number,900719925474099300 179 180// Check deopt: 181let obj = {}; 182obj.toString = function () : string { 183 return "22"; 184} 185test(testParseFloat(obj)); 186//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 187//aot: [trace] Check Type: NotString1 188//: exit testParseFloat 189//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: func_main_0@builtinNumberParseFloat 190//: number,22 191test(testNumberParseFloat(obj)); 192//: exit testNumberParseFloat 193//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: func_main_0@builtinNumberParseFloat 194//: number,22 195 196obj = new Number(22); 197test(testParseFloat(obj)); 198//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 199//aot: [trace] Check Type: NotString1 200//: exit testParseFloat 201//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: func_main_0@builtinNumberParseFloat 202//: number,22 203test(testNumberParseFloat(obj)); 204//: exit testNumberParseFloat 205//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: func_main_0@builtinNumberParseFloat 206//: number,22 207 208// Check deopt with throw: 209try { 210 test(testParseFloat(obj, 1)); 211} catch (e) {} 212//: catch 'Error: thr 1' in testParseFloat 213//: exit testParseFloat 214try { 215 test(testNumberParseFloat(obj, 1)); 216} catch (e) {} 217//: catch 'Error: thr 1' in testNumberParseFloat 218//: exit testNumberParseFloat 219 220try { 221 test(testParseFloat(obj, 2)); 222} catch (e) {} 223//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 224//aot: [trace] Check Type: NotString1 225//: catch 'Error: thr 2' in testParseFloat 226//: exit testParseFloat 227try { 228 test(testNumberParseFloat(obj, 2)); 229} catch (e) {} 230//: catch 'Error: thr 2' in testNumberParseFloat 231//: exit testNumberParseFloat 232 233// Check replace: 234Number.parseFloat = () => { throw "CheckReplace" } 235 236test(testParseFloat("1")); 237 238//aot: [trace] aot inline builtin: Number.parseFloat, caller function name:#*#testParseFloat@builtinNumberParseFloat 239//: exit testParseFloat 240//aot: [trace] aot inline function name: #*#test@builtinNumberParseFloat caller function name: func_main_0@builtinNumberParseFloat 241//: number,1 242 243try { 244 test(testNumberParseFloat("1")); 245} catch (e) { 246 if (e != "CheckReplace") throw Error(); 247} 248//: catch 'CheckReplace' in testNumberParseFloat 249//: exit testNumberParseFloat 250