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