• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_API_JS_API_ARRAYLIST_H
17 #define ECMASCRIPT_JS_API_JS_API_ARRAYLIST_H
18 
19 #include "ecmascript/js_object.h"
20 #include "ecmascript/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 uint32_t DEFAULT_CAPACITY_LENGTH = 10;
Cast(TaggedObject * object)30     static JSAPIArrayList *Cast(TaggedObject *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 JSTaggedValue 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 JSTaggedValue 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(const JSTaggedValue value) const;
71     static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
72     static JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIArrayList> &obj);
73     static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
74                                const JSHandle<JSTaggedValue> &key);
75     static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
76                                        const JSHandle<JSTaggedValue> &key);
77     static bool SetProperty(JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
78                             const JSHandle<JSTaggedValue> &key,
79                             const JSHandle<JSTaggedValue> &value);
GetSize()80     inline uint32_t GetSize() const
81     {
82         return GetLength().GetInt();
83     }
84 
85     static constexpr size_t LENGTH_OFFSET = JSObject::SIZE;
86     ACCESSORS(Length, LENGTH_OFFSET, SIZE);
87 
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,LENGTH_OFFSET,SIZE)88     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LENGTH_OFFSET, SIZE)
89     DECL_DUMP()
90 
91 private:
92     inline static uint32_t ComputeCapacity(uint32_t oldCapacity)
93     {
94         uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U);
95         return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH;
96     }
97     static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIArrayList> &obj,
98                                               uint32_t capacity);
99 };
100 }  // namespace panda::ecmascript
101 
102 #endif  // ECMASCRIPT_JS_API_JS_API_ARRAYLIST_H
103