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_ci
214514f5e3Sopenharmony_cifunction replace()
224514f5e3Sopenharmony_ci{
234514f5e3Sopenharmony_ci    return 12345;
244514f5e3Sopenharmony_ci}
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cifunction doDateNowOneParam(x: any): number {
274514f5e3Sopenharmony_ci    return Date.now(x);
284514f5e3Sopenharmony_ci}
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_cifunction doDateWithoutParam(): number {
314514f5e3Sopenharmony_ci    return Date.now();
324514f5e3Sopenharmony_ci}
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_cifunction printDateNowOneParam(x: any) {
354514f5e3Sopenharmony_ci    try {
364514f5e3Sopenharmony_ci        print(doDateNowOneParam(x));
374514f5e3Sopenharmony_ci    } finally {
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci}
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_cifunction printDateNowWithoutParam() {
424514f5e3Sopenharmony_ci    try {
434514f5e3Sopenharmony_ci        print(doDateWithoutParam());
444514f5e3Sopenharmony_ci    } finally {
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// Сhecking that the value changes over time
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
504514f5e3Sopenharmony_ciprint(Date.now()); //: __INT__
514514f5e3Sopenharmony_cilet delay = 20000000
524514f5e3Sopenharmony_cilet result1 = 0
534514f5e3Sopenharmony_cilet result2 = 0
544514f5e3Sopenharmony_ciwhile (result1 < delay) {
554514f5e3Sopenharmony_ci    result1++
564514f5e3Sopenharmony_ci    result2 += result1 % 2
574514f5e3Sopenharmony_ci}
584514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
594514f5e3Sopenharmony_ciprint(Date.now()); //: __INT_MORE_PREV__
604514f5e3Sopenharmony_ci// Need for disable optimization of loop
614514f5e3Sopenharmony_ciprint(result2); //: 10000000
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci// Check with parameters more 0
644514f5e3Sopenharmony_ci// NOTE: We don't check results between that launches, because they are very close
654514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
664514f5e3Sopenharmony_ciprint(Date.now(0)); //: __INT__
674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
684514f5e3Sopenharmony_ciprint(Date.now(1, 2)); //: __INT__
694514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
704514f5e3Sopenharmony_ciprint(Date.now(3, 4, 5, 6)); //: __INT__
714514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
724514f5e3Sopenharmony_ciprint(Date.now({a:10, b:20})); //: __INT__
734514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
744514f5e3Sopenharmony_ciprint(Date.now("abc")); //: __INT__
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:#*#doDateWithoutParam@builtinDateNow
774514f5e3Sopenharmony_ciprintDateNowWithoutParam(); //: __INT__
784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:#*#doDateNowOneParam@builtinDateNow
794514f5e3Sopenharmony_ciprintDateNowOneParam(2); //: __INT__
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_cilet true_now = Date.now;
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci// Check, that copy method without "this" also is inlined
844514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
854514f5e3Sopenharmony_ciprint(true_now()); //: __INT__
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printDateNowOneParam)) {
884514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
894514f5e3Sopenharmony_ci    Date.now = replace;
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ciprintDateNowWithoutParam(); //pgo: __INT__
934514f5e3Sopenharmony_ci                            //aot: [trace] Check Type: NotCallTarget1
944514f5e3Sopenharmony_ci                            //aot: 12345
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ciprintDateNowOneParam(123); //pgo: __INT__
974514f5e3Sopenharmony_ci                           //aot: [trace] Check Type: NotCallTarget1
984514f5e3Sopenharmony_ci                           //aot: 12345
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ciprintDateNowOneParam("abc"); //pgo: __INT__
1014514f5e3Sopenharmony_ci                             //aot: [trace] Check Type: NotCallTarget1
1024514f5e3Sopenharmony_ci                             //aot: 12345
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ciDate.now = true_now
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci// Check on replace object Date
1074514f5e3Sopenharmony_cilet true_date = Date
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printDateNowWithoutParam)) {
1104514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1114514f5e3Sopenharmony_ci    Date = {
1124514f5e3Sopenharmony_ci        now : function () { return "Now is now"}
1134514f5e3Sopenharmony_ci    };
1144514f5e3Sopenharmony_ci}
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ciprintDateNowWithoutParam(); //pgo: __INT__
1174514f5e3Sopenharmony_ci                            //aot: [trace] Check Type: NotCallTarget1
1184514f5e3Sopenharmony_ci                            //aot: Now is now
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ciprintDateNowOneParam(123); //pgo: __INT__
1214514f5e3Sopenharmony_ci                           //aot: [trace] Check Type: NotCallTarget1
1224514f5e3Sopenharmony_ci                           //aot: Now is now
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ciDate = true_date
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_cilet obj = {
1274514f5e3Sopenharmony_ci    valueOf: () => { return -23; }
1284514f5e3Sopenharmony_ci};
1294514f5e3Sopenharmony_cifunction Throwing() {
1304514f5e3Sopenharmony_ci    this.value = -14;
1314514f5e3Sopenharmony_ci};
1324514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() {
1334514f5e3Sopenharmony_ci    if (this.value > 0) {
1344514f5e3Sopenharmony_ci        throw new Error("already positive");
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci    return this.value;
1374514f5e3Sopenharmony_ci}
1384514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_citry {
1414514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
1424514f5e3Sopenharmony_ci    print(Date.now(throwingObj)); //: __INT__
1434514f5e3Sopenharmony_ci    throwingObj.value = 10;
1444514f5e3Sopenharmony_ci    // Value isn't use
1454514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
1464514f5e3Sopenharmony_ci    print(Date.now(throwingObj)); //: __INT__
1474514f5e3Sopenharmony_ci} catch(e) {
1484514f5e3Sopenharmony_ci    print(e);
1494514f5e3Sopenharmony_ci} finally {
1504514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
1514514f5e3Sopenharmony_ci    print(Date.now(obj)); //: __INT__
1524514f5e3Sopenharmony_ci}
153