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_SET_H
17#define ECMASCRIPT_BUILTINS_BUILTINS_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
44namespace panda::ecmascript::builtins {
45class BuiltinsSet : public base::BuiltinsBase {
46public:
47    // 23.2.1.1
48    static JSTaggedValue SetConstructor(EcmaRuntimeCallInfo *argv);
49    // 23.2.2.2
50    static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
51    // 23.2.3.1
52    static JSTaggedValue Add(EcmaRuntimeCallInfo *argv);
53    // 23.2.3.2
54    static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
55    // 23.2.3.4
56    static JSTaggedValue Delete(EcmaRuntimeCallInfo *argv);
57    // 23.2.3.5
58    static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
59    // 23.2.3.6
60    static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
61    // 23.2.3.7
62    static JSTaggedValue Has(EcmaRuntimeCallInfo *argv);
63    // 23.2.3.9
64    static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv);
65    // 23.2.3.10
66    static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
67
68    // Excluding the '@@' internal properties
69    static Span<const base::BuiltinFunctionEntry> GetSetPrototypeFunctions()
70    {
71        return Span<const base::BuiltinFunctionEntry>(SET_PROTOTYPE_FUNCTIONS);
72    }
73
74    static size_t GetNumPrototypeInlinedProperties()
75    {
76        // 5 : 5 more inline properties in Set.prototype
77        //   (1) Set.prototype.constructor
78        //   (2) Set.prototype [ @@toStringTag ]
79        //   (3) Set.prototype [ @@iterator ]
80        //   (4) get Set.prototype.size
81        //   (5) Set.prototype.keys, which is not included in BuiltinsSet::GetSetPrototypeFunctions()
82        return GetSetPrototypeFunctions().Size() + 5;
83    }
84
85private:
86#define BUILTIN_SET_FUNCTION_ENTRY(name, func, length, id) \
87    base::BuiltinFunctionEntry::Create(name, BuiltinsSet::func, length, kungfu::BuiltinsStubCSigns::id),
88
89    static constexpr std::array SET_PROTOTYPE_FUNCTIONS = {
90        BUILTIN_SET_PROTOTYPE_FUNCTIONS(BUILTIN_SET_FUNCTION_ENTRY)
91    };
92
93#undef BUILTIN_SET_FUNCTION_ENTRY
94};
95}  // namespace panda::ecmascript::builtins
96#endif  // ECMASCRIPT_BUILTINS_BUILTINS_SET_H
97