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 interface ArkTools {
17    isAOTCompiled(args: any): boolean;
18}
19declare function print(arg:any): string;
20
21function replace()
22{
23    return 12345;
24}
25
26function doDateNowOneParam(x: any): number {
27    return Date.now(x);
28}
29
30function doDateWithoutParam(): number {
31    return Date.now();
32}
33
34function printDateNowOneParam(x: any) {
35    try {
36        print(doDateNowOneParam(x));
37    } finally {
38    }
39}
40
41function printDateNowWithoutParam() {
42    try {
43        print(doDateWithoutParam());
44    } finally {
45    }
46}
47
48// Сhecking that the value changes over time
49//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
50print(Date.now()); //: __INT__
51let delay = 20000000
52let result1 = 0
53let result2 = 0
54while (result1 < delay) {
55    result1++
56    result2 += result1 % 2
57}
58//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
59print(Date.now()); //: __INT_MORE_PREV__
60// Need for disable optimization of loop
61print(result2); //: 10000000
62
63// Check with parameters more 0
64// NOTE: We don't check results between that launches, because they are very close
65//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
66print(Date.now(0)); //: __INT__
67//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
68print(Date.now(1, 2)); //: __INT__
69//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
70print(Date.now(3, 4, 5, 6)); //: __INT__
71//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
72print(Date.now({a:10, b:20})); //: __INT__
73//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
74print(Date.now("abc")); //: __INT__
75
76//aot: [trace] aot inline builtin: Date.now, caller function name:#*#doDateWithoutParam@builtinDateNow
77printDateNowWithoutParam(); //: __INT__
78//aot: [trace] aot inline builtin: Date.now, caller function name:#*#doDateNowOneParam@builtinDateNow
79printDateNowOneParam(2); //: __INT__
80
81let true_now = Date.now;
82
83// Check, that copy method without "this" also is inlined
84//aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
85print(true_now()); //: __INT__
86
87if (ArkTools.isAOTCompiled(printDateNowOneParam)) {
88    // Replace standard builtin after call to standard builtin was profiled
89    Date.now = replace;
90}
91
92printDateNowWithoutParam(); //pgo: __INT__
93                            //aot: [trace] Check Type: NotCallTarget1
94                            //aot: 12345
95
96printDateNowOneParam(123); //pgo: __INT__
97                           //aot: [trace] Check Type: NotCallTarget1
98                           //aot: 12345
99
100printDateNowOneParam("abc"); //pgo: __INT__
101                             //aot: [trace] Check Type: NotCallTarget1
102                             //aot: 12345
103
104Date.now = true_now
105
106// Check on replace object Date
107let true_date = Date
108
109if (ArkTools.isAOTCompiled(printDateNowWithoutParam)) {
110    // Replace standard builtin after call to standard builtin was profiled
111    Date = {
112        now : function () { return "Now is now"}
113    };
114}
115
116printDateNowWithoutParam(); //pgo: __INT__
117                            //aot: [trace] Check Type: NotCallTarget1
118                            //aot: Now is now
119
120printDateNowOneParam(123); //pgo: __INT__
121                           //aot: [trace] Check Type: NotCallTarget1
122                           //aot: Now is now
123
124Date = true_date
125
126let obj = {
127    valueOf: () => { return -23; }
128};
129function Throwing() {
130    this.value = -14;
131};
132Throwing.prototype.valueOf = function() {
133    if (this.value > 0) {
134        throw new Error("already positive");
135    }
136    return this.value;
137}
138let throwingObj = new Throwing();
139
140try {
141    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
142    print(Date.now(throwingObj)); //: __INT__
143    throwingObj.value = 10;
144    // Value isn't use
145    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
146    print(Date.now(throwingObj)); //: __INT__
147} catch(e) {
148    print(e);
149} finally {
150    //aot: [trace] aot inline builtin: Date.now, caller function name:func_main_0@builtinDateNow
151    print(Date.now(obj)); //: __INT__
152}
153