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;
20
21function doSize(): number {
22    return myMap.size;
23}
24
25function printSize() {
26    try {
27        print(doSize());
28    } finally {
29    }
30}
31
32let myMap = new Map([[0, 0], [0.0, 5], [-1, 1], [2.5, -2.5], [NaN, Infinity], [2000, -0.0], [56, "oops"], ["xyz", "12345"]]);
33
34// Check without params
35print(myMap.size); //: 7
36printSize(); //: 7
37
38// Check IR correctness inside try-block
39try {
40    print(myMap.size); //: 7
41    printSize(); //: 7
42} catch (e) {
43}
44
45// Check after deleting elements
46//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapGetSize
47myMap.delete(-1);
48print(myMap.size); //: 6
49//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapGetSize
50myMap.delete(-200);
51print(myMap.size); //: 6
52
53// Check after inserting elements
54myMap.set(2000, 1e-98);
55print(myMap.size); //: 6
56myMap.set(133.33, -1);
57print(myMap.size); //: 7
58
59// Check after clearing
60myMap.clear();
61//aot: [trace] aot inline builtin: Map.clear, caller function name:func_main_0@builtinMapGetSize
62print(myMap.size); //: 0
63
64// Check deoptimization
65if (ArkTools.isAOTCompiled(printSize)) {
66    // Define 'size' property in 'myMap', which shadows 'prototype.size'
67    Object.defineProperty(myMap, 'size', {
68        value: 42
69    });
70}
71
72printSize();
73//pgo: 0
74//aot: [trace] Check Type: IsNotMap
75//aot: 42
76