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 doDateGetTimeOneParam(x: any): number {
27    return time.getTime(x);
28}
29
30function printDateGetTimeOneParam(x: any) {
31    try {
32        print(doDateGetTimeOneParam(x));
33    } finally {
34    }
35}
36
37function doDateGetTimeWithoutParam(): number {
38    return time.getTime();
39}
40
41function printDateGetTimeWithoutParam() {
42    try {
43        print(doDateGetTimeWithoutParam());
44    } finally {
45    }
46}
47
48let time = new Date('July 20, 99 20:17:40 GMT+00:00')
49//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
50print(time.getTime()); //: 932501860000
51
52time = new Date(990000000000)
53//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
54print(time.getTime()); //: 990000000000
55//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
56let temp = time.getTime() + 1
57print(temp) //: 990000000001
58
59time = new Date(1990, 2, 15, 16, 17, 18);
60//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
61print(time.getTime()); //: 637489038000
62//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
63print(time.getTime(123)); //: 637489038000
64//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
65print(time.getTime("string")); //: 637489038000
66//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
67print(time.getTime(1, 2)); //: 637489038000
68//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
69print(time.getTime(1, 2, 3)); //: 637489038000
70//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
71print(time.getTime(1, 2, 3, 4)); //: 637489038000
72
73
74time = new Date('May 1, 69 8:0:0 GMT+00:00');
75//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
76print(time.getTime()); //: -21139200000
77//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
78print(time.getTime({})); //: -21139200000
79
80time = new Date('Jan 0, 2000 10:11:12 GMT+00:00');
81//aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
82print(time.getTime()); //: NaN
83
84time = new Date(50000000000)
85let true_func = time.getTime
86
87print(true_func.call(time)) //: 50000000000
88print(true_func.call(time, 1)) //: 50000000000
89print(true_func.call(time, 1, 2)) //: 50000000000
90
91if (ArkTools.isAOTCompiled(printDateGetTimeOneParam)) {
92    // Replace standard builtin after call to standard builtin was profiled
93    time.getTime = replace;
94}
95
96printDateGetTimeWithoutParam() //pgo: 50000000000
97                               //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
98                               //aot: 12345
99printDateGetTimeOneParam(123); //pgo: 50000000000
100                               //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
101                               //aot: 12345
102printDateGetTimeOneParam("abc"); //pgo: 50000000000
103                                 //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
104                                 //aot: 12345
105
106time.getTime = true_func
107
108let obj = {
109    valueOf: () => { return -23; }
110};
111function Throwing() {
112    this.value = -14;
113};
114Throwing.prototype.valueOf = function() {
115    if (this.value > 0) {
116        throw new Error("already positive");
117    }
118    return this.value;
119}
120let throwingObj = new Throwing();
121
122try {
123    //aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
124    print(time.getTime(throwingObj)); //: 50000000000
125    throwingObj.value = 10;
126    //aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
127    print(time.getTime(throwingObj)); //: 50000000000
128} catch(e) {
129    print(e);
130} finally {
131    //aot: [trace] aot inline builtin: Date.prototype.getTime, caller function name:func_main_0@builtinDateGetTime
132    print(time.getTime(obj)); //: 50000000000
133}
134
135function checkObjWithDateProto() {
136    let o = {};
137    Object.setPrototypeOf(o, Date.prototype);
138    try {
139        o.getTime();
140    } catch(e) {
141        print(e);
142    }
143}
144
145//aot: [trace] Check Type: NotCallTarget1
146//: TypeError: Not a Date Object
147checkObjWithDateProto();
148