1 /* 2 * Copyright (c) 2022 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_DEQUE_H 17 #define ECMASCRIPT_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 bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &key); 53 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj, 54 const JSHandle<JSTaggedValue> &key); 55 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj, 56 const JSHandle<JSTaggedValue> &key, 57 const JSHandle<JSTaggedValue> &value); 58 JSTaggedValue GetFront(); 59 60 JSTaggedValue GetTail(); 61 62 JSTaggedValue PopFirst(); 63 64 JSTaggedValue PopLast(); 65 66 JSTaggedValue Get(const uint32_t index); 67 68 JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value); 69 70 uint32_t GetSize() const; 71 72 bool Has(JSTaggedValue value) const; 73 74 static constexpr size_t FIRST_OFFSET = JSObject::SIZE; 75 ACCESSORS_PRIMITIVE_FIELD(First, uint32_t, FIRST_OFFSET, LAST_OFFSET) 76 ACCESSORS_PRIMITIVE_FIELD(Last, uint32_t, LAST_OFFSET, ENDL_OFFSET) 77 DEFINE_ALIGN_SIZE(ENDL_OFFSET); 78 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,ENDL_OFFSET,ENDL_OFFSET)79 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ENDL_OFFSET, ENDL_OFFSET) 80 DECL_DUMP() 81 private: 82 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 83 { 84 uint32_t newCapacity = oldCapacity << 1U; 85 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 86 } 87 static JSHandle<TaggedArray> GrowCapacity(JSThread *thread, const JSHandle<JSAPIDeque> &deque, 88 uint32_t oldCapacity, uint32_t first, uint32_t last); 89 bool IsEmpty(); 90 }; 91 } // namespace panda::ecmascript 92 93 #endif // ECMASCRIPT_JS_API_DEQUE_H 94