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 
16 #ifndef ECMASCRIPT_BUILTINS_BUILTINS_SHARED_SET_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_SHARED_SET_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/ecma_runtime_call_info.h"
21 
22 // List of functions in Set, excluding the constructor and '@@' properties.
23 // V(name, func, length, stubIndex)
24 // where BuiltinsSet::func refers to the native implementation of Set.prototype[name].
25 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
26 // The following functions are not listed:
27 //   - Set.prototype.keys ( ), which is strictly equal to Set.prototype.values
28 #define BUILTIN_SET_PROTOTYPE_FUNCTIONS(V)                      \
29     /* Set.prototype.add ( value ) */                           \
30     V("add",     Add,     1, SetAdd)                            \
31     /* Set.prototype.clear ( ) */                               \
32     V("clear",   Clear,   0, SetClear)                          \
33     /* Set.prototype.delete ( value ) */                        \
34     V("delete",  Delete,  1, SetDelete)                         \
35     /* Set.prototype.entries ( ) */                             \
36     V("entries", Entries, 0, SetEntries)                        \
37     /* Set.prototype.forEach ( callbackfn [ , thisArg ] ) */    \
38     V("forEach", ForEach, 1, SetForEach)                        \
39     /* Set.prototype.has ( value ) */                           \
40     V("has",     Has,     1, SetHas)                            \
41     /* Set.prototype.values ( ) */                              \
42     V("values",  Values,  0, SetValues)
43 
44 namespace panda::ecmascript::builtins {
45 class BuiltinsSharedSet : public base::BuiltinsBase {
46 public:
47     static JSTaggedValue Constructor(EcmaRuntimeCallInfo *argv);
48     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
49     static JSTaggedValue Add(EcmaRuntimeCallInfo *argv);
50     static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
51     static JSTaggedValue Delete(EcmaRuntimeCallInfo *argv);
52     static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
53     static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
54     static JSTaggedValue Has(EcmaRuntimeCallInfo *argv);
55     static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv);
56     static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
57 
58     // Excluding the '@@' internal properties
GetSetPrototypeFunctions()59     static Span<const base::BuiltinFunctionEntry> GetSetPrototypeFunctions()
60     {
61         return Span<const base::BuiltinFunctionEntry>(SET_PROTOTYPE_FUNCTIONS);
62     }
63 
GetNumPrototypeInlinedProperties()64     static size_t GetNumPrototypeInlinedProperties()
65     {
66         // 5 : 5 more inline properties in Set.prototype
67         //   (1) Set.prototype.constructor
68         //   (2) Set.prototype [ @@toStringTag ]
69         //   (3) Set.prototype [ @@iterator ]
70         //   (4) get Set.prototype.size
71         //   (5) Set.prototype.keys, which is not included in BuiltinsSharedSet::GetSetPrototypeFunctions()
72         return GetSetPrototypeFunctions().Size() + 5;
73     }
74 
GetPrototypeProperties()75     static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties()
76     {
77         return Span<const std::pair<std::string_view, bool>>(SET_PROTOTYPE_PROPERTIES);
78     }
79 
GetFunctionProperties()80     static Span<const std::pair<std::string_view, bool>> GetFunctionProperties()
81     {
82         return Span<const std::pair<std::string_view, bool>>(SET_FUNCTION_PROPERTIES);
83     }
84 private:
85 #define BUILTIN_SET_FUNCTION_ENTRY(name, func, length, id) \
86     base::BuiltinFunctionEntry::Create(name, BuiltinsSharedSet::func, length, kungfu::BuiltinsStubCSigns::id),
87 
88     static constexpr std::array SET_PROTOTYPE_FUNCTIONS = {
89         BUILTIN_SET_PROTOTYPE_FUNCTIONS(BUILTIN_SET_FUNCTION_ENTRY)
90     };
91 
92 #undef BUILTIN_SET_FUNCTION_ENTRY
93 
94 #define SET_PROPERTIES_PAIR(name, func, length, id) \
95     std::pair<std::string_view, bool>(name, false),
96 
97     static constexpr std::array SET_PROTOTYPE_PROPERTIES = {
98         std::pair<std::string_view, bool>("constructor", false),
99         BUILTIN_SET_PROTOTYPE_FUNCTIONS(SET_PROPERTIES_PAIR)
100         std::pair<std::string_view, bool>("keys", false),
101         std::pair<std::string_view, bool>("[Symbol.toStringTag]", false),
102         std::pair<std::string_view, bool>("size", true),
103         std::pair<std::string_view, bool>("[Symbol.iterator]", false)
104     };
105 
106     static constexpr std::array SET_FUNCTION_PROPERTIES = {
107         std::pair<std::string_view, bool>("length", false),
108         std::pair<std::string_view, bool>("name", false),
109         std::pair<std::string_view, bool>("prototype", false),
110         std::pair<std::string_view, bool>("[Symbol.species]", true),
111     };
112 #undef SET_PROPERTIES_PAIR
113 };
114 }  // namespace panda::ecmascript::builtins
115 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_SET_H
116