• 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 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 
Clear()47     void Clear()
48     {
49         *reinterpret_cast<JSTaggedType *>(slotAddress_) = JSTaggedValue::VALUE_UNDEFINED;
50     }
51 
GetTaggedObject()52     TaggedObject *GetTaggedObject() const
53     {
54         return reinterpret_cast<TaggedObject *>(GetTaggedType());
55     }
56 
GetTaggedType()57     JSTaggedType GetTaggedType() const
58     {
59         // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
60         return *reinterpret_cast<JSTaggedType *>(slotAddress_);
61     }
62 
63     ObjectSlot &operator++()
64     {
65         slotAddress_ += sizeof(JSTaggedType);
66         return *this;
67     }
68 
69     // NOLINTNEXTLINE(cert-dcl21-cpp)
70     ObjectSlot operator++(int)
71     {
72         ObjectSlot ret = *this;
73         slotAddress_ += sizeof(JSTaggedType);
74         return ret;
75     }
76 
SlotAddress()77     uintptr_t SlotAddress() const
78     {
79         return slotAddress_;
80     }
81 
82     bool operator<(const ObjectSlot &other) const
83     {
84         return slotAddress_ < other.slotAddress_;
85     }
86     bool operator<=(const ObjectSlot &other) const
87     {
88         return slotAddress_ <= other.slotAddress_;
89     }
90     bool operator>(const ObjectSlot &other) const
91     {
92         return slotAddress_ > other.slotAddress_;
93     }
94     bool operator>=(const ObjectSlot &other) const
95     {
96         return slotAddress_ >= other.slotAddress_;
97     }
98     bool operator==(const ObjectSlot &other) const
99     {
100         return slotAddress_ == other.slotAddress_;
101     }
102     bool operator!=(const ObjectSlot &other) const
103     {
104         return slotAddress_ != other.slotAddress_;
105     }
106 
107 private:
108     uintptr_t slotAddress_;
109 };
110 }  // namespace panda::ecmascript
111 
112 #endif  // ECMASCRIPT_MEM_SLOTS_H
113