1 /*
2  * Copyright (c) 2021 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 
16 #ifndef ECMASCRIPT_BUILTINS_BUILTINS_BIGINT_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_BIGINT_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/js_tagged_value.h"
21 
22 // List of functions in BigInt, excluding the constructor and '@@' properties.
23 // V(name, func, length, stubIndex)
24 // where BuiltinsBigInt::func refers to the native implementation of BigInt[name].
25 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
26 #define BUILTIN_BIGINT_FUNCTIONS(V)                                         \
27     /* BigInt.asIntN (bits, bigint) */                                      \
28     V("asIntN", AsIntN, 2, BigIntAsIntN)                                    \
29     /* BigInt.asUintN ( bits, bigint ) */                                   \
30     V("asUintN", AsUintN, 2, BigIntAsUintN)
31 // List of functions in BigInt.prototype, excluding the constructor and '@@' properties.
32 // V(name, func, length, stubIndex)
33 // where BuiltinsBigInt::func refers to the native implementation of BigInt.prototype[name].
34 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
35 #define BUILTIN_BIGINT_PROTOTYPE_FUNCTIONS(V)                               \
36     /* BigInt.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */           \
37     V("toLocaleString", ToLocaleString, 0, INVALID)                         \
38     /* BigInt.toString ( [radix] ) */                                       \
39     V("toString", ToString, 0, INVALID)                                     \
40     /* BigInt.valueOf ( ) */                                                \
41     V("valueOf", ValueOf, 0, INVALID)
42 
43 namespace panda::ecmascript::builtins {
44 class BuiltinsBigInt : public base::BuiltinsBase {
45 public:
46     // 21.2.1.1
47     static JSTaggedValue BigIntConstructor(EcmaRuntimeCallInfo *argv);
48     static JSTaggedValue BigIntConstructorInternal(JSThread *thread, JSHandle<JSTaggedValue> value);
49     // 21.2.2.1
50     static JSTaggedValue AsIntN(EcmaRuntimeCallInfo *argv);
51     // 21.2.2.2
52     static JSTaggedValue AsUintN(EcmaRuntimeCallInfo *argv);
53     // 21.2.3.2
54     static JSTaggedValue ToLocaleString(EcmaRuntimeCallInfo *argv);
55     // 21.2.3.3
56     static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv);
57     // 21.2.3.4
58     static JSTaggedValue ValueOf(EcmaRuntimeCallInfo *argv);
59 private:
60     static JSTaggedValue ThisBigIntValue(EcmaRuntimeCallInfo *argv);
61 
62 public:
63     // Excluding the constructor and '@@' internal properties.
GetBigIntFunctions()64     static Span<const base::BuiltinFunctionEntry> GetBigIntFunctions()
65     {
66         return Span<const base::BuiltinFunctionEntry>(BIGINT_FUNCTIONS);
67     }
68 
69     // Excluding the constructor and '@@' internal properties.
GetBigIntPrototypeFunctions()70     static Span<const base::BuiltinFunctionEntry> GetBigIntPrototypeFunctions()
71     {
72         return Span<const base::BuiltinFunctionEntry>(BIGINT_PROTOTYPE_FUNCTIONS);
73     }
74 
75 private:
76 #define BUILTIN_BIGINT_FUNCTION_ENTRY(name, func, length, builtinId) \
77     base::BuiltinFunctionEntry::Create(name, BuiltinsBigInt::func, length, kungfu::BuiltinsStubCSigns::builtinId),
78 
79     static inline std::array BIGINT_FUNCTIONS = {
80         BUILTIN_BIGINT_FUNCTIONS(BUILTIN_BIGINT_FUNCTION_ENTRY)
81     };
82     static inline std::array BIGINT_PROTOTYPE_FUNCTIONS = {
83         BUILTIN_BIGINT_PROTOTYPE_FUNCTIONS(BUILTIN_BIGINT_FUNCTION_ENTRY)
84     };
85 #undef BUILTIN_BIGINT_FUNCTION_ENTRY
86 };
87 }  // namespace panda::ecmascript::builtins
88 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_BIGINT_H