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_STACK_H 17 #define ECMASCRIPT_JS_API_JS_API_STACK_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 22 namespace panda::ecmascript { 23 class JSAPIStack : public JSObject { 24 public: 25 static constexpr uint32_t DEFAULT_CAPACITY_LENGTH = 10; Cast(TaggedObject * object)26 static JSAPIStack *Cast(TaggedObject *object) 27 { 28 ASSERT(JSTaggedValue(object).IsJSAPIStack()); 29 return static_cast<JSAPIStack *>(object); 30 } 31 32 static JSTaggedValue Push(JSThread *thread, const JSHandle<JSAPIStack> &stack, 33 const JSHandle<JSTaggedValue> &value); 34 35 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj); 36 37 static JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj); 38 39 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj, const JSHandle<JSTaggedValue> &key); 40 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj, 41 const JSHandle<JSTaggedValue> &key); 42 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj, 43 const JSHandle<JSTaggedValue> &key, 44 const JSHandle<JSTaggedValue> &value); 45 bool Empty(); 46 47 bool Has(JSTaggedValue value) const; 48 49 int Search(const JSHandle<JSTaggedValue> &value); 50 GetSize()51 inline uint32_t GetSize() const 52 { 53 return GetTop(); 54 } 55 56 JSTaggedValue Peek(); 57 58 JSTaggedValue Pop(); 59 60 JSTaggedValue Get(const uint32_t index); 61 62 JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value); 63 64 static constexpr size_t TOP_OFFSET = JSObject::SIZE; 65 ACCESSORS_PRIMITIVE_FIELD(Top, int, TOP_OFFSET, LAST_OFFSET) 66 DEFINE_ALIGN_SIZE(LAST_OFFSET); 67 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,LAST_OFFSET,LAST_OFFSET)68 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LAST_OFFSET, LAST_OFFSET) 69 DECL_DUMP() 70 private: 71 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 72 { 73 uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U); 74 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 75 } 76 static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIStack> &obj, 77 uint32_t capacity); 78 }; 79 } // namespace panda::ecmascript 80 81 #endif // ECMASCRIPT_JS_API_JS_API_STACK_H 82