1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16declare interface ArkTools {
17    isAOTCompiled(args: any): boolean;
18}
19declare function print(arg:any):string;
20function replace(a)
21{
22    return a * 2;
23}
24
25function checkBigInt(x) {
26    let val = BigInt(x);
27    print(val);
28    if (typeof val !== 'bigint') {
29        print(typeof val);
30        throw new Error();
31    }
32}
33
34function checkNotBigInt(x) {
35    try {
36        let val = BigInt(x);
37        print(val);
38        print(typeof val);
39    } catch (e) {
40        print(e.name)
41    }
42}
43
44
45//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
46let n = BigInt(1);
47print(typeof n); //: bigint
48//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
49n = BigInt(1)
50print(n); //: 1
51
52//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
53n = BigInt(1, 2)
54print(typeof n); //: bigint
55print(n); //: 1
56//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
57//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
58checkBigInt(-1); //: -1
59//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
60//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
61checkBigInt(-12345); //: -12345
62//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
63//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
64checkBigInt(1n); //: 1
65//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
66//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
67checkBigInt(-1n); //: -1
68//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
69//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
70checkBigInt(0.0); //: 0
71//aot: [trace] aot inline function name: #*#checkBigInt@builtinBigIntConstructor caller function name: func_main_0@builtinBigIntConstructor
72//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
73checkBigInt(-0.0); //: 0
74//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
75//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
76checkBigInt(BigInt(-1)); //: -1
77
78// test large ints
79// 2^31-1
80//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
81checkBigInt(2147483647); //: 2147483647
82//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
83checkBigInt(2147483647 << 1); //: -2
84//-(2^32)
85//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
86checkBigInt(-2147483648); //: -2147483648
87//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
88checkBigInt(Number.MIN_SAFE_INTEGER); //: -9007199254740991
89//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
90checkBigInt(Number.MAX_SAFE_INTEGER); //: 9007199254740991
91//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
92checkBigInt(Number.MAX_SAFE_INTEGER - 0.5); //: 9007199254740990
93// 1e22
94//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
95checkBigInt(10000000000000000000000) //: 10000000000000000000000
96
97//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
98checkBigInt(1e23) //: 99999999999999991611392
99// also 1e23
100//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
101checkBigInt(100000000000000000000000) //: 99999999999999991611392
102//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
103checkBigInt("100000000000000000000000") //: 100000000000000000000000
104//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
105checkBigInt("-100000000000000000000000") //: -100000000000000000000000
106//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
107checkBigInt(100000000000000000000000n) //: 100000000000000000000000
108//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
109//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
110checkBigInt(BigInt(100000000000000000000000n)) //: 100000000000000000000000
111
112
113//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
114checkBigInt("0xf") //: 15
115//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
116checkBigInt({ valueOf: () => 1 }); //: 1
117//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkBigInt@builtinBigIntConstructor
118checkBigInt({ valueOf: () => "-11" }); //: -11
119
120//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
121checkNotBigInt(1.5); //: RangeError
122//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
123checkNotBigInt(-20.000001); //: RangeError
124//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
125checkNotBigInt(Number.EPSILON); //: RangeError
126//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
127checkNotBigInt(Number.POSITIVE_INFINITY); //: RangeError
128//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
129checkNotBigInt(Number.NEGATIVE_INFINITY); //: RangeError
130//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
131checkNotBigInt(NaN); //: RangeError
132
133//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
134checkNotBigInt(null); //: TypeError
135//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
136checkNotBigInt(undefined); //: TypeError
137//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
138checkNotBigInt("f"); //: SyntaxError
139//aot: [trace] aot inline builtin: BigInt, caller function name:#*#checkNotBigInt@builtinBigIntConstructor
140checkNotBigInt({}); //: SyntaxError
141
142try {
143    //NOTE: not inlined by now
144    //aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
145    n = BigInt();
146} catch(e) {
147    print(e.name); //: TypeError
148}
149
150try {
151    // not inlined as expected
152    n = new BigInt(1);
153} catch(e) {
154    print(e.name); //: TypeError
155}
156
157//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
158n = 2n.constructor(12);
159print(typeof n); //: bigint
160print(n); //: 12
161
162//aot: [trace] aot inline builtin: BigInt, caller function name:func_main_0@builtinBigIntConstructor
163n = globalThis.BigInt(12);
164print(typeof n); //: bigint
165print(n); //: 12
166
167if (ArkTools.isAOTCompiled(checkBigInt)) {
168    BigInt = replace;
169}
170//aot: [trace] Check Type: NotCallTarget1
171print(BigInt(2)); //pgo: 2
172                  //aot: 4
173