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