1 /* 2 * Copyright (c) 2021 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_MEM_SLOTS_H 17 #define ECMASCRIPT_MEM_SLOTS_H 18 19 #include "ecmascript/js_tagged_value.h" 20 #include "ecmascript/mem/mem.h" 21 22 namespace panda::ecmascript { 23 enum class SlotStatus : bool { 24 KEEP_SLOT, 25 CLEAR_SLOT, 26 }; 27 28 class ObjectSlot { 29 public: ObjectSlot(uintptr_t slotAddr)30 explicit ObjectSlot(uintptr_t slotAddr) : slotAddress_(slotAddr) {} 31 ~ObjectSlot() = default; 32 33 DEFAULT_COPY_SEMANTIC(ObjectSlot); 34 DEFAULT_MOVE_SEMANTIC(ObjectSlot); 35 Update(TaggedObject * header)36 void Update(TaggedObject *header) 37 { 38 Update(static_cast<JSTaggedType>(ToUintPtr(header))); 39 } 40 Update(JSTaggedType value)41 void Update(JSTaggedType value) 42 { 43 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 44 *reinterpret_cast<JSTaggedType *>(slotAddress_) = value; 45 } 46 UpdateWeak(uintptr_t value)47 void UpdateWeak(uintptr_t value) 48 { 49 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 50 *reinterpret_cast<JSTaggedType *>(slotAddress_) = value | JSTaggedValue::TAG_WEAK; 51 } 52 Clear()53 void Clear() 54 { 55 *reinterpret_cast<JSTaggedType *>(slotAddress_) = JSTaggedValue::VALUE_UNDEFINED; 56 } 57 GetTaggedObject()58 TaggedObject *GetTaggedObject() const 59 { 60 return reinterpret_cast<TaggedObject *>(GetTaggedType()); 61 } 62 GetTaggedType()63 JSTaggedType GetTaggedType() const 64 { 65 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 66 return *reinterpret_cast<JSTaggedType *>(slotAddress_); 67 } 68 69 ObjectSlot &operator++() 70 { 71 slotAddress_ += sizeof(JSTaggedType); 72 return *this; 73 } 74 75 // NOLINTNEXTLINE(cert-dcl21-cpp) 76 ObjectSlot operator++(int) 77 { 78 ObjectSlot ret = *this; 79 slotAddress_ += sizeof(JSTaggedType); 80 return ret; 81 } 82 83 ObjectSlot operator+=(size_t length) 84 { 85 ObjectSlot ret = *this; 86 slotAddress_ += sizeof(JSTaggedType) * length; 87 return ret; 88 } 89 SlotAddress()90 uintptr_t SlotAddress() const 91 { 92 return slotAddress_; 93 } 94 95 bool operator<(const ObjectSlot &other) const 96 { 97 return slotAddress_ < other.slotAddress_; 98 } 99 bool operator<=(const ObjectSlot &other) const 100 { 101 return slotAddress_ <= other.slotAddress_; 102 } 103 bool operator>(const ObjectSlot &other) const 104 { 105 return slotAddress_ > other.slotAddress_; 106 } 107 bool operator>=(const ObjectSlot &other) const 108 { 109 return slotAddress_ >= other.slotAddress_; 110 } 111 bool operator==(const ObjectSlot &other) const 112 { 113 return slotAddress_ == other.slotAddress_; 114 } 115 bool operator!=(const ObjectSlot &other) const 116 { 117 return slotAddress_ != other.slotAddress_; 118 } 119 120 private: 121 uintptr_t slotAddress_; 122 }; 123 } // namespace panda::ecmascript 124 125 #endif // ECMASCRIPT_MEM_SLOTS_H 126