1/*
2 * Copyright (c) 2022-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_CONTAINERS_CONTAINERS_PRIVATE_H
17#define ECMASCRIPT_CONTAINERS_CONTAINERS_PRIVATE_H
18
19#include "ecmascript/base/builtins_base.h"
20
21#include "ecmascript/interpreter/interpreter.h"
22#include "ecmascript/js_function.h"
23namespace panda::ecmascript::containers {
24using InitializeFunction = JSHandle<JSTaggedValue> (*)(JSThread *);
25enum FuncLength : uint8_t { ZERO = 0, ONE, TWO, THREE, FOUR };
26enum ContainerTag : uint8_t {
27    ArrayList = 0,
28    Queue,
29    Deque,
30    Stack,
31    Vector,
32    List,
33    LinkedList,
34    TreeMap,
35    TreeSet,
36    HashMap,
37    HashSet,
38    LightWeightMap,
39    LightWeightSet,
40    PlainArray,
41    BitVector,
42    END
43};
44
45// Using Lazy-loading container, including ArrayList, Queue, Stack, Vector, List, LinkedList, Deque,
46// TreeMap, TreeSet, HashMap, HashSet, LightWeightMap, LightWeightSet, PlainArray.
47// Use through ArkPrivate.Load([ContainerTag]) in js, ContainTag was declaerd in ArkPrivate like ArkPrivate::ArrayList.
48class ContainersPrivate : public base::BuiltinsBase {
49public:
50    static JSTaggedValue Load(EcmaRuntimeCallInfo *msg);
51
52private:
53    static JSHandle<JSFunction> NewContainerConstructor(JSThread *thread, const JSHandle<JSObject> &prototype,
54                                                        EcmaEntrypoint ctorFunc, const char *name, int length);
55    static JSHandle<JSFunction> NewFunction(JSThread *thread, const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func,
56                                            int length, kungfu::BuiltinsStubCSigns::ID builtinId =
57                                            kungfu::BuiltinsStubCSigns::INVALID);
58    static void SetFrozenFunction(JSThread *thread, const JSHandle<JSObject> &obj, const char *key, EcmaEntrypoint func,
59                                  int length, kungfu::BuiltinsStubCSigns::ID builtinId =
60                                  kungfu::BuiltinsStubCSigns::INVALID);
61    static void SetFrozenConstructor(JSThread *thread, const JSHandle<JSObject> &obj, const char *keyChar,
62                                     JSHandle<JSTaggedValue> &value);
63    static JSHandle<JSTaggedValue> CreateGetter(JSThread *thread, EcmaEntrypoint func, const char *name,
64                                                int length);
65    static void SetGetter(JSThread *thread, const JSHandle<JSObject> &obj,
66                          const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &getter);
67    static void SetFunctionAtSymbol(JSThread *thread, const JSHandle<GlobalEnv> &env,
68                                    const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &symbol,
69                                    const char *name, EcmaEntrypoint func, int length);
70    static void SetStringTagSymbol(JSThread *thread, const JSHandle<GlobalEnv> &env,
71                                   const JSHandle<JSObject> &obj, const char *key);
72
73    static JSTaggedValue InitializeContainer(JSThread *thread, const JSHandle<JSObject> &obj, InitializeFunction func,
74                                             const char *name);
75    static JSHandle<JSTaggedValue> InitializeArrayList(JSThread *thread);
76    static void InitializeArrayListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
77                                            GlobalEnvConstants *globalConst);
78    static JSHandle<JSTaggedValue> InitializeHashMap(JSThread *thread);
79    static void InitializeHashMapIterator(JSThread *thread);
80    static JSHandle<JSTaggedValue> InitializeHashSet(JSThread *thread);
81    static void InitializeHashSetIterator(JSThread *thread);
82    static JSHandle<JSTaggedValue> InitializeLightWeightMap(JSThread *thread);
83    static void InitializeLightWeightMapIterator(JSThread *thread);
84    static JSHandle<JSTaggedValue> InitializeLightWeightSet(JSThread *thread);
85    static void InitializeLightWeightSetIterator(JSThread *thread);
86    static JSHandle<JSTaggedValue> InitializeTreeMap(JSThread *thread);
87    static void InitializeTreeMapIterator(JSThread *thread);
88    static JSHandle<JSTaggedValue> InitializeTreeSet(JSThread *thread);
89    static void InitializeTreeSetIterator(JSThread *thread);
90    static JSHandle<JSTaggedValue> InitializePlainArray(JSThread *thread);
91    static void InitializePlainArrayIterator(JSThread *thread);
92    static JSHandle<JSTaggedValue> InitializeVector(JSThread *thread);
93    static void InitializeVectorIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
94                                         GlobalEnvConstants *globalConst);
95    static JSHandle<JSTaggedValue> InitializeBitVector(JSThread *thread);
96    static void InitializeBitVectorIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
97                                         GlobalEnvConstants *globalConst);
98    static JSHandle<JSTaggedValue> InitializeQueue(JSThread *thread);
99    static void InitializeQueueIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
100                                        GlobalEnvConstants *globalConst);
101    static JSHandle<JSTaggedValue> InitializeDeque(JSThread *thread);
102    static void InitializeDequeIterator(JSThread *thread,  const JSHandle<GlobalEnv> &env,
103                                        GlobalEnvConstants *globalConst);
104    static JSHandle<JSTaggedValue> InitializeStack(JSThread *thread);
105    static void InitializeStackIterator(JSThread *thread, GlobalEnvConstants *globalConst);
106    static JSHandle<JSTaggedValue> InitializeList(JSThread *thread);
107    static JSHandle<JSTaggedValue> InitializeLinkedList(JSThread *thread);
108    static void InitializeLinkedListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env);
109    static void InitializeListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env);
110};
111}  // namespace panda::ecmascript::containers
112
113#endif  // ECMASCRIPT_CONTAINERS_CONTAINERS_PRIVATE_H
114