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_ci
834514f5e3Sopenharmony_ci// Call standard builtin with non-number param
844514f5e3Sopenharmony_cimySet.add("abc");
854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
864514f5e3Sopenharmony_ciprint(mySet.has("abc"));
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
884514f5e3Sopenharmony_ci//: true
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_cilet true_add = mySet.add
924514f5e3Sopenharmony_cimySet.add = replace
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ciprint(mySet.add(12)) //: 12
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_cimySet.add = true_add
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
994514f5e3Sopenharmony_ciprint(mySet.add(12)) //: [object Set]
1004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
1014514f5e3Sopenharmony_ciprint(mySet.add(12)) //: [object Set]
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ciprint(mySet.has(12));
1044514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1054514f5e3Sopenharmony_ci//: true
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printAdd)) {
1084514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1094514f5e3Sopenharmony_ci    mySet.add = replace
1104514f5e3Sopenharmony_ci}
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ciprintAdd(42);
1134514f5e3Sopenharmony_ci//pgo: [object Set]
1144514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1154514f5e3Sopenharmony_ci//aot: 42
1164514f5e3Sopenharmony_ciprint(mySet.has(42));
1174514f5e3Sopenharmony_ci//pgo: true
1184514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1194514f5e3Sopenharmony_ci//aot: false
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_cimySet.add = true_add
1224514f5e3Sopenharmony_ci
1234514f5e3Sopenharmony_ciprint(mySet.add("xyz"));
1244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
1254514f5e3Sopenharmony_ci//: [object Set]
1264514f5e3Sopenharmony_ciprint(mySet.has("xyz"));
1274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1284514f5e3Sopenharmony_ci//: true
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1314514f5e3Sopenharmony_citry {
1324514f5e3Sopenharmony_ci    printAdd(2.5);
1334514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:#*#doAdd@builtinSetAdd
1344514f5e3Sopenharmony_ci    //: [object Set]
1354514f5e3Sopenharmony_ci    printAdd("oops");
1364514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:#*#doAdd@builtinSetAdd
1374514f5e3Sopenharmony_ci    //: [object Set]
1384514f5e3Sopenharmony_ci    print(mySet.has(2.5));
1394514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1404514f5e3Sopenharmony_ci    //: true
1414514f5e3Sopenharmony_ci    print(mySet.has("oops"));
1424514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1434514f5e3Sopenharmony_ci    //: true
1444514f5e3Sopenharmony_ci} catch (e) {
1454514f5e3Sopenharmony_ci}
1464514f5e3Sopenharmony_ci
1474514f5e3Sopenharmony_ci// Specific object
1484514f5e3Sopenharmony_cilet obj = {};
1494514f5e3Sopenharmony_ciobj.valueOf = (() => { return 7; })
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_cimySet.add(obj);
1524514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
1534514f5e3Sopenharmony_ciprint(mySet.has(obj));
1544514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1554514f5e3Sopenharmony_ci//: true
1564514f5e3Sopenharmony_ciprint(mySet.has(7));
1574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1584514f5e3Sopenharmony_ci//: false
1594514f5e3Sopenharmony_cimySet.clear();
1604514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetAdd
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1634514f5e3Sopenharmony_citry {
1644514f5e3Sopenharmony_ci    mySet.add(throwingObj);
1654514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
1664514f5e3Sopenharmony_ci    print(mySet.has(throwingObj));
1674514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1684514f5e3Sopenharmony_ci    //: true
1694514f5e3Sopenharmony_ci    print(mySet.has(2));
1704514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1714514f5e3Sopenharmony_ci    //: false
1724514f5e3Sopenharmony_ci} catch(e) {
1734514f5e3Sopenharmony_ci    print(e);
1744514f5e3Sopenharmony_ci} finally {
1754514f5e3Sopenharmony_ci    mySet.add(obj);
1764514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetAdd
1774514f5e3Sopenharmony_ci    print(mySet.has(obj));
1784514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1794514f5e3Sopenharmony_ci    //: true
1804514f5e3Sopenharmony_ci    print(mySet.has(7));
1814514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
1824514f5e3Sopenharmony_ci    //: false
1834514f5e3Sopenharmony_ci}
1844514f5e3Sopenharmony_ci
1854514f5e3Sopenharmony_cifunction checkObjWithSetProto() {
1864514f5e3Sopenharmony_ci    let o = {};
1874514f5e3Sopenharmony_ci    Object.setPrototypeOf(o, Set.prototype);
1884514f5e3Sopenharmony_ci    try {
1894514f5e3Sopenharmony_ci        o.add(1);
1904514f5e3Sopenharmony_ci    } catch(e) {
1914514f5e3Sopenharmony_ci        print(e);
1924514f5e3Sopenharmony_ci    }
1934514f5e3Sopenharmony_ci}
1944514f5e3Sopenharmony_ci
1954514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1964514f5e3Sopenharmony_ci//: TypeError: obj is not JSSet
1974514f5e3Sopenharmony_cicheckObjWithSetProto();
1984514f5e3Sopenharmony_ci
1994514f5e3Sopenharmony_cifunction Throwing() {
2004514f5e3Sopenharmony_ci    this.value = 2;
2014514f5e3Sopenharmony_ci    Throwing.prototype.valueOf = function() {
2024514f5e3Sopenharmony_ci        if (this.value > 0) {
2034514f5e3Sopenharmony_ci            throw new Error("positive");
2044514f5e3Sopenharmony_ci        }
2054514f5e3Sopenharmony_ci        return this.value;
2064514f5e3Sopenharmony_ci    }
2074514f5e3Sopenharmony_ci}
2084514f5e3Sopenharmony_ci
2094514f5e3Sopenharmony_cilet m = new Set();
2104514f5e3Sopenharmony_ci
2114514f5e3Sopenharmony_ciprint("baseline"); //: baseline
2124514f5e3Sopenharmony_cilet m2 = new Set([1]);
2134514f5e3Sopenharmony_cilet m3 = new Set([1]);
2144514f5e3Sopenharmony_cilet m4 = new Set([1]);
2154514f5e3Sopenharmony_ci
2164514f5e3Sopenharmony_citryAdd(m, 13);
2174514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2184514f5e3Sopenharmony_ci//: [object Set]
2194514f5e3Sopenharmony_ciprint(m.has(13));
2204514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2214514f5e3Sopenharmony_ci//: true
2224514f5e3Sopenharmony_ci
2234514f5e3Sopenharmony_citryAdd(m2, 13);
2244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2254514f5e3Sopenharmony_ci//: [object Set]
2264514f5e3Sopenharmony_ciprint(m2.has(13));
2274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2284514f5e3Sopenharmony_ci//: true
2294514f5e3Sopenharmony_ci
2304514f5e3Sopenharmony_citryAdd(m3, 13);
2314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2324514f5e3Sopenharmony_ci//: [object Set]
2334514f5e3Sopenharmony_ciprint(m3.has(13));
2344514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2354514f5e3Sopenharmony_ci//: true
2364514f5e3Sopenharmony_ci
2374514f5e3Sopenharmony_citryAdd(m4, 13);
2384514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2394514f5e3Sopenharmony_ci//: [object Set]
2404514f5e3Sopenharmony_ciprint(m4.has(13));
2414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2424514f5e3Sopenharmony_ci//: true
2434514f5e3Sopenharmony_ci
2444514f5e3Sopenharmony_ciprint("case 0"); //: case 0
2454514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(tryAdd)) {
2464514f5e3Sopenharmony_ci    m4.garbage = function(x: any) {
2474514f5e3Sopenharmony_ci        return undefined;
2484514f5e3Sopenharmony_ci    }
2494514f5e3Sopenharmony_ci}
2504514f5e3Sopenharmony_ci
2514514f5e3Sopenharmony_ci// Nothing changed
2524514f5e3Sopenharmony_citryAdd(m, 25);
2534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2544514f5e3Sopenharmony_ci//: [object Set]
2554514f5e3Sopenharmony_ciprint(m.has(25));
2564514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2574514f5e3Sopenharmony_ci//: true
2584514f5e3Sopenharmony_ci
2594514f5e3Sopenharmony_citryAdd(m2, 25);
2604514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2614514f5e3Sopenharmony_ci//: [object Set]
2624514f5e3Sopenharmony_ciprint(m2.has(25));
2634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2644514f5e3Sopenharmony_ci//: true
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_citryAdd(m3, 25);
2674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:#*#tryAdd@builtinSetAdd
2684514f5e3Sopenharmony_ci//: [object Set]
2694514f5e3Sopenharmony_ciprint(m3.has(25));
2704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2714514f5e3Sopenharmony_ci//: true
2724514f5e3Sopenharmony_ci
2734514f5e3Sopenharmony_citryAdd(m4);
2744514f5e3Sopenharmony_ci//aot: [trace] Check Type: BuiltinInstanceHClassMismatch
2754514f5e3Sopenharmony_ci//: [object Set]
2764514f5e3Sopenharmony_ciprint(m3.has(25));
2774514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetAdd
2784514f5e3Sopenharmony_ci//: true
279