• 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 doHas(x: any): any {
26    return myMap.has(x);
27}
28
29function printHas(x: any) {
30    try {
31        print(doHas(x));
32    } finally {
33    }
34}
35
36let myMap = new Map([[0, 0], [0.0, 5], [-1, 1], [2.5, -2.5], [NaN, Infinity], [2000, -0.0], [56, "oops"], ["xyz", "12345"]]);
37
38// Check without params
39//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
40print(myMap.has()); //: false
41
42// Check with adding element undefined
43//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapHas
44myMap.set(undefined, 42);
45//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
46print(myMap.has()); //: true
47
48// Check with single param
49//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
50print(myMap.has(0)); //: true
51//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
52print(myMap.has(3)); //: false
53//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
54print(myMap.has(2.5)); //: true
55//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
56print(myMap.has(NaN)); //: true
57
58// Check with 2 params
59//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
60print(myMap.has(0, 0)); //: true
61
62// Check with 3 params
63//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
64print(myMap.has(-21, 10.2, 15)); //: false
65
66// Check with 4 params
67//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
68print(myMap.has(2.5, -800, 0.56, 0)); //: true
69
70// Check after inserting elements
71//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapHas
72myMap.set(2000, 1e-98);
73//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapHas
74myMap.set(133.33, -1);
75//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
76print(myMap.has(2000)); //: true
77//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
78print(myMap.has(133.33)); //: true
79
80// Replace standard builtin
81let true_has = myMap.has
82myMap.has = replace
83
84// no deopt
85print(myMap.has(2.5)); //: 2.5
86myMap.has = true_has
87
88//aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
89printHas(-1); //: true
90// Call standard builtin with non-number param
91//aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
92printHas("abc"); //: false
93//aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
94printHas("-1"); //: false
95//aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
96printHas(56); //: true
97//aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
98printHas("xyz"); //: true
99
100if (ArkTools.isAOTCompiled(printHas)) {
101    // Replace standard builtin after call to standard builtin was profiled
102    myMap.has = replace
103}
104printHas(2.5); //pgo: true
105//aot: [trace] Check Type: NotCallTarget1
106//aot: 2.5
107
108printHas("abc"); //pgo: false
109//aot: [trace] Check Type: NotCallTarget1
110//aot: abc
111
112myMap.has = true_has
113
114// Check IR correctness inside try-block
115try {
116    //aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
117    printHas(2000); //: true
118    //aot: [trace] aot inline builtin: Map.has, caller function name:#*#doHas@builtinMapHas
119    printHas("abc"); //: false
120} catch (e) {
121}
122
123let obj = {};
124obj.valueOf = (() => { return 0; })
125//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
126print(myMap.has(obj)); //: false
127
128function Throwing() {
129    this.value = 2.5;
130    this.valueOf = function() {
131        if (this.value > 0) {
132            throw new Error("already positive");
133        }
134        return this.value;
135    }
136}
137
138let throwingObj = new Throwing();
139
140try {
141    //aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
142    print(myMap.has(throwingObj)); //: false
143} catch(e) {
144    print(e);
145} finally {
146    //aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
147    print(myMap.has(obj)); //: false
148}
149
150// Check after clearing
151myMap.clear();
152//aot: [trace] aot call builtin: Map.clear, caller function name:func_main_0@builtinMapHas
153print(myMap.has(2000));
154//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
155//: false
156print(myMap.has(2000));
157//aot: [trace] aot inline builtin: Map.has, caller function name:func_main_0@builtinMapHas
158//: false
159
160function checkObjWithMapProto() {
161    let o = {};
162    //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#checkObjWithMapProto@builtinMapHas
163    Object.setPrototypeOf(o, Map.prototype);
164    try {
165        print((o as Map<number, number>).has(1));
166    } catch(e) {
167        print(e);
168    }
169}
170
171//aot: [trace] Check Type: NotCallTarget1
172//: TypeError: obj is not JSMap
173checkObjWithMapProto();
174