14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SHAREAD_MAP_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_SHAREAD_MAP_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ci// List of functions in Map.prototype, excluding the constructor and '@@' properties.
234514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
244514f5e3Sopenharmony_ci// where BuiltinsMap::func refers to the native implementation of Map.prototype[name].
254514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
264514f5e3Sopenharmony_ci#define BUILTIN_MAP_PROTOTYPE_FUNCTIONS(V)                      \
274514f5e3Sopenharmony_ci    /* Map.prototype.clear ( ) */                               \
284514f5e3Sopenharmony_ci    V("clear",   Clear,   0, MapClear)                          \
294514f5e3Sopenharmony_ci    /* Map.prototype.delete ( key ) */                          \
304514f5e3Sopenharmony_ci    V("delete",  Delete,  1, MapDelete)                         \
314514f5e3Sopenharmony_ci    /* Map.prototype.entries ( ) */                             \
324514f5e3Sopenharmony_ci    V("entries", Entries, 0, MapEntries)                        \
334514f5e3Sopenharmony_ci    /* Map.prototype.forEach ( callbackfn [ , thisArg ] ) */    \
344514f5e3Sopenharmony_ci    V("forEach", ForEach, 1, MapForEach)                        \
354514f5e3Sopenharmony_ci    /* Map.prototype.get ( key ) */                             \
364514f5e3Sopenharmony_ci    V("get",     Get,     1, INVALID)                           \
374514f5e3Sopenharmony_ci    /* Map.prototype.has ( key ) */                             \
384514f5e3Sopenharmony_ci    V("has",     Has,     1, MapHas)                            \
394514f5e3Sopenharmony_ci    /* Map.prototype.keys ( ) */                                \
404514f5e3Sopenharmony_ci    V("keys",    Keys,    0, MapKeys)                           \
414514f5e3Sopenharmony_ci    /* Map.prototype.set ( key, value ) */                      \
424514f5e3Sopenharmony_ci    V("set",     Set,     2, MapSet)                            \
434514f5e3Sopenharmony_ci    /* Map.prototype.values ( ) */                              \
444514f5e3Sopenharmony_ci    V("values",  Values,  0, MapValues)
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
474514f5e3Sopenharmony_ciclass BuiltinsSharedMap : public base::BuiltinsBase {
484514f5e3Sopenharmony_cipublic:
494514f5e3Sopenharmony_ci    static JSTaggedValue Constructor(EcmaRuntimeCallInfo *argv);
504514f5e3Sopenharmony_ci    static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
514514f5e3Sopenharmony_ci    static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
524514f5e3Sopenharmony_ci    static JSTaggedValue Delete(EcmaRuntimeCallInfo *argv);
534514f5e3Sopenharmony_ci    static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
544514f5e3Sopenharmony_ci    static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
554514f5e3Sopenharmony_ci    static JSTaggedValue Get(EcmaRuntimeCallInfo *argv);
564514f5e3Sopenharmony_ci    static JSTaggedValue Has(EcmaRuntimeCallInfo *argv);
574514f5e3Sopenharmony_ci    static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv);
584514f5e3Sopenharmony_ci    static JSTaggedValue Set(EcmaRuntimeCallInfo *argv);
594514f5e3Sopenharmony_ci    static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv);
604514f5e3Sopenharmony_ci    static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
614514f5e3Sopenharmony_ci    static JSTaggedValue AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
624514f5e3Sopenharmony_ci                                                const JSHandle<JSTaggedValue> &iterable,
634514f5e3Sopenharmony_ci                                                const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory);
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    // Excluding the constructor and '@@' internal properties.
664514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetMapPrototypeFunctions()
674514f5e3Sopenharmony_ci    {
684514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(MAP_PROTOTYPE_FUNCTIONS);
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    static size_t GetNumPrototypeInlinedProperties()
724514f5e3Sopenharmony_ci    {
734514f5e3Sopenharmony_ci        // 4 : 4 more inline properties in Map.prototype
744514f5e3Sopenharmony_ci        //   (1) Map.prototype.constructor
754514f5e3Sopenharmony_ci        //   (2) Map.prototype [ @@toStringTag ]
764514f5e3Sopenharmony_ci        //   (3) Map.prototype [ @@iterator ]
774514f5e3Sopenharmony_ci        //   (4) get Map.prototype.size
784514f5e3Sopenharmony_ci        return GetMapPrototypeFunctions().Size() + 4;
794514f5e3Sopenharmony_ci    }
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties()
824514f5e3Sopenharmony_ci    {
834514f5e3Sopenharmony_ci        return Span<const std::pair<std::string_view, bool>>(MAP_PROTOTYPE_PROPERTIES);
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    static Span<const std::pair<std::string_view, bool>> GetFunctionProperties()
874514f5e3Sopenharmony_ci    {
884514f5e3Sopenharmony_ci        return Span<const std::pair<std::string_view, bool>>(MAP_FUNCTION_PROPERTIES);
894514f5e3Sopenharmony_ci    }
904514f5e3Sopenharmony_ciprivate:
914514f5e3Sopenharmony_ci#define BUILTIN_MAP_FUNCTION_ENTRY(name, func, length, id) \
924514f5e3Sopenharmony_ci    base::BuiltinFunctionEntry::Create(name, BuiltinsSharedMap::func, length, kungfu::BuiltinsStubCSigns::id),
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    static constexpr std::array MAP_PROTOTYPE_FUNCTIONS = {
954514f5e3Sopenharmony_ci        BUILTIN_MAP_PROTOTYPE_FUNCTIONS(BUILTIN_MAP_FUNCTION_ENTRY)
964514f5e3Sopenharmony_ci    };
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci#undef BUILTIN_MAP_FUNCTION_ENTRY
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci#define MAP_PROPERTIES_PAIR(name, func, length, id) \
1014514f5e3Sopenharmony_ci    std::pair<std::string_view, bool>(name, false),
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ci    static constexpr std::array MAP_PROTOTYPE_PROPERTIES = {
1044514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("constructor", false),
1054514f5e3Sopenharmony_ci        BUILTIN_MAP_PROTOTYPE_FUNCTIONS(MAP_PROPERTIES_PAIR)
1064514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("[Symbol.toStringTag]", false),
1074514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("size", true),
1084514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("[Symbol.iterator]", false)
1094514f5e3Sopenharmony_ci    };
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ci    static constexpr std::array MAP_FUNCTION_PROPERTIES = {
1124514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("length", false),
1134514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("name", false),
1144514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("prototype", false),
1154514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("[Symbol.species]", true),
1164514f5e3Sopenharmony_ci    };
1174514f5e3Sopenharmony_ci#undef MAP_PROPERTIES_PAIR
1184514f5e3Sopenharmony_ci};
1194514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
1204514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_SHAREAD_MAP_H
121