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