• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JS_APIARRAYLIST_H
17 #define ECMASCRIPT_JS_APIARRAYLIST_H
18 
19 #include "js_object.h"
20 #include "js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
23 /**
24  * Provide the object of non ECMA standard jsapi container.
25  * JSAPIArrayList provides dynamically modified array.
26  * */
27 class JSAPIArrayList : public JSObject {
28 public:
29     static constexpr int DEFAULT_CAPACITY_LENGTH = 10;
Cast(ObjectHeader * object)30     static JSAPIArrayList *Cast(ObjectHeader *object)
31     {
32         ASSERT(JSTaggedValue(object).IsJSAPIArrayList());
33         return static_cast<JSAPIArrayList *>(object);
34     }
35 
36     static bool Add(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList, const JSHandle<JSTaggedValue> &value);
37     static void Insert(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
38                        const JSHandle<JSTaggedValue> &value, const int &index);
39     static void Clear(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
40     static JSHandle<JSAPIArrayList> Clone(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
41     static uint32_t GetCapacity(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
42     static void IncreaseCapacityTo(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
43                                    int capacity);
44     static void TrimToCurrentLength(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList);
45     static bool IsEmpty(const JSHandle<JSAPIArrayList> &arrayList);
46     static int GetIndexOf(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
47                           const JSHandle<JSTaggedValue> &value);
48     static int GetLastIndexOf(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
49                               const JSHandle<JSTaggedValue> &value);
50     static bool RemoveByIndex(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList, int value);
51     static bool Remove(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
52                        const JSHandle<JSTaggedValue> &value);
53     static JSTaggedValue RemoveByRange(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
54                                        const JSHandle<JSTaggedValue> &value1, const JSHandle<JSTaggedValue> &value2);
55     static JSTaggedValue ReplaceAllElements(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle,
56                                             const JSHandle<JSTaggedValue> &callbackFn,
57                                             const JSHandle<JSTaggedValue> &thisArg);
58     static JSHandle<JSAPIArrayList> SubArrayList(JSThread *thread, const JSHandle<JSAPIArrayList> &arrayList,
59                                               const JSHandle<JSTaggedValue> &value1,
60                                               const JSHandle<JSTaggedValue> &value2);
61     static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle,
62                                  const JSHandle<JSTaggedValue> &callbackFn,
63                                  const JSHandle<JSTaggedValue> &thisArg);
64 
65     static JSTaggedValue GetIteratorObj(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
66 
67     JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value);
68     JSTaggedValue Get(JSThread *thread, const uint32_t index);
69 
70     bool Has(JSTaggedValue value) const;
71     static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
72     static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
73                                const JSHandle<JSTaggedValue> &key, PropertyDescriptor &desc);
GetSize()74     inline int GetSize() const
75     {
76         return GetLength().GetArrayLength();
77     }
78 
79     static constexpr size_t LENGTH_OFFSET = JSObject::SIZE;
80     ACCESSORS(Length, LENGTH_OFFSET, SIZE);
81 
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,LENGTH_OFFSET,SIZE)82     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LENGTH_OFFSET, SIZE)
83     DECL_DUMP()
84 private:
85     inline static uint32_t ComputeCapacity(uint32_t oldCapacity)
86     {
87         uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U);
88         return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH;
89     }
90     static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
91                                               uint32_t capacity);
92 };
93 }  // namespace panda::ecmascript
94 
95 #endif  // ECMASCRIPT_JS_APIARRAYLIST_H
96