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 function print(arg:any):string;
174514f5e3Sopenharmony_cideclare interface ArkTools {
184514f5e3Sopenharmony_ci    isAOTCompiled(args: any): boolean;
194514f5e3Sopenharmony_ci}
204514f5e3Sopenharmony_cifunction replace(a : number, b : number)
214514f5e3Sopenharmony_ci{
224514f5e3Sopenharmony_ci    return a;
234514f5e3Sopenharmony_ci}
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cifunction doImul(x: any, y: any): number {
264514f5e3Sopenharmony_ci    return Math.imul(x, y);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cifunction printImul(x: any, y: any) {
304514f5e3Sopenharmony_ci    try {
314514f5e3Sopenharmony_ci        print(doImul(x, y));
324514f5e3Sopenharmony_ci    } finally {
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci}
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_cilet len:number = 1;
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_cilen = Math.imul(2, 3)
394514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
404514f5e3Sopenharmony_ciprint(len) //: 6
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci// Check without params
434514f5e3Sopenharmony_cilen = Math.imul();
444514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
454514f5e3Sopenharmony_ciprint(len); //: 0
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci// Check with single param
484514f5e3Sopenharmony_cilen = Math.imul(0);
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
504514f5e3Sopenharmony_ciprint(len); //: 0
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci// Check with three param
534514f5e3Sopenharmony_cilen = Math.imul(2, 4, 6);
544514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
554514f5e3Sopenharmony_ciprint(len); //: 8
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci// If an input is NaN, return 0
584514f5e3Sopenharmony_cilen = Math.imul(2, NaN)
594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
604514f5e3Sopenharmony_ciprint(len) //: 0
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci// Check with 0
634514f5e3Sopenharmony_cilen = Math.imul(3, +0.0);
644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
654514f5e3Sopenharmony_ciprint(len); //: 0
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_cilen = Math.imul(3, -0.0);
684514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
694514f5e3Sopenharmony_ciprint(len); //: 0
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci// If an input is Infinity or -Infinity, return 0
724514f5e3Sopenharmony_cilen = Math.imul(-Infinity, 5);
734514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
744514f5e3Sopenharmony_ciprint(len); //: 0
754514f5e3Sopenharmony_cilen = Math.imul(Infinity, 6);
764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
774514f5e3Sopenharmony_ciprint(len); //: 0
784514f5e3Sopenharmony_cilen = Math.imul(3, -Infinity);
794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
804514f5e3Sopenharmony_ciprint(len); //: 0
814514f5e3Sopenharmony_cilen = Math.imul(5, Infinity);
824514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
834514f5e3Sopenharmony_ciprint(len); //: 0
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci// Check int
864514f5e3Sopenharmony_cilen = Math.imul(-2, -10);
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
884514f5e3Sopenharmony_ciprint(len); //: 20
894514f5e3Sopenharmony_cilen = Math.imul(5, 10000);
904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
914514f5e3Sopenharmony_ciprint(len); //: 50000
924514f5e3Sopenharmony_cilen = Math.imul(-600, -20);
934514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
944514f5e3Sopenharmony_ciprint(len); //: 12000
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci// Check double
974514f5e3Sopenharmony_cilen = Math.imul(5.3, 2.7);
984514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
994514f5e3Sopenharmony_ciprint(len); //: 10
1004514f5e3Sopenharmony_cilen = Math.imul(-2.4, -4.7);
1014514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1024514f5e3Sopenharmony_ciprint(len); //: 8
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_cilen = Math.imul(-7.3, 6.2);
1054514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1064514f5e3Sopenharmony_ciprint(len); //: -42
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ci//big double
1094514f5e3Sopenharmony_cilen = Math.imul(2, 1.9e80);
1104514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1114514f5e3Sopenharmony_ciprint(len); //: 0
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci//small double
1144514f5e3Sopenharmony_cilen = Math.imul(2, 1.9e-80);
1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1164514f5e3Sopenharmony_ciprint(len); //: 0
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci
1194514f5e3Sopenharmony_ci// Check edge cases
1204514f5e3Sopenharmony_ciconst INT_MAX: number = 2147483647;
1214514f5e3Sopenharmony_ciconst INT_MIN: number = -INT_MAX - 1;
1224514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1234514f5e3Sopenharmony_ciprint(Math.imul(INT_MAX, 1)); //: 2147483647
1244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1254514f5e3Sopenharmony_ciprint(Math.imul(2147483648 , 2)); //: 0
1264514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1274514f5e3Sopenharmony_ciprint(Math.imul(-INT_MAX, 2)); //: 2
1284514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1294514f5e3Sopenharmony_ciprint(Math.imul(INT_MIN, 2)); //: 0
1304514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1314514f5e3Sopenharmony_ciprint(Math.imul(INT_MIN - 1, 2)); //: -2
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_cilen = Math.imul(2, "three");
1344514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:func_main_0@builtinMathImul
1354514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1364514f5e3Sopenharmony_ciprint(len); //: 0
1374514f5e3Sopenharmony_ci
1384514f5e3Sopenharmony_ci// Replace standard builtin
1394514f5e3Sopenharmony_cilet true_imul = Math.imul
1404514f5e3Sopenharmony_ciMath.imul= replace
1414514f5e3Sopenharmony_ciprint(Math.imul(2, 40)); //: 2
1424514f5e3Sopenharmony_ciMath.imul = true_imul
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_cilen = Math.imul(3, 3)
1454514f5e3Sopenharmony_ciprint(len) //: 9
1464514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1474514f5e3Sopenharmony_ciprintImul(-12, 2); //: -24
1484514f5e3Sopenharmony_ci// Call standard builtin with non-number param
1494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1504514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1514514f5e3Sopenharmony_ciprintImul("abc", 2); //: 0
1524514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1534514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1544514f5e3Sopenharmony_ciprintImul("-12", 2); //: -24
1554514f5e3Sopenharmony_ci
1564514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printImul)) {
1574514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1584514f5e3Sopenharmony_ci    Math.imul= replace
1594514f5e3Sopenharmony_ci}
1604514f5e3Sopenharmony_ciprintImul(-12, 2); //pgo: -24
1614514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1624514f5e3Sopenharmony_ci//aot: -12
1634514f5e3Sopenharmony_ciprintImul("abc", 2); //pgo: 0
1644514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1654514f5e3Sopenharmony_ci//aot: abc
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ciMath.imul = true_imul
1684514f5e3Sopenharmony_ci
1694514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1704514f5e3Sopenharmony_citry {
1714514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1724514f5e3Sopenharmony_ci    printImul(-12, 2); //: -24
1734514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1744514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber1
1754514f5e3Sopenharmony_ci    printImul("abc", 2); //: 0
1764514f5e3Sopenharmony_ci} catch (e) {
1774514f5e3Sopenharmony_ci}
1784514f5e3Sopenharmony_ci
1794514f5e3Sopenharmony_cilet obj = {};
1804514f5e3Sopenharmony_ciobj.valueOf = (() => { return -23; })
1814514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.imul, caller function name:#*#doImul@builtinMathImul
1824514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1834514f5e3Sopenharmony_ciprintImul(obj, -2); //: 46
1844514f5e3Sopenharmony_ci
1854514f5e3Sopenharmony_cifunction Throwing() {
1864514f5e3Sopenharmony_ci    this.value = -14;
1874514f5e3Sopenharmony_ci}
1884514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() {
1894514f5e3Sopenharmony_ci    if (this.value > 0) {
1904514f5e3Sopenharmony_ci        throw new Error("already positive");
1914514f5e3Sopenharmony_ci    }
1924514f5e3Sopenharmony_ci    return this.value;
1934514f5e3Sopenharmony_ci}
1944514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1954514f5e3Sopenharmony_ci
1964514f5e3Sopenharmony_citry {
1974514f5e3Sopenharmony_ci    print(Math.imul(throwingObj, 2)); //: -28
1984514f5e3Sopenharmony_ci    throwingObj.value = 10;
1994514f5e3Sopenharmony_ci    print(Math.imul(throwingObj), 2); //: Error: already positive
2004514f5e3Sopenharmony_ci} catch(e) {
2014514f5e3Sopenharmony_ci    print(e);
2024514f5e3Sopenharmony_ci} finally {
2034514f5e3Sopenharmony_ci    print(Math.imul(obj, 2)); //: -46
2044514f5e3Sopenharmony_ci}
205