• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MATH_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 
21 // List of constants in Math, excluding '@@' internal properties.
22 #define BUILTIN_MATH_CONSTANTS(V)   \
23     V(E)                            \
24     V(LN10)                         \
25     V(LN2)                          \
26     V(LOG10E)                       \
27     V(LOG2E)                        \
28     V(PI)                           \
29     V(SQRT1_2)                      \
30     V(SQRT2)
31 
32 // List of functions in Math.
33 // V(name, func, length, stubIndex)
34 // where BuiltinsMath::func refers to the native implementation of Math[name].
35 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
36 #define BUILTIN_MATH_FUNCTIONS(V)                                           \
37     V("abs",    Abs,    1, MathAbs)     /* Math.abs ( x ) */                \
38     V("acos",   Acos,   1, MathAcos)    /* Math.acos ( x ) */               \
39     V("acosh",  Acosh,  1, MathAcosh)   /* Math.acosh ( x ) */              \
40     V("asin",   Asin,   1, MathAsin)    /* Math.asin ( x ) */               \
41     V("asinh",  Asinh,  1, MathAsinh)   /* Math.asinh ( x ) */              \
42     V("atan",   Atan,   1, MathAtan)    /* Math.atan ( x ) */               \
43     V("atan2",  Atan2,  2, MathAtan2)   /* Math.atan2 ( y, x ) */           \
44     V("atanh",  Atanh,  1, MathAtanh)   /* Math.atanh ( x ) */              \
45     V("cbrt",   Cbrt,   1, MathCbrt)     /* Math.cbrt ( x ) */              \
46     V("ceil",   Ceil,   1, MathCeil)    /* Math.ceil ( x ) */               \
47     V("clz32",  Clz32,  1, MathClz32)   /* Math.clz32 ( x ) */              \
48     V("cos",    Cos,    1, MathCos)     /* Math.cos ( x ) */                \
49     V("cosh",   Cosh,   1, MathCosh)    /* Math.cosh ( x ) */               \
50     V("exp",    Exp,    1, MathExp)     /* Math.exp ( x ) */                \
51     V("expm1",  Expm1,  1, MathExpm1)   /* Math.expm1 ( x ) */              \
52     V("floor",  Floor,  1, MathFloor)       /* Math.floor ( x ) */              \
53     V("fround", Fround, 1, MathFRound)  /* Math.fround ( x ) */             \
54     V("hypot",  Hypot,  2, INVALID)     /* Math.hypot ( ...args ) */        \
55     V("imul",   Imul,   2, MathImul)    /* Math.imul ( x, y ) */            \
56     V("log",    Log,    1, MathLog)     /* Math.log ( x ) */                \
57     V("log10",  Log10,  1, MathLog10)   /* Math.log10 ( x ) */              \
58     V("log1p",  Log1p,  1, MathLog1p)   /* Math.log1p ( x ) */              \
59     V("log2",   Log2,   1, MathLog2)    /* Math.log2 ( x ) */               \
60     V("max",    Max,    2, MathMax)     /* Math.max ( ...args ) */          \
61     V("min",    Min,    2, MathMin)     /* Math.min ( ...args ) */          \
62     V("pow",    Pow,    2, MathPow)     /* Math.pow ( base, exponent ) */   \
63     V("random", Random, 0, INVALID)     /* Math.random ( ) */               \
64     V("round",  Round,  1, MathRound)   /* Math.round ( x ) */              \
65     V("sign",   Sign,   1, MathSign)    /* Math.sign ( x ) */               \
66     V("sin",    Sin,    1, MathSin)     /* Math.sin ( x ) */                \
67     V("sinh",   Sinh,   1, MathSinh)    /* Math.sinh ( x ) */               \
68     V("sqrt",   Sqrt,   1, MathSqrt)    /* Math.sqrt ( x ) */               \
69     V("tan",    Tan,    1, MathTan)     /* Math.tan ( x ) */                \
70     V("tanh",   Tanh,   1, MathTanh)    /* Math.tanh ( x ) */               \
71     V("trunc",  Trunc,  1, MathTrunc)   /* Math.trunc ( x ) */
72 
73 namespace panda::ecmascript::builtins {
74 class BuiltinsMath : public base::BuiltinsBase {
75 public:
76     // 20.2.1.1
77     static constexpr double E = 2.718281828459045;
78     // 20.2.1.2
79     static constexpr double LN10 = 2.302585092994046;
80     // 20.2.1.3
81     static constexpr double LN2 = 0.6931471805599453;
82     // 20.2.1.4
83     static constexpr double LOG10E = 0.4342944819032518;
84     // 20.2.1.5
85     static constexpr double LOG2E = 1.4426950408889634;
86     // 20.2.1.6
87     static constexpr double PI = 3.141592653589793;
88     // 20.2.1.7
89     static constexpr double SQRT1_2 = 0.7071067811865476;
90     // 20.2.1.8
91     static constexpr double SQRT2 = 1.4142135623730951;
92     // 20.2.2.1
93     static JSTaggedValue Abs(EcmaRuntimeCallInfo *argv);
94     // 20.2.2.2
95     static JSTaggedValue Acos(EcmaRuntimeCallInfo *argv);
96     // 20.2.2.3
97     static JSTaggedValue Acosh(EcmaRuntimeCallInfo *argv);
98     // 20.2.2.4
99     static JSTaggedValue Asin(EcmaRuntimeCallInfo *argv);
100     // 20.2.2.5
101     static JSTaggedValue Asinh(EcmaRuntimeCallInfo *argv);
102     // 20.2.2.6
103     static JSTaggedValue Atan(EcmaRuntimeCallInfo *argv);
104     // 20.2.2.7
105     static JSTaggedValue Atanh(EcmaRuntimeCallInfo *argv);
106     // 20.2.2.8
107     static JSTaggedValue Atan2(EcmaRuntimeCallInfo *argv);
108     // 20.2.2.9
109     static JSTaggedValue Cbrt(EcmaRuntimeCallInfo *argv);
110     // 20.2.2.10
111     static JSTaggedValue Ceil(EcmaRuntimeCallInfo *argv);
112     // 20.2.2.11
113     static JSTaggedValue Clz32(EcmaRuntimeCallInfo *argv);
114     // 20.2.2.12
115     static JSTaggedValue Cos(EcmaRuntimeCallInfo *argv);
116     // 20.2.2.13
117     static JSTaggedValue Cosh(EcmaRuntimeCallInfo *argv);
118     // 20.2.2.14
119     static JSTaggedValue Exp(EcmaRuntimeCallInfo *argv);
120     // 20.2.2.15
121     static JSTaggedValue Expm1(EcmaRuntimeCallInfo *argv);
122     // 20.2.2.16
123     static JSTaggedValue Floor(EcmaRuntimeCallInfo *argv);
124     // 20.2.2.17
125     static JSTaggedValue Fround(EcmaRuntimeCallInfo *argv);
126     // 20.2.2.18
127     static JSTaggedValue Hypot(EcmaRuntimeCallInfo *argv);
128     // 20.2.2.19
129     static JSTaggedValue Imul(EcmaRuntimeCallInfo *argv);
130     // 20.2.2.20
131     static JSTaggedValue Log(EcmaRuntimeCallInfo *argv);
132     // 20.2.2.21
133     static JSTaggedValue Log1p(EcmaRuntimeCallInfo *argv);
134     // 20.2.2.22
135     static JSTaggedValue Log10(EcmaRuntimeCallInfo *argv);
136     // 20.2.2.23
137     static JSTaggedValue Log2(EcmaRuntimeCallInfo *argv);
138     // 20.2.2.24
139     static JSTaggedValue Max(EcmaRuntimeCallInfo *argv);
140     // 20.2.2.25
141     static JSTaggedValue Min(EcmaRuntimeCallInfo *argv);
142     // 20.2.2.26
143     static JSTaggedValue Pow(EcmaRuntimeCallInfo *argv);
144     // 20.2.2.27
145     static JSTaggedValue Random(EcmaRuntimeCallInfo *argv);
146     // 20.2.2.28
147     static JSTaggedValue Round(EcmaRuntimeCallInfo *argv);
148     // 20.2.2.29
149     static JSTaggedValue Sign(EcmaRuntimeCallInfo *argv);
150     // 20.2.2.30
151     static JSTaggedValue Sin(EcmaRuntimeCallInfo *argv);
152     // 20.2.2.31
153     static JSTaggedValue Sinh(EcmaRuntimeCallInfo *argv);
154     // 20.2.2.32
155     static JSTaggedValue Sqrt(EcmaRuntimeCallInfo *argv);
156     // 20.2.2.33
157     static JSTaggedValue Tan(EcmaRuntimeCallInfo *argv);
158     // 20.2.2.34
159     static JSTaggedValue Tanh(EcmaRuntimeCallInfo *argv);
160     // 20.2.2.35
161     static JSTaggedValue Trunc(EcmaRuntimeCallInfo *argv);
162 
163     // Excluding the '@@' internal properties.
GetMathConstants()164     static Span<const base::BuiltinConstantEntry> GetMathConstants()
165     {
166         return Span<const base::BuiltinConstantEntry>(MATH_CONSTANTS);
167     }
168 
GetMathFunctions()169     static Span<const base::BuiltinFunctionEntry> GetMathFunctions()
170     {
171         return Span<const base::BuiltinFunctionEntry>(MATH_FUNCTIONS);
172     }
173 
174 private:
175 #define BUILTIN_MATH_CONSTANT_ENTRY(name) \
176     base::BuiltinConstantEntry::Create(#name, JSTaggedValue(BuiltinsMath::name)),
177 
178     static inline std::array MATH_CONSTANTS = {
179         BUILTIN_MATH_CONSTANTS(BUILTIN_MATH_CONSTANT_ENTRY)
180     };
181 #undef BUILTIN_MATH_CONSTANT_ENTRY
182 
183 #define BUILTIN_MATH_FUNCTION_ENTRY(name, func, length, builtinId) \
184     base::BuiltinFunctionEntry::Create(name, BuiltinsMath::func, length, kungfu::BuiltinsStubCSigns::builtinId),
185 
186     static constexpr std::array MATH_FUNCTIONS = {
187         BUILTIN_MATH_FUNCTIONS(BUILTIN_MATH_FUNCTION_ENTRY)
188     };
189 #undef BUILTIN_MATH_FUNCTION_ENTRY
190 };
191 }  // namespace panda::ecmascript::builtins
192 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
193