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_SYMBOL_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_SYMBOL_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/ecma_runtime_call_info.h"
21 #include "ecmascript/js_tagged_value.h"
22 
23 #define BUILTIN_WELL_KNOWN_SYMBOLS(V)                   \
24     V(hasInstance,        HasInstance)                  \
25     V(isConcatSpreadable, IsConcatSpreadable)           \
26     V(toStringTag,        ToStringTag)
27 
28 #define BUILTIN_PUBLIC_SYMBOLS(V)                       \
29     V(asyncIterator, AsyncIterator)                     \
30     V(iterator,      Iterator)                          \
31     V(match,         Match)                             \
32     V(matchAll,      MatchAll)                          \
33     V(nativeBinding, NativeBinding)                     \
34     V(replace,       Replace)                           \
35     V(search,        Search)                            \
36     V(species,       Species)                           \
37     V(split,         Split)                             \
38     V(toPrimitive,   ToPrimitive)                       \
39     V(unscopables,   Unscopables)
40 
41 #define BUILTIN_ALL_SYMBOLS(V)      \
42     BUILTIN_WELL_KNOWN_SYMBOLS(V)   \
43     BUILTIN_PUBLIC_SYMBOLS(V)
44 
45 // List of functions in Symbol, excluding the '@@' properties.
46 // V(name, func, length, stubIndex)
47 // where BuiltinsSymbol::func refers to the native implementation of Symbol[name].
48 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
49 #define BUILTIN_SYMBOL_FUNCTIONS(V)             \
50     V("for",    For,    1, INVALID)             \
51     V("keyFor", KeyFor, 1, INVALID)
52 
53 // List of get accessors in Symbol.prototype, excluding the '@@' properties.
54 // V(name, func, length, stubIndex)
55 // where BuiltinsSymbol::func refers to the native implementation of Symbol.prototype[name].
56 #define BUILTIN_SYMBOL_PROTOTYPE_FUNCTIONS(V)   \
57     V("toString", ToString, 0, INVALID)         \
58     V("valueOf",  ValueOf,  0, INVALID)
59 
60 namespace panda::ecmascript::builtins {
61 class BuiltinsSymbol : public base::BuiltinsBase {
62 public:
63     // 19.4.1
64     static JSTaggedValue SymbolConstructor(EcmaRuntimeCallInfo *argv);
65 
66     // prototype
67     // 19.4.3.2
68     static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv);
69     // 19.4.3.3
70     static JSTaggedValue ValueOf(EcmaRuntimeCallInfo *argv);
71     // 19.4.2.1 Symbol.for (key)
72     static JSTaggedValue For(EcmaRuntimeCallInfo *argv);
73     // 19.4.2.5 Symbol.keyFor (sym)
74     static JSTaggedValue KeyFor(EcmaRuntimeCallInfo *argv);
75 
76     // 19.4.3.2 get Symbol.prototype.description
77     static JSTaggedValue DescriptionGetter(EcmaRuntimeCallInfo *argv);
78 
79     static JSTaggedValue ThisSymbolValue(JSThread *thread, const JSHandle<JSTaggedValue> &value);
80 
81     // 19.4.3.4
82     static JSTaggedValue ToPrimitive(EcmaRuntimeCallInfo *argv);
83 
84     static JSTaggedValue SymbolDescriptiveString(JSThread *thread, JSTaggedValue sym);
85 
86     // Excluding the '@@' internal properties
GetSymbolFunctions()87     static Span<const base::BuiltinFunctionEntry> GetSymbolFunctions()
88     {
89         return Span<const base::BuiltinFunctionEntry>(SYMBOL_FUNCTIONS);
90     }
91 
92     // Excluding the constructor and '@@' internal properties.
GetSymbolPrototypeFunctions()93     static Span<const base::BuiltinFunctionEntry> GetSymbolPrototypeFunctions()
94     {
95         return Span<const base::BuiltinFunctionEntry>(SYMBOL_PROTOTYPE_FUNCTIONS);
96     }
97 
98 private:
99 #define BUILTIN_SYMBOL_FUNCTION_ENTRY(name, func, length, id) \
100     base::BuiltinFunctionEntry::Create(name, BuiltinsSymbol::func, length, kungfu::BuiltinsStubCSigns::id),
101 
102     static constexpr std::array SYMBOL_FUNCTIONS = {
103         BUILTIN_SYMBOL_FUNCTIONS(BUILTIN_SYMBOL_FUNCTION_ENTRY)
104     };
105     static constexpr std::array SYMBOL_PROTOTYPE_FUNCTIONS = {
106         BUILTIN_SYMBOL_PROTOTYPE_FUNCTIONS(BUILTIN_SYMBOL_FUNCTION_ENTRY)
107     };
108 #undef BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY
109 #undef BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY
110 };
111 }  // namespace panda::ecmascript::builtins
112 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_SYMBOL_H
113