• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 doDelete(x: any): any {
26    return myMap.delete(x);
27}
28
29function printDelete(x: any) {
30    try {
31        print(doDelete(x));
32    } finally {
33    }
34}
35
36function printDelete2(x: any, y: any) {
37    try {
38        print(x.delete(y));
39    } finally {
40    }
41}
42
43let myMap = new Map([[0, 0], [0.0, 5], [-1, 1], [2.5, -2.5], [NaN, Infinity], [2000, -0.0], [56, "oops"], ["xyz", "12345"], [-3, 1]]);
44
45// Check without params
46//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
47print(myMap.delete()); //: false
48
49// Check with seting element undefined
50//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
51myMap.set(undefined, 42);
52//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
53print(myMap.delete()); //: true
54
55// Check with single param
56//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
57print(myMap.delete(0)); //: true
58//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
59print(myMap.delete(3)); //: false
60//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
61print(myMap.delete(NaN)); //: true
62
63// Check with 2 params
64//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
65print(myMap.delete(2000, 0)); //: true
66
67// Check with 3 params
68//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
69print(myMap.delete(-51, 10.2, 15)); //: false
70
71// Check after inserting elements
72//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
73myMap.set(2000, 1e-98);
74//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
75print(myMap.delete(2000)); //: true
76
77// Replace standard builtin
78let true_delete = myMap.delete
79myMap.delete = replace
80// no deopt
81print(myMap.delete(2.5)); //: 2.5
82
83myMap.delete = true_delete
84//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#doDelete@builtinMapDelete
85printDelete(-1); //: true
86
87// Call standard builtin with non-number param
88//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
89print(myMap.delete("abc")); //: false
90//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
91print(myMap.delete("2.5")); //: false
92//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
93print(myMap.delete("xyz")); //: true
94
95if (ArkTools.isAOTCompiled(printDelete)) {
96    // Replace standard builtin after call to standard builtin was profiled
97    myMap.delete = replace
98}
99
100printDelete(2.5); //pgo: true
101                  //aot: [trace] Check Type: NotCallTarget1
102                  //aot: 2.5
103
104printDelete("abc"); //pgo: false
105                    //aot: [trace] Check Type: NotCallTarget1
106                    //aot: abc
107
108myMap.delete = true_delete
109
110// Check IR correctness inside try-block
111try {
112    print("try-block"); //: try-block
113    //aot: [trace] aot inline builtin: Map.delete, caller function name:#*#doDelete@builtinMapDelete
114    printDelete(0); //: false
115    //aot: [trace] aot inline builtin: Map.delete, caller function name:#*#doDelete@builtinMapDelete
116    printDelete("xyz"); //: false
117} catch (e) {
118}
119
120let obj = {};
121obj.valueOf = (() => { return 0; });
122
123//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
124myMap.set(0, 0);
125//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
126print(myMap.delete(obj)); //: false
127
128function Throwing() {
129    this.value = 2;
130    Throwing.prototype.valueOf = function() {
131        if (this.value > 0) {
132            throw new Error("positive");
133        }
134        return this.value;
135    }
136}
137
138let throwingObj = new Throwing();
139//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
140myMap.set(2, 4);
141try {
142//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
143print(myMap.delete(throwingObj)); //: false
144} catch(e) {
145    print(e);
146} finally {
147//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
148    print(myMap.delete(obj)); //: false
149}
150
151// Check after clearing
152myMap.clear();
153//aot: [trace] aot call builtin: Map.clear, caller function name:func_main_0@builtinMapDelete
154print(myMap.delete(2000));
155//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
156//: false
157
158let truedelete = Map.prototype.delete;
159let m = new Map();
160//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
161m.set(1, 2);
162//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
163m.set(2, 4);
164//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
165m.set("ab", 5);
166//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
167m.set("cd", "e");
168let obj1 = {};
169//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
170m.set(obj1, "obj");
171
172print("prototype"); //: prototype
173//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
174print(m.delete(1)); //: true
175//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
176print(m.delete(2)); //: true
177//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
178print(m.delete(3)); //: false
179//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
180print(m.delete("ab")); //: true
181//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
182print(m.delete("cd")); //: true
183//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
184print(m.delete("x")); //: false
185//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
186print(m.delete(obj1)); //: true
187
188let obj2 = {};
189//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
190print(m.delete(obj2)); //: false
191
192//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
193print(m.delete()); //: false
194//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
195m.set(undefined, -1);
196//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapDelete
197print(m.delete()); //: true
198
199print("baseline"); //: baseline
200//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
201m.set(10, 20);
202let m2 = new Map([[1, 2]]);
203let m3 = new Map([[1, 2]]);
204let m4 = new Map([[1, 2]]);
205
206//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
207printDelete2(m, 10); //: true
208//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
209printDelete2(m2, 1); //: true
210//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
211printDelete2(m3, 1); //: true
212//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
213printDelete2(m4, 1); //: true
214
215print("case 0"); //: case 0
216if (ArkTools.isAOTCompiled(printDelete2)) {
217    m4.garbage = function(x: any) {
218        return undefined;
219    }
220}
221
222// Nothing changed
223//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
224m.set(10, 20);
225//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
226m2.set(10, 20);
227//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapDelete
228m3.set(10, 20);
229m4.set(10, 20); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
230
231//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
232printDelete2(m, 10);  //: true
233//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
234printDelete2(m2, 10); //: true
235//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
236printDelete2(m3, 10); //: true
237printDelete2(m4, 10); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
238                      //: true
239
240print("case 1"); //: case 1
241if (ArkTools.isAOTCompiled(printDelete2)) {
242    m3.delete = function(x: any) {
243        return -x;
244    }
245}
246
247m.set(10, 20);
248m2.set(10, 20);
249m3.set(10, 20);
250
251//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
252printDelete2(m, 10);  //: true
253//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
254printDelete2(m2, 10); //: true
255printDelete2(m3, 10); //pgo: true
256                      //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
257                      //aot: -10
258
259print("case 2"); //: case 2
260let mimicMap = {
261    delete: truedelete
262}
263let mm = new Map([[1, 2]]);
264
265//aot: [trace] aot inline builtin: Map.delete, caller function name:#*#printDelete2@builtinMapDelete
266printDelete2(mm, 1); //: true
267if (ArkTools.isAOTCompiled(printDelete2)) {
268    Object.setPrototypeOf(mm, mimicMap);
269}
270printDelete2(mm, 1); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
271                    //: false
272
273print("case 3") //: case 3
274
275function checkObjWithMapProto() {
276    let o = {};
277    //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#checkObjWithMapProto@builtinMapDelete
278    Object.setPrototypeOf(o, Map.prototype);
279    try {
280        print((o as Map<number, number>).delete(1));
281    } catch(e) {
282        print(e);
283    }
284}
285
286//aot: [trace] Check Type: NotCallTarget1
287//: TypeError: obj is not JSMap
288checkObjWithMapProto();
289
290if (ArkTools.isAOTCompiled(printDelete2)) {
291    Map.prototype.delete = function(x: any) {
292        return -x * 10;
293    }
294}
295
296m.set(10, 20);
297m2.set(10, 20);
298
299printDelete2(m, 10); //pgo: true
300                     //aot: [trace] Check Type: NotCallTarget1
301                     //aot: -100
302printDelete2(m2, 10); //pgo: true
303                      //aot: [trace] Check Type: NotCallTarget1
304                      //aot: -100
305