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