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_QUEUE_H 17 #define ECMASCRIPT_JS_API_QUEUE_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 22 namespace panda::ecmascript { 23 class JSAPIQueue : public JSObject { 24 public: 25 static constexpr int DEFAULT_CAPACITY_LENGTH = 8; Cast(TaggedObject * object)26 static JSAPIQueue *Cast(TaggedObject *object) 27 { 28 ASSERT(JSTaggedValue(object).IsJSAPIQueue()); 29 return static_cast<JSAPIQueue *>(object); 30 } 31 32 static void Add(JSThread *thread, const JSHandle<JSAPIQueue> &queue, const JSHandle<JSTaggedValue> &value); 33 static JSTaggedValue GetFirst(JSThread *thread, const JSHandle<JSAPIQueue> &queue); 34 static JSTaggedValue Pop(JSThread *thread, const JSHandle<JSAPIQueue> &queue); 35 static void ForEach(JSThread *thread, const JSHandle<JSAPIQueue> &queue, const JSHandle<JSTaggedValue> &value); 36 JSTaggedValue Get(JSThread *thread, const uint32_t index); 37 38 JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value); 39 bool Has(JSTaggedValue value) const; 40 41 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIQueue> &obj); 42 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, const JSHandle<JSTaggedValue> &key); 43 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, 44 const JSHandle<JSTaggedValue> &key); 45 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, 46 const JSHandle<JSTaggedValue> &key, 47 const JSHandle<JSTaggedValue> &value); 48 GetSize()49 inline uint32_t GetSize() const 50 { 51 return GetLength().GetArrayLength(); 52 } 53 GetCurrentFront()54 inline uint32_t GetCurrentFront() const 55 { 56 return GetFront(); 57 } 58 GetCurrentTail()59 inline uint32_t GetCurrentTail() const 60 { 61 return GetTail(); 62 } 63 64 static constexpr size_t LENGTH_OFFSET = JSObject::SIZE; 65 ACCESSORS(Length, LENGTH_OFFSET, FRONT_OFFSET); 66 ACCESSORS_PRIMITIVE_FIELD(Front, uint32_t, FRONT_OFFSET, TAIL_OFFSET) 67 ACCESSORS_PRIMITIVE_FIELD(Tail, uint32_t, TAIL_OFFSET, LAST_OFFSET) 68 DEFINE_ALIGN_SIZE(LAST_OFFSET); 69 70 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LENGTH_OFFSET, FRONT_OFFSET) 71 DECL_DUMP() 72 73 static uint32_t GetArrayLength(JSThread *thread, const JSHandle<JSAPIQueue> &queue); 74 75 uint32_t GetNextPosition(uint32_t currentPosition); 76 77 private: ComputeCapacity(uint32_t oldCapacity)78 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 79 { 80 uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U); 81 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 82 } 83 static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIQueue> &obj, 84 uint32_t capacity); 85 }; 86 } // namespace panda::ecmascript 87 88 #endif // ECMASCRIPT_JS_API_QUEUE_H 89