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, b)
214514f5e3Sopenharmony_ci{
224514f5e3Sopenharmony_ci    return a;
234514f5e3Sopenharmony_ci}
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cifunction doAsIntN(bits: number, b : bigint) {
264514f5e3Sopenharmony_ci    return BigInt.asIntN(bits, b);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cifunction tryDoAsIntN(bits: number, b : bigint) {
304514f5e3Sopenharmony_ci    try {
314514f5e3Sopenharmony_ci        print(doAsIntN(bits, b));
324514f5e3Sopenharmony_ci    } catch(e) {
334514f5e3Sopenharmony_ci        print(e);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci}
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_cifunction tryDoAsIntNEmpty() {
384514f5e3Sopenharmony_ci    try {
394514f5e3Sopenharmony_ci        print(BigInt.asIntN());
404514f5e3Sopenharmony_ci    } catch(e) {
414514f5e3Sopenharmony_ci        print(e);
424514f5e3Sopenharmony_ci    }
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_cifunction tryDoAsIntNSingle(a) {
464514f5e3Sopenharmony_ci    try {
474514f5e3Sopenharmony_ci        print(BigInt.asIntN(a));
484514f5e3Sopenharmony_ci    } catch(e) {
494514f5e3Sopenharmony_ci        print(e);
504514f5e3Sopenharmony_ci    }
514514f5e3Sopenharmony_ci}
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_cifunction printAsIntN(bits: number, b : bigint) {
544514f5e3Sopenharmony_ci    try {
554514f5e3Sopenharmony_ci        print(doAsIntN(bits, b));
564514f5e3Sopenharmony_ci    } finally {
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci}
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci// Check standart behaviour
614514f5e3Sopenharmony_ci// 25n = 00011001
624514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
634514f5e3Sopenharmony_ciprint(BigInt.asIntN(3, 25n)); //: 1
644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
654514f5e3Sopenharmony_ciprint(BigInt.asIntN(4, 25n)); //: -7
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci// Check without params
684514f5e3Sopenharmony_citryDoAsIntNEmpty(); //: TypeError: Cannot convert a undefine or null value to a BigInt
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci// Check with 1 param
714514f5e3Sopenharmony_citryDoAsIntNSingle(16); //: TypeError: Cannot convert a undefine or null value to a BigInt
724514f5e3Sopenharmony_citryDoAsIntNSingle(23n); //: TypeError: Cannot convert a BigInt value to a number
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci// Check with 2 params
754514f5e3Sopenharmony_ci// 100n = 01100100
764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
774514f5e3Sopenharmony_ciprint(BigInt.asIntN(4, 100n)); //: 4
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci// Check with 3 params
804514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
814514f5e3Sopenharmony_ciprint(BigInt.asIntN(8, 100n, undefined)); //: 100
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci// Check with 4 params
844514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
854514f5e3Sopenharmony_ciprint(BigInt.asIntN(16, 100n, -20000, "abc")); //: 100
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci// Check some corner cases 
884514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
894514f5e3Sopenharmony_ciprint(BigInt.asIntN(0, 10000n)); //: 0
904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
914514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, 0n)); //: 0
924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
934514f5e3Sopenharmony_ci//aot: [trace] Check Type: RangeError
944514f5e3Sopenharmony_citryDoAsIntN(-2, 10000n); //: RangeError: integerIndex < 0 or integerIndex > SAFE_NUMBER
954514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
964514f5e3Sopenharmony_ci//aot: [trace] Check Type: RangeError
974514f5e3Sopenharmony_citryDoAsIntN(2 ** 53, 100000n); //: RangeError: integerIndex < 0 or integerIndex > SAFE_NUMBER
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci// bits > kMaxLengthBits => return bigint
1004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1014514f5e3Sopenharmony_ciprint(BigInt.asIntN(2 ** 35, 2n ** 75n)); //: 37778931862957161709568
1024514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1034514f5e3Sopenharmony_ciprint(BigInt.asIntN(2 ** 35, 65531n)); //: 65531
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci// Check maximum and minimum values
1064514f5e3Sopenharmony_ciconst max32 = 2n ** (32n - 1n) - 1n; // INT32_MAX = 2147483647
1074514f5e3Sopenharmony_ciconst max32_possible_value = 2n ** 32n - 1n; // 11...11b
1084514f5e3Sopenharmony_ciconst max64 = 2n ** (64n - 1n) - 1n; // INT64_MAX = 9223372036854775807
1094514f5e3Sopenharmony_ciconst max64_possible_value = 2n ** 64n - 1n; // 11...11b
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1124514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, max32)); //: 2147483647
1134514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1144514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, max32 + 1n)); //: -2147483648
1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1164514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, max32_possible_value)); //: -1
1174514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1184514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, max32_possible_value + 1n)); //: 0
1194514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1204514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, max64)); //: 9223372036854775807
1214514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1224514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, max64 + 1n)); //: -9223372036854775808
1234514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1244514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, max64_possible_value)); //: -1
1254514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1264514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, max64_possible_value + 1n)); //: 0
1274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1284514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, -max32 - 1n)); //: -2147483648
1294514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1304514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, -max32 - 2n)); //: 2147483647
1314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1324514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, -max32_possible_value)); //: 1
1334514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1344514f5e3Sopenharmony_ciprint(BigInt.asIntN(32, -max32_possible_value - 1n)); //: 0
1354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1364514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, -max64 - 1n)); //: -9223372036854775808
1374514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1384514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, -max64 - 2n)); //: 9223372036854775807
1394514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1404514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, -max64_possible_value)); //: 1
1414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1424514f5e3Sopenharmony_ciprint(BigInt.asIntN(64, -max64_possible_value - 1n)); //: 0
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ci// Replace standard builtin
1454514f5e3Sopenharmony_cilet true_asintn = BigInt.asIntN
1464514f5e3Sopenharmony_ciBigInt.asIntN = replace
1474514f5e3Sopenharmony_ciprint(BigInt.asIntN(-1.001, 26n)); //: -1.001
1484514f5e3Sopenharmony_ciBigInt.asIntN = true_asintn
1494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
1504514f5e3Sopenharmony_ciprintAsIntN(3, 25n); //: 1
1514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1524514f5e3Sopenharmony_ciprint(true_asintn(3, 25n)); //: 1
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci// Call standard builtin with non-number param
1554514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
1564514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1574514f5e3Sopenharmony_citryDoAsIntN("abc", "abc"); //: SyntaxError: Cannot convert string to a BigInt,because not allow Infinity, decimal points, or exponents
1584514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
1594514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1604514f5e3Sopenharmony_ciprintAsIntN("3", 25n); //: 1
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printAsIntN)) {
1634514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1644514f5e3Sopenharmony_ci    BigInt.asIntN = replace
1654514f5e3Sopenharmony_ci}
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ciprintAsIntN(3, 25n); //pgo: 1
1684514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1694514f5e3Sopenharmony_ci//aot: 3
1704514f5e3Sopenharmony_ci
1714514f5e3Sopenharmony_citryDoAsIntN("abc", "abc"); //pgo: SyntaxError: Cannot convert string to a BigInt,because not allow Infinity, decimal points, or exponents
1724514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1734514f5e3Sopenharmony_ci//aot: abc
1744514f5e3Sopenharmony_ci
1754514f5e3Sopenharmony_ciBigInt.asIntN = true_asintn
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1784514f5e3Sopenharmony_citry {
1794514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
1804514f5e3Sopenharmony_ci    printAsIntN(3, 25n); //: 1
1814514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:#*#doAsIntN@builtinBigIntAsIntN
1824514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber1
1834514f5e3Sopenharmony_ci    printAsIntN("abc", "abc");
1844514f5e3Sopenharmony_ci} catch (e) {
1854514f5e3Sopenharmony_ci}
1864514f5e3Sopenharmony_ci
1874514f5e3Sopenharmony_cilet obj = {
1884514f5e3Sopenharmony_ci    valueOf: () => { return 5; }
1894514f5e3Sopenharmony_ci};
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci// 25n = 00011001
1924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asIntN, caller function name:func_main_0@builtinBigIntAsIntN
1934514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1944514f5e3Sopenharmony_ciprint(BigInt.asIntN(obj, 25n)); //: -7
1954514f5e3Sopenharmony_ci
1964514f5e3Sopenharmony_cifunction Throwing() {
1974514f5e3Sopenharmony_ci    this.value = 5;
1984514f5e3Sopenharmony_ci}
1994514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() {
2004514f5e3Sopenharmony_ci    if (this.value < 0) {
2014514f5e3Sopenharmony_ci        throw new Error("negative bitness");
2024514f5e3Sopenharmony_ci    }
2034514f5e3Sopenharmony_ci    return this.value;
2044514f5e3Sopenharmony_ci}
2054514f5e3Sopenharmony_cilet throwingObj = new Throwing();
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_citry {
2084514f5e3Sopenharmony_ci    // 42n = 00101010
2094514f5e3Sopenharmony_ci    print(BigInt.asIntN(throwingObj, 42n)); //: 10
2104514f5e3Sopenharmony_ci    throwingObj.value = -8;
2114514f5e3Sopenharmony_ci    print(BigInt.asIntN(throwingObj, 42n));
2124514f5e3Sopenharmony_ci} catch(e) {
2134514f5e3Sopenharmony_ci    print(e); //: Error: negative bitness
2144514f5e3Sopenharmony_ci} finally {
2154514f5e3Sopenharmony_ci    // 15n = 00001111
2164514f5e3Sopenharmony_ci    print(BigInt.asIntN(obj, 15n)); //: 15
2174514f5e3Sopenharmony_ci}
218