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()
214514f5e3Sopenharmony_ci{
224514f5e3Sopenharmony_ci    return "replaced";
234514f5e3Sopenharmony_ci}
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cifunction doClear(): any {
264514f5e3Sopenharmony_ci    print(mySet.size);
274514f5e3Sopenharmony_ci    print(mySet.clear());
284514f5e3Sopenharmony_ci    print(mySet.size);
294514f5e3Sopenharmony_ci}
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_cifunction printClear() {
324514f5e3Sopenharmony_ci    try {
334514f5e3Sopenharmony_ci        doClear();
344514f5e3Sopenharmony_ci    } finally {
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci}
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_cifunction printClear2(x: any) {
394514f5e3Sopenharmony_ci    try {
404514f5e3Sopenharmony_ci        print(x.clear());
414514f5e3Sopenharmony_ci    } finally {
424514f5e3Sopenharmony_ci    }
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_cilet mySet = new Set([0, -5, 2.5]);
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci// Check without params
484514f5e3Sopenharmony_ciprint(mySet.size); //: 3
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
504514f5e3Sopenharmony_ciprint(mySet.clear()); //: undefined
514514f5e3Sopenharmony_ciprint(mySet.size); //: 0
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci// Check with single param
544514f5e3Sopenharmony_cilet mySet1 = new Set([0, -5, 2.5]);
554514f5e3Sopenharmony_ciprint(mySet1.size); //: 3
564514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
574514f5e3Sopenharmony_ciprint(mySet1.clear(125)); //: undefined
584514f5e3Sopenharmony_ciprint(mySet1.size); //: 0
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci// Check with 2 params
614514f5e3Sopenharmony_cilet mySet2 = new Set([0, -5, 2.5]);
624514f5e3Sopenharmony_ciprint(mySet2.size); //: 3
634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
644514f5e3Sopenharmony_ciprint(mySet2.clear(0, undefined)); //: undefined
654514f5e3Sopenharmony_ciprint(mySet2.size); //: 0
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_cimySet.add(0);
684514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
694514f5e3Sopenharmony_cimySet.add(12);
704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci// Replace standard builtin
734514f5e3Sopenharmony_cilet true_clear = mySet.clear
744514f5e3Sopenharmony_cimySet.clear = replace
754514f5e3Sopenharmony_ci// no deopt
764514f5e3Sopenharmony_ciprint(mySet.clear()); //: replaced
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_cimySet.clear = true_clear
794514f5e3Sopenharmony_ciprintClear(); //: 2
804514f5e3Sopenharmony_ci              //aot: [trace] aot inline builtin: Set.clear, caller function name:#*#doClear@builtinSetClear
814514f5e3Sopenharmony_ci              //: undefined
824514f5e3Sopenharmony_ci              //: 0
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci// Call standard builtin with non-number param
854514f5e3Sopenharmony_cimySet.add(0);
864514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
874514f5e3Sopenharmony_cimySet.add(12);
884514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
894514f5e3Sopenharmony_ciprint(mySet.size); //: 2
904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
914514f5e3Sopenharmony_cimySet.clear("abc");
924514f5e3Sopenharmony_ciprint(mySet.size); //: 0
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printClear)) {
954514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
964514f5e3Sopenharmony_ci    mySet.clear = replace
974514f5e3Sopenharmony_ci}
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_cimySet.add(0);
1004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1014514f5e3Sopenharmony_cimySet.add(12);
1024514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1034514f5e3Sopenharmony_ciprintClear(); //: 2
1044514f5e3Sopenharmony_ci              //aot: [trace] Check Type: NotCallTarget1
1054514f5e3Sopenharmony_ci              //pgo: undefined
1064514f5e3Sopenharmony_ci              //pgo: 0
1074514f5e3Sopenharmony_ci              //aot: replaced
1084514f5e3Sopenharmony_ci              //aot: 2
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_cimySet.clear = true_clear
1114514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
1124514f5e3Sopenharmony_cimySet.clear();
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1154514f5e3Sopenharmony_citry {
1164514f5e3Sopenharmony_ci    mySet.add(0);
1174514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1184514f5e3Sopenharmony_ci    mySet.add(12);
1194514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1204514f5e3Sopenharmony_ci    printClear(); //: 2
1214514f5e3Sopenharmony_ci                  //aot: [trace] aot inline builtin: Set.clear, caller function name:#*#doClear@builtinSetClear
1224514f5e3Sopenharmony_ci                  //: undefined
1234514f5e3Sopenharmony_ci                  //: 0
1244514f5e3Sopenharmony_ci} catch (e) {
1254514f5e3Sopenharmony_ci}
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_cilet obj = {};
1284514f5e3Sopenharmony_ciobj.valueOf = (() => { return 0; })
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_cimySet.add(0);
1314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1324514f5e3Sopenharmony_cimySet.add(12);
1334514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1344514f5e3Sopenharmony_ciprint(mySet.size); //: 2
1354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
1364514f5e3Sopenharmony_ciprint(mySet.clear(obj)); //: undefined
1374514f5e3Sopenharmony_ciprint(mySet.size); //: 0
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_cifunction Throwing() {
1404514f5e3Sopenharmony_ci    this.value = 2;
1414514f5e3Sopenharmony_ci    Throwing.prototype.valueOf = function() {
1424514f5e3Sopenharmony_ci        if (this.value > 0) {
1434514f5e3Sopenharmony_ci            throw new Error("positive");
1444514f5e3Sopenharmony_ci        }
1454514f5e3Sopenharmony_ci        return this.value;
1464514f5e3Sopenharmony_ci    }
1474514f5e3Sopenharmony_ci}
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1504514f5e3Sopenharmony_citry {
1514514f5e3Sopenharmony_ci    mySet.add(0);
1524514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1534514f5e3Sopenharmony_ci    mySet.add(12);
1544514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1554514f5e3Sopenharmony_ci    print(mySet.size); //: 2
1564514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
1574514f5e3Sopenharmony_ci    print(mySet.clear(throwingObj)); //: undefined
1584514f5e3Sopenharmony_ci    print(mySet.size); //: 0
1594514f5e3Sopenharmony_ci} catch(e) {
1604514f5e3Sopenharmony_ci    print(e);
1614514f5e3Sopenharmony_ci} finally {
1624514f5e3Sopenharmony_ci    mySet.add(0);
1634514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1644514f5e3Sopenharmony_ci    mySet.add(12);
1654514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ci    print(mySet.size); //: 2
1684514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
1694514f5e3Sopenharmony_ci    print(mySet.clear(obj)); //: undefined
1704514f5e3Sopenharmony_ci    print(mySet.size); //: 0
1714514f5e3Sopenharmony_ci}
1724514f5e3Sopenharmony_ci
1734514f5e3Sopenharmony_cilet trueclear = Set.prototype.clear;
1744514f5e3Sopenharmony_cilet m = new Set();
1754514f5e3Sopenharmony_cim.add(1);
1764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1774514f5e3Sopenharmony_cim.add(2);
1784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1794514f5e3Sopenharmony_cim.add("ab");
1804514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1814514f5e3Sopenharmony_cim.add("cd");
1824514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1834514f5e3Sopenharmony_cilet obj1 = {};
1844514f5e3Sopenharmony_cim.add(obj1);
1854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1864514f5e3Sopenharmony_ci
1874514f5e3Sopenharmony_ciprint(m.size); //: 5
1884514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetClear
1894514f5e3Sopenharmony_cim.clear();
1904514f5e3Sopenharmony_ciprint(m.size); //: 0
1914514f5e3Sopenharmony_ci
1924514f5e3Sopenharmony_ciprint("baseline"); //: baseline
1934514f5e3Sopenharmony_cim.add(20);
1944514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
1954514f5e3Sopenharmony_cilet m2 = new Set([1]);
1964514f5e3Sopenharmony_cilet m3 = new Set([1]);
1974514f5e3Sopenharmony_cilet m4 = new Set([1]);
1984514f5e3Sopenharmony_ci
1994514f5e3Sopenharmony_ciprint(m.size); //: 1
2004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2014514f5e3Sopenharmony_ciprintClear2(m) //: undefined
2024514f5e3Sopenharmony_ciprint(m.size); //: 0
2034514f5e3Sopenharmony_ci
2044514f5e3Sopenharmony_ciprint(m2.size); //: 1
2054514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2064514f5e3Sopenharmony_ciprintClear2(m2) //: undefined
2074514f5e3Sopenharmony_ciprint(m2.size); //: 0
2084514f5e3Sopenharmony_ci
2094514f5e3Sopenharmony_ciprint(m3.size); //: 1
2104514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2114514f5e3Sopenharmony_ciprintClear2(m3) //: undefined
2124514f5e3Sopenharmony_ciprint(m3.size); //: 0
2134514f5e3Sopenharmony_ci
2144514f5e3Sopenharmony_ciprint(m4.size); //: 1
2154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2164514f5e3Sopenharmony_ciprintClear2(m4) //: undefined
2174514f5e3Sopenharmony_ciprint(m4.size); //: 0
2184514f5e3Sopenharmony_ci
2194514f5e3Sopenharmony_ciprint("case 0"); //: case 0
2204514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printClear2)) {
2214514f5e3Sopenharmony_ci    m4.garbage = function(x: any) {
2224514f5e3Sopenharmony_ci        return undefined;
2234514f5e3Sopenharmony_ci    }
2244514f5e3Sopenharmony_ci}
2254514f5e3Sopenharmony_ci
2264514f5e3Sopenharmony_ci// Nothing changed
2274514f5e3Sopenharmony_cim.add(20);
2284514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
2294514f5e3Sopenharmony_cim2.add(20);
2304514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
2314514f5e3Sopenharmony_cim3.add(20);
2324514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetClear
2334514f5e3Sopenharmony_cim4.add(20); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2344514f5e3Sopenharmony_ci
2354514f5e3Sopenharmony_ciprint(m.size); //: 1
2364514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2374514f5e3Sopenharmony_ciprintClear2(m) //: undefined
2384514f5e3Sopenharmony_ciprint(m.size); //: 0
2394514f5e3Sopenharmony_ci
2404514f5e3Sopenharmony_ciprint(m2.size); //: 1
2414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2424514f5e3Sopenharmony_ciprintClear2(m2) //: undefined
2434514f5e3Sopenharmony_ciprint(m2.size); //: 0
2444514f5e3Sopenharmony_ci
2454514f5e3Sopenharmony_ciprint(m3.size); //: 1
2464514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2474514f5e3Sopenharmony_ciprintClear2(m3) //: undefined
2484514f5e3Sopenharmony_ciprint(m3.size); //: 0
2494514f5e3Sopenharmony_ci
2504514f5e3Sopenharmony_ciprint(m4.size); //: 1
2514514f5e3Sopenharmony_ciprintClear2(m4) //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2524514f5e3Sopenharmony_ci                //: undefined
2534514f5e3Sopenharmony_ciprint(m4.size); //: 0
2544514f5e3Sopenharmony_ci
2554514f5e3Sopenharmony_ciprint("case 1"); //: case 1
2564514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printClear2)) {
2574514f5e3Sopenharmony_ci    m3.clear = function() {
2584514f5e3Sopenharmony_ci        return false;
2594514f5e3Sopenharmony_ci    }
2604514f5e3Sopenharmony_ci}
2614514f5e3Sopenharmony_ci
2624514f5e3Sopenharmony_cim.add(20);
2634514f5e3Sopenharmony_cim2.add(20);
2644514f5e3Sopenharmony_cim3.add(20);
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_ciprint(m.size); //: 1
2674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2684514f5e3Sopenharmony_ciprintClear2(m) //: undefined
2694514f5e3Sopenharmony_ciprint(m.size); //: 0
2704514f5e3Sopenharmony_ci
2714514f5e3Sopenharmony_ciprint(m3.size); //: 1
2724514f5e3Sopenharmony_ciprintClear2(m3) //pgo: undefined
2734514f5e3Sopenharmony_ci                //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2744514f5e3Sopenharmony_ci                //aot: false
2754514f5e3Sopenharmony_ciprint(m3.size); //pgo: 0
2764514f5e3Sopenharmony_ci                //aot: 1
2774514f5e3Sopenharmony_ci
2784514f5e3Sopenharmony_ciprint("case 2"); //: case 2
2794514f5e3Sopenharmony_cilet mimicSet = {
2804514f5e3Sopenharmony_ci    clear: trueclear
2814514f5e3Sopenharmony_ci}
2824514f5e3Sopenharmony_cilet mm = new Set([1]);
2834514f5e3Sopenharmony_ci
2844514f5e3Sopenharmony_ciprint(mm.size); //: 1
2854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:#*#printClear2@builtinSetClear
2864514f5e3Sopenharmony_ciprintClear2(mm) //: undefined
2874514f5e3Sopenharmony_ciprint(m.size);  //: 0
2884514f5e3Sopenharmony_ci
2894514f5e3Sopenharmony_cifunction checkObjWithSetProto() {
2904514f5e3Sopenharmony_ci    let o = {};
2914514f5e3Sopenharmony_ci    Object.setPrototypeOf(o, Set.prototype);
2924514f5e3Sopenharmony_ci    try {
2934514f5e3Sopenharmony_ci        o.clear(1);
2944514f5e3Sopenharmony_ci    } catch(e) {
2954514f5e3Sopenharmony_ci        print(e);
2964514f5e3Sopenharmony_ci    }
2974514f5e3Sopenharmony_ci}
2984514f5e3Sopenharmony_ci
2994514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
3004514f5e3Sopenharmony_ci//: TypeError: obj is not JSSet
3014514f5e3Sopenharmony_cicheckObjWithSetProto();
3024514f5e3Sopenharmony_ci
3034514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printClear2)) {
3044514f5e3Sopenharmony_ci    Object.setPrototypeOf(mm, mimicSet)
3054514f5e3Sopenharmony_ci}
3064514f5e3Sopenharmony_ci
3074514f5e3Sopenharmony_ciprintClear2(mm) //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
3084514f5e3Sopenharmony_ci                //: undefined
3094514f5e3Sopenharmony_ci
3104514f5e3Sopenharmony_ciprint("case 3") //: case 3
3114514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printClear2)) {
3124514f5e3Sopenharmony_ci    Set.prototype.clear = function(x: any) {
3134514f5e3Sopenharmony_ci        return "prototype";
3144514f5e3Sopenharmony_ci    }
3154514f5e3Sopenharmony_ci}
3164514f5e3Sopenharmony_ci
3174514f5e3Sopenharmony_cim.add(20);
3184514f5e3Sopenharmony_cim2.add(20);
3194514f5e3Sopenharmony_cim3.add(20);
3204514f5e3Sopenharmony_ci
3214514f5e3Sopenharmony_ciprint(m.size);  //: 1
3224514f5e3Sopenharmony_ciprintClear2(m); //pgo: undefined
3234514f5e3Sopenharmony_ci                //aot: [trace] Check Type: NotCallTarget1
3244514f5e3Sopenharmony_ci                //aot: prototype
3254514f5e3Sopenharmony_ciprint(m.size);  //pgo: 0
3264514f5e3Sopenharmony_ci                //aot: 1
3274514f5e3Sopenharmony_ci
3284514f5e3Sopenharmony_ciprint(m2.size);  //: 1
3294514f5e3Sopenharmony_ciprintClear2(m2); //pgo: undefined
3304514f5e3Sopenharmony_ci                 //aot: [trace] Check Type: NotCallTarget1
3314514f5e3Sopenharmony_ci                 //aot: prototype
3324514f5e3Sopenharmony_ciprint(m2.size);  //pgo: 0
3334514f5e3Sopenharmony_ci                 //aot: 1
334