14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 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_ci#ifndef ECMASCRIPT_BUILTINS_BUILTINS_NUMBER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_NUMBER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ci// List of constants in Number, excluding '@@' internal properties.
234514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_CONSTANTS(V)                             \
244514f5e3Sopenharmony_ci    V(EPSILON)           /* Number.EPSILON */                   \
254514f5e3Sopenharmony_ci    V(MAX_SAFE_INTEGER)  /* Number.MAX_SAFE_INTEGER */          \
264514f5e3Sopenharmony_ci    V(MAX_VALUE)         /* Number.MAX_VALUE */                 \
274514f5e3Sopenharmony_ci    V(MIN_SAFE_INTEGER)  /* Number.MIN_SAFE_INTEGER */          \
284514f5e3Sopenharmony_ci    V(MIN_VALUE)         /* Number.MIN_VALUE */                 \
294514f5e3Sopenharmony_ci    V(NEGATIVE_INFINITY) /* Number.NEGATIVE_INFINITY */         \
304514f5e3Sopenharmony_ci    V(NaN)               /* Number.NaN */                       \
314514f5e3Sopenharmony_ci    V(POSITIVE_INFINITY) /* Number.POSITIVE_INFINITY */
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci// List of functions in Number.
344514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
354514f5e3Sopenharmony_ci// where BuiltinsNumber::func refers to the native implementation of Number[name].
364514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
374514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_NON_GLOBAL_FUNCTIONS(V)                                                           \
384514f5e3Sopenharmony_ci    V("isFinite",      IsFinite,      1, NumberIsFinite)          /* Number.isFinite ( number ) */       \
394514f5e3Sopenharmony_ci    V("isInteger",     IsInteger,     1, NumberIsInteger)         /* Number.isInteger ( number ) */      \
404514f5e3Sopenharmony_ci    V("isNaN",         IsNaN,         1, NumberIsNaN)             /* Number.isNaN ( number ) */          \
414514f5e3Sopenharmony_ci    V("isSafeInteger", IsSafeInteger, 1, NumberIsSafeInteger)     /* Number.isSafeInteger ( number ) */
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci// List of functions in Number that can be accessed via globalThis.
444514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
454514f5e3Sopenharmony_ci// where BuiltinsNumber::func refers to the native implementation of Number[name].
464514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_GLOBAL_FUNCTIONS(V)                                              \
474514f5e3Sopenharmony_ci    V("parseFloat", ParseFloat, 1, NumberParseFloat) /* Number.parseFloat ( string ) */ \
484514f5e3Sopenharmony_ci    V("parseInt",   ParseInt,   2, NumberParseInt) /* Number.parseInt ( string, radix ) */
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_FUNCTIONS(V)         \
514514f5e3Sopenharmony_ci    BUILTIN_NUMBER_NON_GLOBAL_FUNCTIONS(V)  \
524514f5e3Sopenharmony_ci    BUILTIN_NUMBER_GLOBAL_FUNCTIONS(V)
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci// List of functions in Number.prototype, excluding the constructor and '@@' properties.
554514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
564514f5e3Sopenharmony_ci// where BuiltinsNumber::func refers to the native implementation of Number.prototype[name].
574514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_PROTOTYPE_FUNCTIONS(V)                               \
584514f5e3Sopenharmony_ci    /* Number.prototype.toExponential ( fractionDigits ) */                 \
594514f5e3Sopenharmony_ci    V("toExponential",  ToExponential,  1, INVALID)                         \
604514f5e3Sopenharmony_ci    /* Number.prototype.toFixed ( fractionDigits ) */                       \
614514f5e3Sopenharmony_ci    V("toFixed",        ToFixed,        1, INVALID)                         \
624514f5e3Sopenharmony_ci    /* Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */ \
634514f5e3Sopenharmony_ci    V("toLocaleString", ToLocaleString, 0, INVALID)                         \
644514f5e3Sopenharmony_ci    /* Number.prototype.toPrecision ( precision ) */                        \
654514f5e3Sopenharmony_ci    V("toPrecision",    ToPrecision,    1, INVALID)                         \
664514f5e3Sopenharmony_ci    /* Number.prototype.toString ( [ radix ] ) */                           \
674514f5e3Sopenharmony_ci    V("toString",       ToString,       1, NumberToString)                  \
684514f5e3Sopenharmony_ci    /* Number.prototype.valueOf ( ) */                                      \
694514f5e3Sopenharmony_ci    V("valueOf",        ValueOf,        0, INVALID)
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
724514f5e3Sopenharmony_ciclass BuiltinsNumber : public base::BuiltinsBase {
734514f5e3Sopenharmony_cipublic:
744514f5e3Sopenharmony_ci    // 21.1.2.1 Number.EPSILON
754514f5e3Sopenharmony_ci    static constexpr double EPSILON = std::numeric_limits<double>::epsilon();
764514f5e3Sopenharmony_ci    // 21.1.2.6 Number.MAX_SAFE_INTEGER (which is 2**53 - 1 = 9007199254740991)
774514f5e3Sopenharmony_ci    static constexpr int64_t MAX_SAFE_INTEGER = (1LL << 53) - 1;
784514f5e3Sopenharmony_ci    // 21.1.2.8 Number.MIN_SAFE_INTEGER (which is -(2**53 - 1) = -9007199254740991)
794514f5e3Sopenharmony_ci    static constexpr int64_t MIN_SAFE_INTEGER = -((1LL << 53) - 1);
804514f5e3Sopenharmony_ci    // 21.1.2.7 Number.MAX_VALUE
814514f5e3Sopenharmony_ci    static constexpr double MAX_VALUE = std::numeric_limits<double>::max();
824514f5e3Sopenharmony_ci    // 21.1.2.9 Number.MIN_VALUE
834514f5e3Sopenharmony_ci    static constexpr double MIN_VALUE = std::numeric_limits<double>::denorm_min();
844514f5e3Sopenharmony_ci    // 21.1.2.14 Number.POSITIVE_INFINITY
854514f5e3Sopenharmony_ci    static constexpr double POSITIVE_INFINITY = std::numeric_limits<double>::infinity();
864514f5e3Sopenharmony_ci    // 21.1.2.11 Number.NEGATIVE_INFINITY
874514f5e3Sopenharmony_ci    static constexpr double NEGATIVE_INFINITY = -POSITIVE_INFINITY;
884514f5e3Sopenharmony_ci    // 21.1.2.10 Number.NaN
894514f5e3Sopenharmony_ci    static constexpr double NaN = NAN;
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci    // 20.1.1.1
924514f5e3Sopenharmony_ci    static JSTaggedValue NumberConstructor(EcmaRuntimeCallInfo *argv);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    // 20.1.2.2
954514f5e3Sopenharmony_ci    static JSTaggedValue IsFinite(EcmaRuntimeCallInfo *argv);
964514f5e3Sopenharmony_ci    // 20.1.2.3
974514f5e3Sopenharmony_ci    static JSTaggedValue IsInteger(EcmaRuntimeCallInfo *argv);
984514f5e3Sopenharmony_ci    // 20.1.2.4
994514f5e3Sopenharmony_ci    static JSTaggedValue IsNaN(EcmaRuntimeCallInfo *argv);
1004514f5e3Sopenharmony_ci    // 20.1.2.5
1014514f5e3Sopenharmony_ci    static JSTaggedValue IsSafeInteger(EcmaRuntimeCallInfo *argv);
1024514f5e3Sopenharmony_ci    // 20.1.2.12
1034514f5e3Sopenharmony_ci    static JSTaggedValue ParseFloat(EcmaRuntimeCallInfo *argv);
1044514f5e3Sopenharmony_ci    // 20.1.2.13
1054514f5e3Sopenharmony_ci    static JSTaggedValue ParseInt(EcmaRuntimeCallInfo *argv);
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ci    // prototype
1084514f5e3Sopenharmony_ci    // 20.1.3.2
1094514f5e3Sopenharmony_ci    static JSTaggedValue ToExponential(EcmaRuntimeCallInfo *argv);
1104514f5e3Sopenharmony_ci    // 20.1.3.3
1114514f5e3Sopenharmony_ci    static JSTaggedValue ToFixed(EcmaRuntimeCallInfo *argv);
1124514f5e3Sopenharmony_ci    // 20.1.3.4
1134514f5e3Sopenharmony_ci    static JSTaggedValue ToLocaleString(EcmaRuntimeCallInfo *argv);
1144514f5e3Sopenharmony_ci    // 20.1.3.5
1154514f5e3Sopenharmony_ci    static JSTaggedValue ToPrecision(EcmaRuntimeCallInfo *argv);
1164514f5e3Sopenharmony_ci    // 20.1.3.6
1174514f5e3Sopenharmony_ci    static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv);
1184514f5e3Sopenharmony_ci    // 20.1.3.7
1194514f5e3Sopenharmony_ci    static JSTaggedValue ValueOf(EcmaRuntimeCallInfo *argv);
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties.
1224514f5e3Sopenharmony_ci    static Span<const base::BuiltinConstantEntry> GetNumberConstants()
1234514f5e3Sopenharmony_ci    {
1244514f5e3Sopenharmony_ci        return Span<const base::BuiltinConstantEntry>(NUMBER_CONSTANTS);
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties.
1284514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetNumberNonGlobalFunctions()
1294514f5e3Sopenharmony_ci    {
1304514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(NUMBER_NON_GLOBAL_FUNCTIONS);
1314514f5e3Sopenharmony_ci    }
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties.
1344514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetNumberGlobalFunctions()
1354514f5e3Sopenharmony_ci    {
1364514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(NUMBER_GLOBAL_FUNCTIONS);
1374514f5e3Sopenharmony_ci    }
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci    // Excluding the constructor and '@@' internal properties.
1404514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetNumberPrototypeFunctions()
1414514f5e3Sopenharmony_ci    {
1424514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(NUMBER_PROTOTYPE_FUNCTIONS);
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ciprivate:
1464514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_CONSTANT_ENTRY(name) \
1474514f5e3Sopenharmony_ci    base::BuiltinConstantEntry::Create(#name, JSTaggedValue(BuiltinsNumber::name)),
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_ci    static inline std::array NUMBER_CONSTANTS = {
1504514f5e3Sopenharmony_ci        BUILTIN_NUMBER_CONSTANTS(BUILTIN_NUMBER_CONSTANT_ENTRY)
1514514f5e3Sopenharmony_ci    };
1524514f5e3Sopenharmony_ci#undef BUILTIN_NUMBER_CONSTANT_ENTRY
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci#define BUILTIN_NUMBER_FUNCTION_ENTRY(name, func, length, id) \
1554514f5e3Sopenharmony_ci    base::BuiltinFunctionEntry::Create(name, BuiltinsNumber::func, length, kungfu::BuiltinsStubCSigns::id),
1564514f5e3Sopenharmony_ci
1574514f5e3Sopenharmony_ci    static constexpr std::array NUMBER_NON_GLOBAL_FUNCTIONS = {
1584514f5e3Sopenharmony_ci        BUILTIN_NUMBER_NON_GLOBAL_FUNCTIONS(BUILTIN_NUMBER_FUNCTION_ENTRY)
1594514f5e3Sopenharmony_ci    };
1604514f5e3Sopenharmony_ci    static constexpr std::array NUMBER_GLOBAL_FUNCTIONS = {
1614514f5e3Sopenharmony_ci        BUILTIN_NUMBER_GLOBAL_FUNCTIONS(BUILTIN_NUMBER_FUNCTION_ENTRY)
1624514f5e3Sopenharmony_ci    };
1634514f5e3Sopenharmony_ci    static constexpr std::array NUMBER_PROTOTYPE_FUNCTIONS = {
1644514f5e3Sopenharmony_ci        BUILTIN_NUMBER_PROTOTYPE_FUNCTIONS(BUILTIN_NUMBER_FUNCTION_ENTRY)
1654514f5e3Sopenharmony_ci    };
1664514f5e3Sopenharmony_ci#undef BUILTIN_NUMBER_FUNCTION_ENTRY
1674514f5e3Sopenharmony_ci
1684514f5e3Sopenharmony_ci    static JSTaggedNumber ThisNumberValue(JSThread *thread, EcmaRuntimeCallInfo *argv);
1694514f5e3Sopenharmony_ci};
1704514f5e3Sopenharmony_ci
1714514f5e3Sopenharmony_ciclass NumberToStringResultCache : public TaggedArray {
1724514f5e3Sopenharmony_cipublic:
1734514f5e3Sopenharmony_ci    static NumberToStringResultCache *Cast(TaggedObject *object)
1744514f5e3Sopenharmony_ci    {
1754514f5e3Sopenharmony_ci        return reinterpret_cast<NumberToStringResultCache*>(object);
1764514f5e3Sopenharmony_ci    }
1774514f5e3Sopenharmony_ci    static JSTaggedValue CreateCacheTable(const JSThread *thread);
1784514f5e3Sopenharmony_ci    JSTaggedValue FindCachedResult(int entry, JSTaggedValue &target);
1794514f5e3Sopenharmony_ci    void SetCachedResult(const JSThread *thread, int entry, JSTaggedValue &number, JSHandle<EcmaString> &result);
1804514f5e3Sopenharmony_ci    int GetNumberHash(JSTaggedValue &number)
1814514f5e3Sopenharmony_ci    {
1824514f5e3Sopenharmony_ci        unsigned int mask = INITIAL_CACHE_NUMBER - 1;
1834514f5e3Sopenharmony_ci        unsigned int value = 0;
1844514f5e3Sopenharmony_ci        if (number.IsInt()) {
1854514f5e3Sopenharmony_ci            value = static_cast<unsigned int>(number.GetInt());
1864514f5e3Sopenharmony_ci        } else {
1874514f5e3Sopenharmony_ci            int64_t bits = base::bit_cast<int64_t>(number.GetDouble());
1884514f5e3Sopenharmony_ci            value = static_cast<unsigned int>(bits) ^ static_cast<unsigned int>(bits >> 32); // 32: hight 32 bit
1894514f5e3Sopenharmony_ci        }
1904514f5e3Sopenharmony_ci        return value & mask;
1914514f5e3Sopenharmony_ci    }
1924514f5e3Sopenharmony_ci
1934514f5e3Sopenharmony_ciprivate:
1944514f5e3Sopenharmony_ci    static constexpr int INITIAL_CACHE_NUMBER = 256;
1954514f5e3Sopenharmony_ci    static constexpr int NUMBER_INDEX = 0;
1964514f5e3Sopenharmony_ci    static constexpr int RESULT_INDEX = 1;
1974514f5e3Sopenharmony_ci    static constexpr int ENTRY_SIZE = 2;
1984514f5e3Sopenharmony_ci};
1994514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
2004514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_NUBMER_H
201