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 doHas(x: any) {
264514f5e3Sopenharmony_ci    return mySet.has(x);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cifunction printHas(x: any) {
304514f5e3Sopenharmony_ci    try {
314514f5e3Sopenharmony_ci        print(doHas(x));
324514f5e3Sopenharmony_ci    } finally {
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci}
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_cilet mySet = new Set([0, 0.0, -5, 2.5, 1e-78, NaN, "xyz", "12345"]);
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci// Check without params
394514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
404514f5e3Sopenharmony_ciprint(mySet.has()); //: false
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci// Check with adding element undefined
434514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetHas
444514f5e3Sopenharmony_cimySet.add(undefined);
454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
464514f5e3Sopenharmony_ciprint(mySet.has()); //: true
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// Check with single param
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
504514f5e3Sopenharmony_ciprint(mySet.has(0)); //: true
514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
524514f5e3Sopenharmony_ciprint(mySet.has(3)); //: false
534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
544514f5e3Sopenharmony_ciprint(mySet.has(2.5)); //: true
554514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
564514f5e3Sopenharmony_ciprint(mySet.has(NaN)); //: true
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci// Check with 2 params
594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
604514f5e3Sopenharmony_ciprint(mySet.has(0, 0)); //: true
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci// Check with 3 params
634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
644514f5e3Sopenharmony_ciprint(mySet.has(-21, 10.2, 15)); //: false
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci// Check with 4 params
674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
684514f5e3Sopenharmony_ciprint(mySet.has(2.5, -800, 0.56, 0)); //: true
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci// Check after inserting elements
714514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetHas
724514f5e3Sopenharmony_cimySet.add(-5);
734514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.add, caller function name:func_main_0@builtinSetHas
744514f5e3Sopenharmony_cimySet.add(133.33);
754514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
764514f5e3Sopenharmony_ciprint(mySet.has(-5)); //: true
774514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
784514f5e3Sopenharmony_ciprint(mySet.has(133.33)); //: true
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci// Replace standard builtin
814514f5e3Sopenharmony_cilet true_has = mySet.has
824514f5e3Sopenharmony_cimySet.has = replace
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci// no deopt
854514f5e3Sopenharmony_ciprint(mySet.has(2.5)); //: 2.5
864514f5e3Sopenharmony_cimySet.has = true_has
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_cifunction checkObjWithSetProto() {
894514f5e3Sopenharmony_ci    let o = {};
904514f5e3Sopenharmony_ci    Object.setPrototypeOf(o, Set.prototype);
914514f5e3Sopenharmony_ci    try {
924514f5e3Sopenharmony_ci        print((o as Set<number>).has(1));
934514f5e3Sopenharmony_ci    } catch(e) {
944514f5e3Sopenharmony_ci        print(e);
954514f5e3Sopenharmony_ci    }
964514f5e3Sopenharmony_ci}
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
994514f5e3Sopenharmony_ci//: TypeError: obj is not JSSet
1004514f5e3Sopenharmony_cicheckObjWithSetProto();
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1034514f5e3Sopenharmony_ciprintHas(-5); //: true
1044514f5e3Sopenharmony_ci// Call standard builtin with non-number param
1054514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1064514f5e3Sopenharmony_ciprintHas("abc"); //: false
1074514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1084514f5e3Sopenharmony_ciprintHas("-5"); //: false
1094514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1104514f5e3Sopenharmony_ciprintHas("xyz"); //: true
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printHas)) {
1134514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1144514f5e3Sopenharmony_ci    mySet.has = replace
1154514f5e3Sopenharmony_ci}
1164514f5e3Sopenharmony_ciprintHas(2.5); //pgo: true
1174514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1184514f5e3Sopenharmony_ci//aot: 2.5
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ciprintHas("abc"); //pgo: false
1214514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1224514f5e3Sopenharmony_ci//aot: abc
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_cimySet.has = true_has
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1274514f5e3Sopenharmony_citry {
1284514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1294514f5e3Sopenharmony_ci    printHas(NaN); //: true
1304514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:#*#doHas@builtinSetHas
1314514f5e3Sopenharmony_ci    printHas("abc"); //: false
1324514f5e3Sopenharmony_ci} catch (e) {
1334514f5e3Sopenharmony_ci}
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_cilet obj = {};
1364514f5e3Sopenharmony_ciobj.valueOf = (() => { return 0; })
1374514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
1384514f5e3Sopenharmony_ciprint(mySet.has(obj)); //: false
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_cifunction Throwing() {
1414514f5e3Sopenharmony_ci    this.value = 2.5;
1424514f5e3Sopenharmony_ci    this.valueOf = function() {
1434514f5e3Sopenharmony_ci        if (this.value > 0) {
1444514f5e3Sopenharmony_ci            throw new Error("already positive");
1454514f5e3Sopenharmony_ci        }
1464514f5e3Sopenharmony_ci        return this.value;
1474514f5e3Sopenharmony_ci    }
1484514f5e3Sopenharmony_ci}
1494514f5e3Sopenharmony_ci
1504514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1514514f5e3Sopenharmony_citry {
1524514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
1534514f5e3Sopenharmony_ci    print(mySet.has(throwingObj)); //: false
1544514f5e3Sopenharmony_ci} catch(e) {
1554514f5e3Sopenharmony_ci    print(e);
1564514f5e3Sopenharmony_ci} finally {
1574514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
1584514f5e3Sopenharmony_ci    print(mySet.has(obj)); //: false
1594514f5e3Sopenharmony_ci}
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_ci// Check after clearing
1624514f5e3Sopenharmony_cimySet.clear();
1634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.clear, caller function name:func_main_0@builtinSetHas
1644514f5e3Sopenharmony_ciprint(mySet.has(0));
1654514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Set.has, caller function name:func_main_0@builtinSetHas
1664514f5e3Sopenharmony_ci//: false
167