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_MAP_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_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, MapGet)                            \
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 BuiltinsMap : public base::BuiltinsBase {
48 public:
49     // 23.1.1.1
50     static JSTaggedValue MapConstructor(EcmaRuntimeCallInfo *argv);
51     // 23.1.2.2
52     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
53     // 23.1.3.1
54     static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
55     // 23.1.3.3
56     static JSTaggedValue Delete(EcmaRuntimeCallInfo *argv);
57     // 23.1.3.4
58     static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
59     // 23.1.3.5
60     static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
61     // 23.1.3.6
62     static JSTaggedValue Get(EcmaRuntimeCallInfo *argv);
63     // 23.1.3.7
64     static JSTaggedValue Has(EcmaRuntimeCallInfo *argv);
65     // 23.1.3.8
66     static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv);
67     // 23.1.3.9
68     static JSTaggedValue Set(EcmaRuntimeCallInfo *argv);
69     // 23.1.3.10
70     static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv);
71     // 23.1.3.11
72     static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
73 
74     // es12 24.1.1.2 AddEntriesFromIterable ( target, iterable, adder )
75     static JSTaggedValue AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
76                                                 const JSHandle<JSTaggedValue> &iterable,
77                                                 const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory);
78 
79     // Excluding the constructor and '@@' internal properties.
GetMapPrototypeFunctions()80     static Span<const base::BuiltinFunctionEntry> GetMapPrototypeFunctions()
81     {
82         return Span<const base::BuiltinFunctionEntry>(MAP_PROTOTYPE_FUNCTIONS);
83     }
84 
GetNumPrototypeInlinedProperties()85     static size_t GetNumPrototypeInlinedProperties()
86     {
87         // 4 : 4 more inline properties in Map.prototype
88         //   (1) Map.prototype.constructor
89         //   (2) Map.prototype [ @@toStringTag ]
90         //   (3) Map.prototype [ @@iterator ] ( )
91         //   (4) get Map.prototype.size
92         return GetMapPrototypeFunctions().Size() + 4;
93     }
94 
95 private:
96 #define BUILTIN_MAP_FUNCTION_ENTRY(name, func, length, id) \
97     base::BuiltinFunctionEntry::Create(name, BuiltinsMap::func, length, kungfu::BuiltinsStubCSigns::id),
98 
99     static constexpr std::array MAP_PROTOTYPE_FUNCTIONS = {
100         BUILTIN_MAP_PROTOTYPE_FUNCTIONS(BUILTIN_MAP_FUNCTION_ENTRY)
101     };
102 
103 #undef BUILTIN_MAP_FUNCTION_ENTRY
104 };
105 }  // namespace panda::ecmascript::builtins
106 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_MAP_H
107