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_LINKEDLIST_H 17 #define ECMASCRIPT_JS_API_JS_API_LINKEDLIST_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 #include "ecmascript/tagged_list.h" 22 23 namespace panda::ecmascript { 24 class JSAPILinkedList : public JSObject { 25 public: 26 static constexpr uint32_t DEFAULT_CAPACITY_LENGTH = 10; Cast(TaggedObject * object)27 static JSAPILinkedList *Cast(TaggedObject *object) 28 { 29 ASSERT(JSTaggedValue(object).IsJSAPILinkedList()); 30 return static_cast<JSAPILinkedList *>(object); 31 } 32 33 static void Add(JSThread *thread, const JSHandle<JSAPILinkedList> &list, const JSHandle<JSTaggedValue> &value); 34 static JSHandle<JSAPILinkedList> Clone(JSThread *thread, const JSHandle<JSAPILinkedList> &list); 35 static void AddFirst(JSThread *thread, const JSHandle<JSAPILinkedList> &list, const JSHandle<JSTaggedValue> &value); 36 static JSTaggedValue Insert(JSThread *thread, const JSHandle<JSAPILinkedList> &list, 37 const JSHandle<JSTaggedValue> &value, const int index); 38 static JSTaggedValue Set(JSThread *thread, const JSHandle<JSAPILinkedList> &list, 39 const int index, const JSHandle<JSTaggedValue> &value); 40 static JSTaggedValue ConvertToArray(const JSThread *thread, const JSHandle<JSAPILinkedList> &list); 41 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPILinkedList> &list); 42 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPILinkedList> &list, 43 const JSHandle<JSTaggedValue> &key); 44 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPILinkedList> &list, 45 const JSHandle<JSTaggedValue> &key); 46 static bool SetProperty(JSThread *thread, const JSHandle<JSAPILinkedList> &obj, 47 const JSHandle<JSTaggedValue> &key, 48 const JSHandle<JSTaggedValue> &value); 49 static JSTaggedValue RemoveFirst(JSThread *thread, const JSHandle<JSAPILinkedList> &list); 50 static JSTaggedValue RemoveLast(JSThread *thread, const JSHandle<JSAPILinkedList> &list); 51 static JSTaggedValue RemoveByIndex(JSThread *thread, JSHandle<JSAPILinkedList> &list, const int index); 52 static JSTaggedValue RemoveFirstFound(JSThread *thread, JSHandle<JSAPILinkedList> &list, 53 const JSTaggedValue &element); 54 static JSTaggedValue RemoveLastFound(JSThread *thread, JSHandle<JSAPILinkedList> &list, 55 const JSTaggedValue &element); 56 void Clear(JSThread *thread); 57 bool Has(const JSTaggedValue &element); 58 JSTaggedValue GetFirst(); 59 JSTaggedValue GetLast(); 60 JSTaggedValue Get(const int index); 61 JSTaggedValue Remove(JSThread *thread, const JSTaggedValue &element); 62 JSTaggedValue GetIndexOf(const JSTaggedValue &element); 63 JSTaggedValue GetLastIndexOf(const JSTaggedValue &element); Length()64 inline uint32_t Length() 65 { 66 return TaggedDoubleList::Cast(GetDoubleList().GetTaggedObject())->Length(); 67 } 68 static constexpr size_t DOUBLE_LIST_OFFSET = JSObject::SIZE; 69 ACCESSORS(DoubleList, DOUBLE_LIST_OFFSET, SIZE); 70 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, DOUBLE_LIST_OFFSET, SIZE) 71 DECL_DUMP() 72 }; 73 } // namespace panda::ecmascript 74 75 #endif // ECMASCRIPT_JS_API_JS_API_LinkedLIST_H 76