14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_cideclare interface ArkTools { 174514f5e3Sopenharmony_ci isAOTCompiled(args: any): boolean; 184514f5e3Sopenharmony_ci} 194514f5e3Sopenharmony_cideclare function print(arg:any):string; 204514f5e3Sopenharmony_cifunction replace(a : number) 214514f5e3Sopenharmony_ci{ 224514f5e3Sopenharmony_ci return a; 234514f5e3Sopenharmony_ci} 244514f5e3Sopenharmony_ci 254514f5e3Sopenharmony_cifunction doAbs(x: any): number { 264514f5e3Sopenharmony_ci return Math.abs(x); 274514f5e3Sopenharmony_ci} 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_cifunction printAbs(x: any) { 304514f5e3Sopenharmony_ci try { 314514f5e3Sopenharmony_ci print(doAbs(x)); 324514f5e3Sopenharmony_ci } finally { 334514f5e3Sopenharmony_ci } 344514f5e3Sopenharmony_ci} 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_cifunction printAbs2() { 374514f5e3Sopenharmony_ci try { 384514f5e3Sopenharmony_ci const INT_MAX: number = 2147483647; 394514f5e3Sopenharmony_ci const INT_MIN: number = -INT_MAX - 1; 404514f5e3Sopenharmony_ci print(Math.abs(INT_MIN)); 414514f5e3Sopenharmony_ci } finally { 424514f5e3Sopenharmony_ci } 434514f5e3Sopenharmony_ci} 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci// Check without params 464514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 474514f5e3Sopenharmony_ciprint(Math.abs()); //: NaN 484514f5e3Sopenharmony_ci 494514f5e3Sopenharmony_ci// Check with single int param 504514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 514514f5e3Sopenharmony_ciprint(Math.abs(0)); //: 0 524514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 534514f5e3Sopenharmony_ciprint(Math.abs(3)); //: 3 544514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 554514f5e3Sopenharmony_ciprint(Math.abs(-5)); //: 5 564514f5e3Sopenharmony_ci 574514f5e3Sopenharmony_ci// Check with single float param 584514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 594514f5e3Sopenharmony_ciprint(Math.abs(-1.5)); //: 1.5 604514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 614514f5e3Sopenharmony_ciprint(Math.abs(Math.PI)); //: 3.141592653589793 624514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 634514f5e3Sopenharmony_ciprint(Math.abs(-Math.PI)); //: 3.141592653589793 644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 654514f5e3Sopenharmony_ciprint(Math.abs(-1.9e80)); //: 1.9e+80 664514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 674514f5e3Sopenharmony_ciprint(Math.abs(1.9e80)); //: 1.9e+80 684514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 694514f5e3Sopenharmony_ciprint(Math.abs(-1.9e-80)); //: 1.9e-80 704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 714514f5e3Sopenharmony_ciprint(Math.abs(1.9e-80)); //: 1.9e-80 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ci// Check with special float params 754514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 764514f5e3Sopenharmony_ciprint(Math.abs(Infinity)); //: Infinity 774514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 784514f5e3Sopenharmony_ciprint(Math.abs(-Infinity)); //: Infinity 794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 804514f5e3Sopenharmony_ciprint(Math.abs(NaN)); //: NaN 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 834514f5e3Sopenharmony_ciprint(1 / Math.abs(-0)); //: Infinity 844514f5e3Sopenharmony_ci 854514f5e3Sopenharmony_ci// Check with 2 params 864514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 874514f5e3Sopenharmony_ciprint(Math.abs(3, 0)); //: 3 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci// Check with 3 params 904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 914514f5e3Sopenharmony_ciprint(Math.abs(-3, 0, 0)); //: 3 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci// Check with 4 params 944514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 954514f5e3Sopenharmony_ciprint(Math.abs(4, 0, 0, 0)); //: 4 964514f5e3Sopenharmony_ci 974514f5e3Sopenharmony_ci// Check with 5 params 984514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 994514f5e3Sopenharmony_ciprint(Math.abs(-4, 0, 0, 0, 0)); //: 4 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci// Replace standard builtin 1024514f5e3Sopenharmony_cilet true_abs = Math.abs 1034514f5e3Sopenharmony_ciMath.abs = replace 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_ci// no deopt 1064514f5e3Sopenharmony_ciprint(Math.abs(-1.001)); //: -1.001 1074514f5e3Sopenharmony_ciMath.abs = true_abs 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci// Check edge cases 1104514f5e3Sopenharmony_ciconst INT_MAX: number = 2147483647; 1114514f5e3Sopenharmony_ciconst INT_MIN: number = -INT_MAX - 1; 1124514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1134514f5e3Sopenharmony_ciprintAbs(INT_MAX); //: 2147483647 1144514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1154514f5e3Sopenharmony_ciprintAbs(2147483648); //: 2147483648 1164514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1174514f5e3Sopenharmony_ciprintAbs(-INT_MAX); //: 2147483647 1184514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#printAbs2@builtinMathAbs 1194514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotInt3 1204514f5e3Sopenharmony_ciprintAbs2(); //: 2147483648 1214514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1224514f5e3Sopenharmony_ciprintAbs(INT_MIN - 1); //: 2147483649 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1254514f5e3Sopenharmony_ciprintAbs(-12); //: 12 1264514f5e3Sopenharmony_ci// Call standard builtin with non-number param 1274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1284514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2 1294514f5e3Sopenharmony_ciprintAbs("abc"); //: NaN 1304514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1314514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2 1324514f5e3Sopenharmony_ciprintAbs("-12"); //: 12 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printAbs)) { 1354514f5e3Sopenharmony_ci // Replace standard builtin after call to standard builtin was profiled 1364514f5e3Sopenharmony_ci Math.abs = replace 1374514f5e3Sopenharmony_ci} 1384514f5e3Sopenharmony_ciprintAbs(-12); //pgo: 12 1394514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1 1404514f5e3Sopenharmony_ci//aot: -12 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ciprintAbs("abc"); //pgo: NaN 1434514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1 1444514f5e3Sopenharmony_ci//aot: abc 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ciMath.abs = true_abs 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ci// Check IR correctness inside try-block 1494514f5e3Sopenharmony_citry { 1504514f5e3Sopenharmony_ci //aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1514514f5e3Sopenharmony_ci printAbs(-12); //: 12 1524514f5e3Sopenharmony_ci //aot: [trace] aot inline builtin: Math.abs, caller function name:#*#doAbs@builtinMathAbs 1534514f5e3Sopenharmony_ci //aot: [trace] Check Type: NotNumber2 1544514f5e3Sopenharmony_ci printAbs("abc"); //: NaN 1554514f5e3Sopenharmony_ci} catch (e) { 1564514f5e3Sopenharmony_ci} 1574514f5e3Sopenharmony_ci 1584514f5e3Sopenharmony_cilet obj = { 1594514f5e3Sopenharmony_ci valueOf: () => { return -23; } 1604514f5e3Sopenharmony_ci}; 1614514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.abs, caller function name:func_main_0@builtinMathAbs 1624514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2 1634514f5e3Sopenharmony_ciprint(Math.abs(obj)); //: 23 1644514f5e3Sopenharmony_ci 1654514f5e3Sopenharmony_cifunction Throwing() { 1664514f5e3Sopenharmony_ci this.value = -14; 1674514f5e3Sopenharmony_ci} 1684514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() { 1694514f5e3Sopenharmony_ci if (this.value > 0) { 1704514f5e3Sopenharmony_ci throw new Error("already positive"); 1714514f5e3Sopenharmony_ci } 1724514f5e3Sopenharmony_ci return this.value; 1734514f5e3Sopenharmony_ci} 1744514f5e3Sopenharmony_cilet throwingObj = new Throwing(); 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_citry { 1774514f5e3Sopenharmony_ci print(Math.abs(throwingObj)); //: 14 1784514f5e3Sopenharmony_ci throwingObj.value = 10; 1794514f5e3Sopenharmony_ci print(Math.abs(throwingObj)); //: Error: already positive 1804514f5e3Sopenharmony_ci} catch(e) { 1814514f5e3Sopenharmony_ci print(e); 1824514f5e3Sopenharmony_ci} finally { 1834514f5e3Sopenharmony_ci print(Math.abs(obj)); //: 23 1844514f5e3Sopenharmony_ci}