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_DEQUE_H 17 #define ECMASCRIPT_JS_API_JS_API_DEQUE_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 #include "ecmascript/ecma_macros.h" 23 #include "ecmascript/js_handle.h" 24 #include "ecmascript/js_object.h" 25 #include "ecmascript/js_tagged_value.h" 26 27 #include "libpandabase/macros.h" 28 29 namespace panda::ecmascript { 30 class JSThread; 31 class TaggedArray; 32 class TaggedObject; 33 class JSAPIDeque : public JSObject { 34 public: 35 static constexpr int DEFAULT_CAPACITY_LENGTH = 8; Cast(TaggedObject * object)36 static JSAPIDeque *Cast(TaggedObject *object) 37 { 38 ASSERT(JSTaggedValue(object).IsJSAPIDeque()); 39 return static_cast<JSAPIDeque *>(object); 40 } 41 42 static void InsertFront(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value); 43 44 static void InsertEnd(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value); 45 46 static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSAPIDeque> &deque); 47 48 static JSTaggedValue GetIteratorObj(JSThread *thread, const JSHandle<JSAPIDeque> &deque); 49 50 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque); 51 52 static JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque); 53 54 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &key); 55 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj, 56 const JSHandle<JSTaggedValue> &key); 57 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj, 58 const JSHandle<JSTaggedValue> &key, 59 const JSHandle<JSTaggedValue> &value); 60 JSTaggedValue GetFront(); 61 62 JSTaggedValue GetTail(); 63 64 JSTaggedValue PopFirst(); 65 66 JSTaggedValue PopLast(); 67 68 JSTaggedValue Get(const uint32_t index); 69 70 JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value); 71 72 uint32_t GetSize() const; 73 74 bool Has(JSTaggedValue value) const; 75 76 static constexpr size_t FIRST_OFFSET = JSObject::SIZE; 77 ACCESSORS_PRIMITIVE_FIELD(First, uint32_t, FIRST_OFFSET, LAST_OFFSET) 78 ACCESSORS_PRIMITIVE_FIELD(Last, uint32_t, LAST_OFFSET, ENDL_OFFSET) 79 DEFINE_ALIGN_SIZE(ENDL_OFFSET); 80 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,ENDL_OFFSET,ENDL_OFFSET)81 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ENDL_OFFSET, ENDL_OFFSET) 82 DECL_DUMP() 83 private: 84 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 85 { 86 uint32_t newCapacity = oldCapacity << 1U; 87 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 88 } 89 static JSHandle<TaggedArray> GrowCapacity(JSThread *thread, const JSHandle<JSAPIDeque> &deque, 90 uint32_t oldCapacity, uint32_t first, uint32_t last); 91 bool IsEmpty(); 92 }; 93 } // namespace panda::ecmascript 94 95 #endif // ECMASCRIPT_JS_API_JS_API_DEQUE_H 96