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