14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_cideclare interface ArkTools {
174514f5e3Sopenharmony_ci    isAOTCompiled(args: any): boolean;
184514f5e3Sopenharmony_ci}
194514f5e3Sopenharmony_cideclare function print(arg:any):string;
204514f5e3Sopenharmony_cifunction replace(a : number)
214514f5e3Sopenharmony_ci{
224514f5e3Sopenharmony_ci    return a;
234514f5e3Sopenharmony_ci}
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cifunction doGet(x: any): any {
264514f5e3Sopenharmony_ci    return myMap.get(x);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cifunction printGet(x: any) {
304514f5e3Sopenharmony_ci    try {
314514f5e3Sopenharmony_ci        print(doGet(x));
324514f5e3Sopenharmony_ci    } finally {
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci}
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_cifunction printGet2(x: any, y: any) {
374514f5e3Sopenharmony_ci    try {
384514f5e3Sopenharmony_ci        print(x.get(y));
394514f5e3Sopenharmony_ci    } finally {
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci}
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_cilet 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]]);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci// Check without params
464514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
474514f5e3Sopenharmony_ciprint(myMap.get()); //: undefined
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci// Check with adding element undefined
504514f5e3Sopenharmony_cimyMap.set(undefined, 42);
514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
524514f5e3Sopenharmony_ciprint(myMap.get()); //: 42
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci// Check with single param
554514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
564514f5e3Sopenharmony_ciprint(myMap.get(0)); //: 5
574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
584514f5e3Sopenharmony_ciprint(myMap.get(3)); //: undefined
594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
604514f5e3Sopenharmony_ciprint(myMap.get(2.5)); //: -2.5
614514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
624514f5e3Sopenharmony_ciprint(myMap.get(NaN)); //: Infinity
634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
644514f5e3Sopenharmony_ciprint("1/x: " + 1 / myMap.get(2000)); //: 1/x: -Infinity
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci// Check with 2 params
674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
684514f5e3Sopenharmony_ciprint(myMap.get(0, 0)); //: 5
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci// Check with 3 params
714514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
724514f5e3Sopenharmony_ciprint(myMap.get(-1, 10.2, 15)); //: 1
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci// Check with 4 params
754514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
764514f5e3Sopenharmony_ciprint(myMap.get(2.5, -800, 0.56, 0)); //: -2.5
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci// Check after inserting elements
794514f5e3Sopenharmony_cimyMap.set(2000, 1e-98);
804514f5e3Sopenharmony_cimyMap.set(133.33, -1);
814514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
824514f5e3Sopenharmony_ciprint(myMap.get(2000)); //: 1e-98
834514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
844514f5e3Sopenharmony_ciprint(myMap.get(133.33)); //: -1
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci// Replace standard builtin
874514f5e3Sopenharmony_cilet true_get = myMap.get
884514f5e3Sopenharmony_cimyMap.get = replace
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci// no deopt
914514f5e3Sopenharmony_ciprint(myMap.get(2.5)); //: 2.5
924514f5e3Sopenharmony_cimyMap.get = true_get
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#doGet@builtinMapGet
954514f5e3Sopenharmony_ciprintGet(-1); //: 1
964514f5e3Sopenharmony_ci// Call standard builtin with non-number param
974514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
984514f5e3Sopenharmony_ciprint(myMap.get("abc")); //: undefined
994514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1004514f5e3Sopenharmony_ciprint(myMap.get("-1")); //: undefined
1014514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1024514f5e3Sopenharmony_ciprint(myMap.get(56)); //: oops
1034514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1044514f5e3Sopenharmony_ciprint(myMap.get("xyz")); //: 12345
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet)) {
1074514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1084514f5e3Sopenharmony_ci    myMap.get = replace
1094514f5e3Sopenharmony_ci}
1104514f5e3Sopenharmony_ciprintGet(2.5); //pgo: -2.5
1114514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1124514f5e3Sopenharmony_ci//aot: 2.5
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ciprintGet("abc"); //pgo: undefined
1154514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1164514f5e3Sopenharmony_ci//aot: abc
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_cimyMap.get = true_get
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1214514f5e3Sopenharmony_citry {
1224514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Map.get, caller function name:#*#doGet@builtinMapGet
1234514f5e3Sopenharmony_ci    printGet(2.5); //: -2.5
1244514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Map.get, caller function name:#*#doGet@builtinMapGet
1254514f5e3Sopenharmony_ci    printGet("abc"); //: undefined
1264514f5e3Sopenharmony_ci} catch (e) {
1274514f5e3Sopenharmony_ci}
1284514f5e3Sopenharmony_ci
1294514f5e3Sopenharmony_cilet obj = {};
1304514f5e3Sopenharmony_ciobj.valueOf = (() => { return 0; })
1314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1324514f5e3Sopenharmony_ciprint(myMap.get(obj)); //: undefined
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_cifunction Throwing() {
1354514f5e3Sopenharmony_ci    this.value = 2;
1364514f5e3Sopenharmony_ci    Throwing.prototype.valueOf = function() {
1374514f5e3Sopenharmony_ci        if (this.value > 0) {
1384514f5e3Sopenharmony_ci            throw new Error("positive");
1394514f5e3Sopenharmony_ci        }
1404514f5e3Sopenharmony_ci        return this.value;
1414514f5e3Sopenharmony_ci    }
1424514f5e3Sopenharmony_ci}
1434514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1444514f5e3Sopenharmony_citry {
1454514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1464514f5e3Sopenharmony_ci    print(myMap.get(throwingObj)); //: undefined
1474514f5e3Sopenharmony_ci} catch(e) {
1484514f5e3Sopenharmony_ci    print(e);
1494514f5e3Sopenharmony_ci} finally {
1504514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1514514f5e3Sopenharmony_ci    print(myMap.get(obj)); //: undefined
1524514f5e3Sopenharmony_ci}
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci// Check after clearing
1554514f5e3Sopenharmony_cimyMap.clear();
1564514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.clear, caller function name:func_main_0@builtinMapGet
1574514f5e3Sopenharmony_ciprint(myMap.get(2000));
1584514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1594514f5e3Sopenharmony_ci//: undefined
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_cilet trueGet = Map.prototype.get
1624514f5e3Sopenharmony_cilet m = new Map()
1634514f5e3Sopenharmony_cim.set(1, 2)
1644514f5e3Sopenharmony_cim.set(2, 4)
1654514f5e3Sopenharmony_cim.set("ab", 5)
1664514f5e3Sopenharmony_cim.set("cd", "e")
1674514f5e3Sopenharmony_cilet obj1 = {}
1684514f5e3Sopenharmony_cim.set(obj1, "obj")
1694514f5e3Sopenharmony_ci
1704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1714514f5e3Sopenharmony_ciprint(m.get(1)) //: 2
1724514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1734514f5e3Sopenharmony_ciprint(m.get(2)) //: 4
1744514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1754514f5e3Sopenharmony_ciprint(m.get(3)) //: undefined
1764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1774514f5e3Sopenharmony_ciprint(m.get("ab")) //: 5
1784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1794514f5e3Sopenharmony_ciprint(m.get("cd")) //: e
1804514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1814514f5e3Sopenharmony_ciprint(m.get("x")) //: undefined
1824514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1834514f5e3Sopenharmony_ciprint(m.get(obj1)) //: obj
1844514f5e3Sopenharmony_cilet obj2 = {}
1854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1864514f5e3Sopenharmony_ciprint(m.get(obj2)) //: undefined
1874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1884514f5e3Sopenharmony_ciprint(m.get()) //: undefined
1894514f5e3Sopenharmony_cim.set(undefined, -1)
1904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:func_main_0@builtinMapGet
1914514f5e3Sopenharmony_ciprint(m.get()) //: -1
1924514f5e3Sopenharmony_ci
1934514f5e3Sopenharmony_cifunction checkObjWithMapProto() {
1944514f5e3Sopenharmony_ci    let o = {};
1954514f5e3Sopenharmony_ci    Object.setPrototypeOf(o, Map.prototype);
1964514f5e3Sopenharmony_ci    try {
1974514f5e3Sopenharmony_ci        print((o as Map<number, number>).get(1));
1984514f5e3Sopenharmony_ci    } catch(e) {
1994514f5e3Sopenharmony_ci        print(e);
2004514f5e3Sopenharmony_ci    }
2014514f5e3Sopenharmony_ci}
2024514f5e3Sopenharmony_ci
2034514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
2044514f5e3Sopenharmony_ci//: TypeError: obj is not JSMap
2054514f5e3Sopenharmony_cicheckObjWithMapProto();
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_cilet notMap = {
2084514f5e3Sopenharmony_ci    get(x: any) {
2094514f5e3Sopenharmony_ci        return this[x] + x
2104514f5e3Sopenharmony_ci    }
2114514f5e3Sopenharmony_ci}
2124514f5e3Sopenharmony_ci
2134514f5e3Sopenharmony_ciprint("baseline") //: baseline
2144514f5e3Sopenharmony_cim[10] = 20
2154514f5e3Sopenharmony_cilet m2 = new Map([[1, 2]])
2164514f5e3Sopenharmony_cilet m3 = new Map([[1, 2]])
2174514f5e3Sopenharmony_cilet m4 = new Map([[1, 2]])
2184514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2194514f5e3Sopenharmony_ciprintGet2(m, 10) //: undefined
2204514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2214514f5e3Sopenharmony_ciprintGet2(m2, 1) //: 2
2224514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2234514f5e3Sopenharmony_ciprintGet2(m3, 1) //: 2
2244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2254514f5e3Sopenharmony_ciprintGet2(m4, 1) //: 2
2264514f5e3Sopenharmony_ci
2274514f5e3Sopenharmony_ciprint("case 0") //: case 0
2284514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet2)) {
2294514f5e3Sopenharmony_ci    m4.garbage = function(x: any) {
2304514f5e3Sopenharmony_ci        return undefined
2314514f5e3Sopenharmony_ci    }
2324514f5e3Sopenharmony_ci}
2334514f5e3Sopenharmony_ci// Nothing changed
2344514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2354514f5e3Sopenharmony_ciprintGet2(m, 10) //: undefined
2364514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2374514f5e3Sopenharmony_ciprintGet2(m2, 1) //: 2
2384514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2394514f5e3Sopenharmony_ciprintGet2(m3, 1) //: 2
2404514f5e3Sopenharmony_ciprintGet2(m4, 1) //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2414514f5e3Sopenharmony_ci                //: 2
2424514f5e3Sopenharmony_ci
2434514f5e3Sopenharmony_ciprint("case 1") //: case 1
2444514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet2)) {
2454514f5e3Sopenharmony_ci    m3.get = function(x: any) {
2464514f5e3Sopenharmony_ci        return -x
2474514f5e3Sopenharmony_ci    }
2484514f5e3Sopenharmony_ci}
2494514f5e3Sopenharmony_ci
2504514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:func_main_0@builtinMapGet
2514514f5e3Sopenharmony_ciprint(Object.getPrototypeOf(m3) === Map.prototype) //: true
2524514f5e3Sopenharmony_ci
2534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2544514f5e3Sopenharmony_ciprintGet2(m, 10) //: undefined
2554514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2564514f5e3Sopenharmony_ciprintGet2(m2, 1) //: 2
2574514f5e3Sopenharmony_ciprintGet2(m3, 1) //pgo: 2
2584514f5e3Sopenharmony_ci                //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2594514f5e3Sopenharmony_ci                //aot: -1
2604514f5e3Sopenharmony_ci
2614514f5e3Sopenharmony_ciprint("case 2") //: case 2
2624514f5e3Sopenharmony_cilet mimicMap = {
2634514f5e3Sopenharmony_ci    get: trueGet
2644514f5e3Sopenharmony_ci}
2654514f5e3Sopenharmony_cilet mm = new Map([[1, 2]])
2664514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Map.get, caller function name:#*#printGet2@builtinMapGet
2674514f5e3Sopenharmony_ciprintGet2(mm, 1) //: 2
2684514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet2)) {
2694514f5e3Sopenharmony_ci    Object.setPrototypeOf(mm, mimicMap)
2704514f5e3Sopenharmony_ci}
2714514f5e3Sopenharmony_ciprintGet2(mm, 1) //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2724514f5e3Sopenharmony_ci                 //: 2
2734514f5e3Sopenharmony_ci
2744514f5e3Sopenharmony_ciprint("case 3") //: case 3
2754514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet2)) {
2764514f5e3Sopenharmony_ci    Map.prototype.get = function(x: any) {
2774514f5e3Sopenharmony_ci        return -x * 10
2784514f5e3Sopenharmony_ci    }
2794514f5e3Sopenharmony_ci}
2804514f5e3Sopenharmony_ci
2814514f5e3Sopenharmony_ciprintGet2(m, 10) //pgo: undefined
2824514f5e3Sopenharmony_ci                //aot: [trace] Check Type: NotCallTarget1
2834514f5e3Sopenharmony_ci                //aot: -100
2844514f5e3Sopenharmony_ciprintGet2(m2, 1) //pgo: 2
2854514f5e3Sopenharmony_ci                //aot: [trace] Check Type: NotCallTarget1
2864514f5e3Sopenharmony_ci                //aot: -10
2874514f5e3Sopenharmony_ciprintGet2(m3, 1) //pgo: 2
2884514f5e3Sopenharmony_ci                //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2894514f5e3Sopenharmony_ci                //aot: -1
2904514f5e3Sopenharmony_ci
2914514f5e3Sopenharmony_ciprint("case 4") //: case 4
2924514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printGet2)) {
2934514f5e3Sopenharmony_ci    Object.setPrototypeOf(m, notMap)
2944514f5e3Sopenharmony_ci}
2954514f5e3Sopenharmony_ci
2964514f5e3Sopenharmony_ciprintGet2(m, 10) //pgo: undefined
2974514f5e3Sopenharmony_ci                //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2984514f5e3Sopenharmony_ci                //aot: 30
2994514f5e3Sopenharmony_ciprintGet2(m2, 1) //pgo: 2
3004514f5e3Sopenharmony_ci                //aot: [trace] Check Type: NotCallTarget1
3014514f5e3Sopenharmony_ci                //aot: -10
3024514f5e3Sopenharmony_ciprintGet2(m3, 1) //pgo: 2
3034514f5e3Sopenharmony_ci                //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
3044514f5e3Sopenharmony_ci                //aot: -1
305