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_SHAREAD_MAP_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_SHAREAD_MAP_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/ecma_runtime_call_info.h"
21 
22 // List of functions in Map.prototype, excluding the constructor and '@@' properties.
23 // V(name, func, length, stubIndex)
24 // where BuiltinsMap::func refers to the native implementation of Map.prototype[name].
25 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
26 #define BUILTIN_MAP_PROTOTYPE_FUNCTIONS(V)                      \
27     /* Map.prototype.clear ( ) */                               \
28     V("clear",   Clear,   0, MapClear)                          \
29     /* Map.prototype.delete ( key ) */                          \
30     V("delete",  Delete,  1, MapDelete)                         \
31     /* Map.prototype.entries ( ) */                             \
32     V("entries", Entries, 0, MapEntries)                        \
33     /* Map.prototype.forEach ( callbackfn [ , thisArg ] ) */    \
34     V("forEach", ForEach, 1, MapForEach)                        \
35     /* Map.prototype.get ( key ) */                             \
36     V("get",     Get,     1, INVALID)                           \
37     /* Map.prototype.has ( key ) */                             \
38     V("has",     Has,     1, MapHas)                            \
39     /* Map.prototype.keys ( ) */                                \
40     V("keys",    Keys,    0, MapKeys)                           \
41     /* Map.prototype.set ( key, value ) */                      \
42     V("set",     Set,     2, MapSet)                            \
43     /* Map.prototype.values ( ) */                              \
44     V("values",  Values,  0, MapValues)
45 
46 namespace panda::ecmascript::builtins {
47 class BuiltinsSharedMap : public base::BuiltinsBase {
48 public:
49     static JSTaggedValue Constructor(EcmaRuntimeCallInfo *argv);
50     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
51     static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
52     static JSTaggedValue Delete(EcmaRuntimeCallInfo *argv);
53     static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
54     static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
55     static JSTaggedValue Get(EcmaRuntimeCallInfo *argv);
56     static JSTaggedValue Has(EcmaRuntimeCallInfo *argv);
57     static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv);
58     static JSTaggedValue Set(EcmaRuntimeCallInfo *argv);
59     static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv);
60     static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
61     static JSTaggedValue AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
62                                                 const JSHandle<JSTaggedValue> &iterable,
63                                                 const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory);
64 
65     // Excluding the constructor and '@@' internal properties.
GetMapPrototypeFunctions()66     static Span<const base::BuiltinFunctionEntry> GetMapPrototypeFunctions()
67     {
68         return Span<const base::BuiltinFunctionEntry>(MAP_PROTOTYPE_FUNCTIONS);
69     }
70 
GetNumPrototypeInlinedProperties()71     static size_t GetNumPrototypeInlinedProperties()
72     {
73         // 4 : 4 more inline properties in Map.prototype
74         //   (1) Map.prototype.constructor
75         //   (2) Map.prototype [ @@toStringTag ]
76         //   (3) Map.prototype [ @@iterator ]
77         //   (4) get Map.prototype.size
78         return GetMapPrototypeFunctions().Size() + 4;
79     }
80 
GetPrototypeProperties()81     static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties()
82     {
83         return Span<const std::pair<std::string_view, bool>>(MAP_PROTOTYPE_PROPERTIES);
84     }
85 
GetFunctionProperties()86     static Span<const std::pair<std::string_view, bool>> GetFunctionProperties()
87     {
88         return Span<const std::pair<std::string_view, bool>>(MAP_FUNCTION_PROPERTIES);
89     }
90 private:
91 #define BUILTIN_MAP_FUNCTION_ENTRY(name, func, length, id) \
92     base::BuiltinFunctionEntry::Create(name, BuiltinsSharedMap::func, length, kungfu::BuiltinsStubCSigns::id),
93 
94     static constexpr std::array MAP_PROTOTYPE_FUNCTIONS = {
95         BUILTIN_MAP_PROTOTYPE_FUNCTIONS(BUILTIN_MAP_FUNCTION_ENTRY)
96     };
97 
98 #undef BUILTIN_MAP_FUNCTION_ENTRY
99 
100 #define MAP_PROPERTIES_PAIR(name, func, length, id) \
101     std::pair<std::string_view, bool>(name, false),
102 
103     static constexpr std::array MAP_PROTOTYPE_PROPERTIES = {
104         std::pair<std::string_view, bool>("constructor", false),
105         BUILTIN_MAP_PROTOTYPE_FUNCTIONS(MAP_PROPERTIES_PAIR)
106         std::pair<std::string_view, bool>("[Symbol.toStringTag]", false),
107         std::pair<std::string_view, bool>("size", true),
108         std::pair<std::string_view, bool>("[Symbol.iterator]", false)
109     };
110 
111     static constexpr std::array MAP_FUNCTION_PROPERTIES = {
112         std::pair<std::string_view, bool>("length", false),
113         std::pair<std::string_view, bool>("name", false),
114         std::pair<std::string_view, bool>("prototype", false),
115         std::pair<std::string_view, bool>("[Symbol.species]", true),
116     };
117 #undef MAP_PROPERTIES_PAIR
118 };
119 }  // namespace panda::ecmascript::builtins
120 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_SHAREAD_MAP_H
121