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