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 doAsUintN(bits: number, b : bigint) { 264514f5e3Sopenharmony_ci return BigInt.asUintN(bits, b); 274514f5e3Sopenharmony_ci} 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_cifunction tryDoAsUintNEmpty() { 304514f5e3Sopenharmony_ci try { 314514f5e3Sopenharmony_ci print(BigInt.asUintN()); 324514f5e3Sopenharmony_ci } catch(e) { 334514f5e3Sopenharmony_ci print(e); 344514f5e3Sopenharmony_ci } 354514f5e3Sopenharmony_ci} 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_cifunction tryDoAsUintNSingle(a) { 384514f5e3Sopenharmony_ci try { 394514f5e3Sopenharmony_ci print(BigInt.asUintN(a)); 404514f5e3Sopenharmony_ci } catch(e) { 414514f5e3Sopenharmony_ci print(e); 424514f5e3Sopenharmony_ci } 434514f5e3Sopenharmony_ci} 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_cifunction tryDoAsUintN(bits: number, b : bigint) { 464514f5e3Sopenharmony_ci try { 474514f5e3Sopenharmony_ci print(doAsUintN(bits, b)); 484514f5e3Sopenharmony_ci } catch(e) { 494514f5e3Sopenharmony_ci print(e); 504514f5e3Sopenharmony_ci } 514514f5e3Sopenharmony_ci} 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_cifunction printAsUintN(bits: number, b : bigint) { 544514f5e3Sopenharmony_ci try { 554514f5e3Sopenharmony_ci print(doAsUintN(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.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 634514f5e3Sopenharmony_ciprint(BigInt.asUintN(3, 25n)); //: 1 644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 654514f5e3Sopenharmony_ciprint(BigInt.asUintN(4, 25n)); //: 9 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci// Check without params 684514f5e3Sopenharmony_citryDoAsUintNEmpty(); //: TypeError: Cannot convert a undefine or null value to a BigInt 694514f5e3Sopenharmony_ci 704514f5e3Sopenharmony_ci// Check with 1 param 714514f5e3Sopenharmony_citryDoAsUintNSingle(15); //: TypeError: Cannot convert a undefine or null value to a BigInt 724514f5e3Sopenharmony_citryDoAsUintNSingle(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.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 774514f5e3Sopenharmony_ciprint(BigInt.asUintN(4, 100n)); //: 4 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_ci// Check with 3 params 804514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 814514f5e3Sopenharmony_ciprint(BigInt.asUintN(8, 100n, undefined)); //: 100 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ci// Check with 4 params 844514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 854514f5e3Sopenharmony_ciprint(BigInt.asUintN(16, 100n, -20000, "abc")); //: 100 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci// Check some corner cases 884514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 894514f5e3Sopenharmony_ciprint(BigInt.asUintN(0, 10000n)); //: 0 904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 914514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, 0n)); //: 0 924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 934514f5e3Sopenharmony_ci//aot: [trace] Check Type: RangeError 944514f5e3Sopenharmony_citryDoAsUintN(-2, 10000n); //: RangeError: integerIndex < 0 or integerIndex > SAFE_NUMBER 954514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 964514f5e3Sopenharmony_ci//aot: [trace] Check Type: RangeError 974514f5e3Sopenharmony_citryDoAsUintN(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.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1014514f5e3Sopenharmony_ciprint(BigInt.asUintN(2 ** 35, 2n ** 75n)); //: 37778931862957161709568 1024514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1034514f5e3Sopenharmony_ciprint(BigInt.asUintN(2 ** 35, 65531n)); //: 65531 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_ci// Check maximum and minimum values 1064514f5e3Sopenharmony_ciconst max32 = 2n ** 32n - 1n; // UINT32_MAX = 4294967295 1074514f5e3Sopenharmony_ciconst max64 = 2n ** 64n - 1n; // UINT64_MAX = 18446744073709551615 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1104514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, max32)); //: 4294967295 1114514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1124514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, max32 + 1n)); //: 0 1134514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1144514f5e3Sopenharmony_ciprint(BigInt.asUintN(64, max64)); //: 18446744073709551615 1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1164514f5e3Sopenharmony_ciprint(BigInt.asUintN(64, max64 + 1n)); //: 0 1174514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1184514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, -max32)); //: 1 1194514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1204514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, -max32 - 1n)); //: 0 1214514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1224514f5e3Sopenharmony_ciprint(BigInt.asUintN(32, -max32 - 2n)); //: 4294967295 1234514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1244514f5e3Sopenharmony_ciprint(BigInt.asUintN(64, -max64)); //: 1 1254514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1264514f5e3Sopenharmony_ciprint(BigInt.asUintN(64, -max64 - 1n)); //: 0 1274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1284514f5e3Sopenharmony_ciprint(BigInt.asUintN(64, -max64 - 2n)); //: 18446744073709551615 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci// Replace standard builtin 1314514f5e3Sopenharmony_cilet true_asUintN = BigInt.asUintN 1324514f5e3Sopenharmony_ciBigInt.asUintN = replace 1334514f5e3Sopenharmony_ciprint(BigInt.asUintN(-1.001, 26n)); //: -1.001 1344514f5e3Sopenharmony_ciBigInt.asUintN = true_asUintN 1354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 1364514f5e3Sopenharmony_ciprintAsUintN(3, 25n); //: 1 1374514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1384514f5e3Sopenharmony_ciprint(true_asUintN(3, 25n)); //: 1 1394514f5e3Sopenharmony_ci 1404514f5e3Sopenharmony_ci// Call standard builtin with non-number param 1414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 1424514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1 1434514f5e3Sopenharmony_citryDoAsUintN("abc", "abc"); //: SyntaxError: Cannot convert string to a BigInt,because not allow Infinity, decimal points, or exponents 1444514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 1454514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1 1464514f5e3Sopenharmony_ciprintAsUintN("3", 25n); //: 1 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printAsUintN)) { 1494514f5e3Sopenharmony_ci // Replace standard builtin after call to standard builtin was profiled 1504514f5e3Sopenharmony_ci BigInt.asUintN = replace 1514514f5e3Sopenharmony_ci} 1524514f5e3Sopenharmony_ci 1534514f5e3Sopenharmony_ciprintAsUintN(3, 25n); //pgo: 1 1544514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1 1554514f5e3Sopenharmony_ci//aot: 3 1564514f5e3Sopenharmony_ci 1574514f5e3Sopenharmony_citryDoAsUintN("abc", "abc"); //pgo: SyntaxError: Cannot convert string to a BigInt,because not allow Infinity, decimal points, or exponents 1584514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1 1594514f5e3Sopenharmony_ci//aot: abc 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ciBigInt.asUintN = true_asUintN 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ci// Check IR correctness inside try-block 1644514f5e3Sopenharmony_citry { 1654514f5e3Sopenharmony_ci //aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 1664514f5e3Sopenharmony_ci printAsUintN(3, 25n); //: 1 1674514f5e3Sopenharmony_ci //aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:#*#doAsUintN@builtinBigIntAsUintN 1684514f5e3Sopenharmony_ci //aot: [trace] Check Type: NotNumber1 1694514f5e3Sopenharmony_ci printAsUintN("abc", "abc"); 1704514f5e3Sopenharmony_ci} catch (e) { 1714514f5e3Sopenharmony_ci} 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_cilet obj = { 1744514f5e3Sopenharmony_ci valueOf: () => { return 5; } 1754514f5e3Sopenharmony_ci}; 1764514f5e3Sopenharmony_ci 1774514f5e3Sopenharmony_ci// 25n = 00011001 1784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: BigInt.asUintN, caller function name:func_main_0@builtinBigIntAsUintN 1794514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1 1804514f5e3Sopenharmony_ciprint(BigInt.asUintN(obj, 25n)); //: 25 1814514f5e3Sopenharmony_ci 1824514f5e3Sopenharmony_cifunction Throwing() { 1834514f5e3Sopenharmony_ci this.value = 5; 1844514f5e3Sopenharmony_ci} 1854514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() { 1864514f5e3Sopenharmony_ci if (this.value < 0) { 1874514f5e3Sopenharmony_ci throw new Error("negative bitness"); 1884514f5e3Sopenharmony_ci } 1894514f5e3Sopenharmony_ci return this.value; 1904514f5e3Sopenharmony_ci} 1914514f5e3Sopenharmony_cilet throwingObj = new Throwing(); 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_citry { 1944514f5e3Sopenharmony_ci // 42n = 00101010 1954514f5e3Sopenharmony_ci print(BigInt.asUintN(throwingObj, 42n)); //: 10 1964514f5e3Sopenharmony_ci throwingObj.value = -8; 1974514f5e3Sopenharmony_ci print(BigInt.asUintN(throwingObj, 42n)); 1984514f5e3Sopenharmony_ci} catch(e) { 1994514f5e3Sopenharmony_ci print(e); //: Error: negative bitness 2004514f5e3Sopenharmony_ci} finally { 2014514f5e3Sopenharmony_ci // 15n = 00001111 2024514f5e3Sopenharmony_ci print(BigInt.asUintN(obj, 15n)); //: 15 2034514f5e3Sopenharmony_ci} 204