• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class ObjectSlot {
24 public:
ObjectSlot(uintptr_t slotAddr)25     explicit ObjectSlot(uintptr_t slotAddr) : slotAddress_(slotAddr) {}
26     ~ObjectSlot() = default;
27 
28     DEFAULT_COPY_SEMANTIC(ObjectSlot);
29     DEFAULT_MOVE_SEMANTIC(ObjectSlot);
30 
Update(TaggedObject * header)31     void Update(TaggedObject *header)
32     {
33         Update(static_cast<JSTaggedType>(ToUintPtr(header)));
34     }
35 
Update(JSTaggedType value)36     void Update(JSTaggedType value)
37     {
38         // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
39         *reinterpret_cast<JSTaggedType *>(slotAddress_) = value;
40     }
41 
GetTaggedObjectHeader()42     TaggedObject *GetTaggedObjectHeader() const
43     {
44         return reinterpret_cast<TaggedObject *>(GetTaggedType());
45     }
46 
GetTaggedType()47     JSTaggedType GetTaggedType() const
48     {
49         // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
50         return *reinterpret_cast<JSTaggedType *>(slotAddress_);
51     }
52 
53     ObjectSlot &operator++()
54     {
55         slotAddress_ += sizeof(JSTaggedType);
56         return *this;
57     }
58 
59     // NOLINTNEXTLINE(cert-dcl21-cpp)
60     ObjectSlot operator++(int)
61     {
62         ObjectSlot ret = *this;
63         slotAddress_ += sizeof(JSTaggedType);
64         return ret;
65     }
66 
SlotAddress()67     uintptr_t SlotAddress() const
68     {
69         return slotAddress_;
70     }
71 
72     bool operator<(const ObjectSlot &other) const
73     {
74         return slotAddress_ < other.slotAddress_;
75     }
76     bool operator<=(const ObjectSlot &other) const
77     {
78         return slotAddress_ <= other.slotAddress_;
79     }
80     bool operator>(const ObjectSlot &other) const
81     {
82         return slotAddress_ > other.slotAddress_;
83     }
84     bool operator>=(const ObjectSlot &other) const
85     {
86         return slotAddress_ >= other.slotAddress_;
87     }
88     bool operator==(const ObjectSlot &other) const
89     {
90         return slotAddress_ == other.slotAddress_;
91     }
92     bool operator!=(const ObjectSlot &other) const
93     {
94         return slotAddress_ != other.slotAddress_;
95     }
96 
97 private:
98     uintptr_t slotAddress_;
99 };
100 }  // namespace panda::ecmascript
101 
102 #endif  // ECMASCRIPT_MEM_SLOTS_H
103