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_LIST_H 17 #define ECMASCRIPT_JS_API_JS_API_LIST_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 JSAPIList : public JSObject { 25 public: 26 static constexpr uint32_t DEFAULT_CAPACITY_LENGTH = 10; 27 static constexpr uint32_t UNUSED_BIT_FILEDS_NUM = 31; 28 using IsOrderedListBit = BitField<uint32_t, 0, UNUSED_BIT_FILEDS_NUM>::NextFlag; 29 Cast(TaggedObject * object)30 static JSAPIList *Cast(TaggedObject *object) 31 { 32 ASSERT(JSTaggedValue(object).IsJSAPIList()); 33 return static_cast<JSAPIList *>(object); 34 } 35 SetIsOrderedList(bool flag)36 inline void SetIsOrderedList(bool flag) const 37 { 38 IsOrderedListBit::Set<uint32_t>(flag, GetBitFieldAddr()); 39 } 40 IsOrderedList()41 inline bool IsOrderedList() const 42 { 43 uint32_t bits = GetBitField(); 44 return IsOrderedListBit::Decode(bits); 45 } 46 GetBitFieldAddr()47 uint32_t *GetBitFieldAddr() const 48 { 49 return reinterpret_cast<uint32_t *>(ToUintPtr(this) + BIT_FIELD_OFFSET); 50 } 51 ClearBitField()52 inline void ClearBitField() 53 { 54 SetBitField(0UL); 55 } 56 57 static void Add(JSThread *thread, const JSHandle<JSAPIList> &list, const JSHandle<JSTaggedValue> &value); 58 static JSTaggedValue Insert(JSThread *thread, const JSHandle<JSAPIList> &list, 59 const JSHandle<JSTaggedValue> &value, const int index); 60 static JSTaggedValue PUBLIC_API Set(JSThread *thread, const JSHandle<JSAPIList> &list, 61 const int index, const JSHandle<JSTaggedValue> &value); 62 static JSTaggedValue ReplaceAllElements(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 63 const JSHandle<JSTaggedValue> &callbackFn, 64 const JSHandle<JSTaggedValue> &thisArg); 65 static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 66 const JSHandle<JSTaggedValue> &callbackFn); 67 static JSTaggedValue ConvertToArray(const JSThread *thread, const JSHandle<JSAPIList> &list); 68 static JSTaggedValue GetSubList(JSThread *thread, const JSHandle<JSAPIList> &list, 69 const int fromIndex, const int toIndex); 70 static JSTaggedValue RemoveByIndex(JSThread *thread, const JSHandle<JSAPIList> &list, const int &index); 71 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIList> &list); 72 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIList> &list, 73 const JSHandle<JSTaggedValue> &key); 74 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIList> &list, 75 const JSHandle<JSTaggedValue> &key); 76 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIList> &obj, 77 const JSHandle<JSTaggedValue> &key, 78 const JSHandle<JSTaggedValue> &value); 79 static JSTaggedValue FastGet(JSThread *thread, const int index, const JSHandle<JSAPIList> &list); 80 81 JSTaggedValue GetFirst(const JSThread *thread); 82 JSTaggedValue GetLast(const JSThread *thread); 83 bool IsEmpty(const JSThread *thread); 84 JSTaggedValue Get(const JSThread *thread, const int index); 85 bool Has(const JSThread *thread, const JSTaggedValue &element); 86 JSTaggedValue GetIndexOf(const JSThread *thread, const JSTaggedValue &element); 87 JSTaggedValue GetLastIndexOf(const JSThread *thread, const JSTaggedValue &element); 88 JSTaggedValue Equal(JSThread *thread, const JSHandle<JSAPIList> &list); 89 void Clear(JSThread *thread); 90 JSTaggedValue Remove(JSThread *thread, const JSTaggedValue &element); Length(const JSThread * thread)91 inline uint32_t Length(const JSThread *thread) 92 { 93 return TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject())->Length(); 94 } 95 96 static constexpr size_t SINGLY_LIST_OFFSET = JSObject::SIZE; 97 ACCESSORS(SingleList, SINGLY_LIST_OFFSET, BIT_FIELD_OFFSET) 98 ACCESSORS_PRIMITIVE_FIELD(BitField, uint32_t, BIT_FIELD_OFFSET, LAST_OFFSET) 99 DEFINE_ALIGN_SIZE(LAST_OFFSET); 100 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, SINGLY_LIST_OFFSET, BIT_FIELD_OFFSET) 101 DECL_DUMP() 102 }; 103 } // namespace panda::ecmascript 104 105 #endif // ECMASCRIPT_JS_API_JS_API_LIST_H 106