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;
20function replace(a : number)
21{
22    return a;
23}
24
25function doKeys(x : any) {
26    return myMap.keys(x);
27}
28
29function printKeys(x : any) {
30    try {
31        print(doKeys(x));
32    } finally {
33    }
34}
35
36let myMap = new Map([[0, 0], [0.0, 5], [-1, 1], [2.5, -2.5], [NaN, Infinity], [2000, -0.0], [56, "oops"], ["xyz", "12345"]]);
37
38// Check without params
39//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
40print(myMap.keys()); //: [object Map Iterator]
41
42// Check with single param
43//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
44print(myMap.keys(0).next().value); //: 0
45
46// Check with 2 params
47//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
48print(myMap.keys(0, 0).next().value); //: 0
49
50// Check with 3 params
51//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
52print(myMap.keys(-1, 10.2, 15).next().value); //: 0
53
54// Check own methods
55//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
56print(myMap.keys().throw); //: function throw() { [native code] }
57//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
58print(myMap.keys().return); //: function return() { [native code] }
59
60// Check using in loop
61//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
62for (let key of myMap.keys()) {
63    print(key);
64}
65//: 0
66//: -1
67//: 2.5
68//: NaN
69//: 2000
70//: 56
71//: xyz
72
73// Replace standard builtin
74let true_keys = myMap.keys
75myMap.keys = replace
76
77// no deopt
78print(myMap.keys(2.5)); //: 2.5
79myMap.keys = true_keys
80
81if (ArkTools.isAOTCompiled(printKeys)) {
82    // Replace standard builtin after call to standard builtin was profiled
83    myMap.keys = replace
84}
85printKeys(2.5); //pgo: [object Map Iterator]
86//aot: [trace] Check Type: NotCallTarget1
87//aot: 2.5
88
89printKeys("abc"); //pgo: [object Map Iterator]
90//aot: [trace] Check Type: NotCallTarget1
91//aot: abc
92
93myMap.keys = true_keys
94
95// Check IR correctness inside try-block
96try {
97    //aot: [trace] aot inline builtin: Map.keys, caller function name:#*#doKeys@builtinMapKeys
98    printKeys(2.5); //: [object Map Iterator]
99    //aot: [trace] aot inline builtin: Map.keys, caller function name:#*#doKeys@builtinMapKeys
100    printKeys("abc"); //: [object Map Iterator]
101} catch (e) {
102}
103
104// Check using in a loop
105//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
106let iter1 = myMap.keys();
107for (let key of iter1) {
108    print(key);
109}
110//: 0
111//: -1
112//: 2.5
113//: NaN
114//: 2000
115//: 56
116//: xyz
117
118// Check reusing possibility
119for (let key of iter1) {
120    print(key);
121} // <nothing>
122
123// Check using out of boundaries
124print(iter1.next().value); //: undefined
125
126// Check using after deleting
127//aot: [trace] aot inline builtin: Map.keys, caller function name:func_main_0@builtinMapKeys
128let iter2 = myMap.keys();
129//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapKeys
130myMap.delete(NaN);
131myMap.set("xyz", -100);
132for (let key of iter2) {
133    print(key);
134}
135//: 0
136//: -1
137//: 2.5
138//: 2000
139//: 56
140//: xyz
141