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_QUEUE_H 17 #define ECMASCRIPT_JS_API_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 uint32_t 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 JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIQueue> &obj); 43 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, const JSHandle<JSTaggedValue> &key); 44 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, 45 const JSHandle<JSTaggedValue> &key); 46 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIQueue> &obj, 47 const JSHandle<JSTaggedValue> &key, 48 const JSHandle<JSTaggedValue> &value); 49 GetSize()50 inline uint32_t GetSize() const 51 { 52 return GetLength().GetArrayLength(); 53 } 54 GetCurrentFront()55 inline uint32_t GetCurrentFront() const 56 { 57 return GetFront(); 58 } 59 GetCurrentTail()60 inline uint32_t GetCurrentTail() const 61 { 62 return GetTail(); 63 } 64 65 static constexpr size_t LENGTH_OFFSET = JSObject::SIZE; 66 ACCESSORS(Length, LENGTH_OFFSET, FRONT_OFFSET); 67 ACCESSORS_PRIMITIVE_FIELD(Front, uint32_t, FRONT_OFFSET, TAIL_OFFSET) 68 ACCESSORS_PRIMITIVE_FIELD(Tail, uint32_t, TAIL_OFFSET, LAST_OFFSET) 69 DEFINE_ALIGN_SIZE(LAST_OFFSET); 70 71 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LENGTH_OFFSET, FRONT_OFFSET) 72 DECL_DUMP() 73 74 static uint32_t GetArrayLength(JSThread *thread, const JSHandle<JSAPIQueue> &queue); 75 76 uint32_t GetNextPosition(uint32_t currentPosition); 77 78 private: ComputeCapacity(uint32_t oldCapacity)79 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 80 { 81 uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U); 82 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 83 } 84 static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIQueue> &obj, 85 uint32_t capacity); 86 }; 87 } // namespace panda::ecmascript 88 89 #endif // ECMASCRIPT_JS_API_JS_API_QUEUE_H 90