V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
maybe-object-inl.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_OBJECTS_MAYBE_OBJECT_INL_H_
6 #define V8_OBJECTS_MAYBE_OBJECT_INL_H_
7 
8 #include "src/objects/maybe-object.h"
9 
10 #include "src/objects-inl.h"
11 #include "src/objects/slots-inl.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 bool MaybeObject::ToSmi(Smi* value) {
17  if (HAS_SMI_TAG(ptr_)) {
18  *value = Smi::cast(ObjectPtr(ptr_));
19  return true;
20  }
21  return false;
22 }
23 
24 Smi MaybeObject::ToSmi() const {
25  DCHECK(HAS_SMI_TAG(ptr_));
26  return Smi::cast(ObjectPtr(ptr_));
27 }
28 
29 bool MaybeObject::IsStrongOrWeak() const {
30  if (IsSmi() || IsCleared()) {
31  return false;
32  }
33  return true;
34 }
35 
36 bool MaybeObject::GetHeapObject(HeapObject** result) const {
37  if (IsSmi() || IsCleared()) {
38  return false;
39  }
40  *result = GetHeapObject();
41  return true;
42 }
43 
44 bool MaybeObject::GetHeapObject(HeapObject** result,
45  HeapObjectReferenceType* reference_type) const {
46  if (IsSmi() || IsCleared()) {
47  return false;
48  }
49  *reference_type = HasWeakHeapObjectTag(ptr_)
50  ? HeapObjectReferenceType::WEAK
51  : HeapObjectReferenceType::STRONG;
52  *result = GetHeapObject();
53  return true;
54 }
55 
56 bool MaybeObject::IsStrong() const {
57  return !HasWeakHeapObjectTag(ptr_) && !IsSmi();
58 }
59 
60 bool MaybeObject::GetHeapObjectIfStrong(HeapObject** result) const {
61  if (!HasWeakHeapObjectTag(ptr_) && !IsSmi()) {
62  *result = reinterpret_cast<HeapObject*>(ptr_);
63  return true;
64  }
65  return false;
66 }
67 
68 HeapObject* MaybeObject::GetHeapObjectAssumeStrong() const {
69  DCHECK(IsStrong());
70  return reinterpret_cast<HeapObject*>(ptr_);
71 }
72 
73 bool MaybeObject::IsWeak() const {
74  return HasWeakHeapObjectTag(ptr_) && !IsCleared();
75 }
76 
77 bool MaybeObject::IsWeakOrCleared() const { return HasWeakHeapObjectTag(ptr_); }
78 
79 bool MaybeObject::GetHeapObjectIfWeak(HeapObject** result) const {
80  if (IsWeak()) {
81  *result = GetHeapObject();
82  return true;
83  }
84  return false;
85 }
86 
87 HeapObject* MaybeObject::GetHeapObjectAssumeWeak() const {
88  DCHECK(IsWeak());
89  return GetHeapObject();
90 }
91 
92 HeapObject* MaybeObject::GetHeapObject() const {
93  DCHECK(!IsSmi());
94  DCHECK(!IsCleared());
95  return reinterpret_cast<HeapObject*>(ptr_ & ~kWeakHeapObjectMask);
96 }
97 
98 Object* MaybeObject::GetHeapObjectOrSmi() const {
99  if (IsSmi()) {
100  return reinterpret_cast<Object*>(ptr_);
101  }
102  return GetHeapObject();
103 }
104 
105 bool MaybeObject::IsObject() const { return IsSmi() || IsStrong(); }
106 
107 MaybeObject MaybeObject::MakeWeak(MaybeObject object) {
108  DCHECK(object.IsStrongOrWeak());
109  return MaybeObject(object.ptr_ | kWeakHeapObjectMask);
110 }
111 
112 // static
113 HeapObjectReference HeapObjectReference::ClearedValue(Isolate* isolate) {
114  // Construct cleared weak ref value.
115  Address raw_value = kClearedWeakHeapObjectLower32;
116 #ifdef V8_COMPRESS_POINTERS
117  // This is necessary to make pointer decompression computation also
118  // suitable for cleared weak references.
119  Address isolate_root = isolate->isolate_root();
120  raw_value |= isolate_root;
121  DCHECK_EQ(raw_value & (~static_cast<Address>(kClearedWeakHeapObjectLower32)),
122  isolate_root);
123 #endif
124  // The rest of the code will check only the lower 32-bits.
125  DCHECK_EQ(kClearedWeakHeapObjectLower32, static_cast<uint32_t>(raw_value));
126  return HeapObjectReference(raw_value);
127 }
128 
129 void HeapObjectReference::Update(HeapObjectSlot slot, HeapObject* value) {
130  Address old_value = (*slot).ptr();
131  DCHECK(!HAS_SMI_TAG(old_value));
132  Address new_value = value->ptr();
133  DCHECK(Internals::HasHeapObjectTag(new_value));
134 
135 #ifdef DEBUG
136  bool weak_before = HasWeakHeapObjectTag(old_value);
137 #endif
138 
139  slot.store(
140  HeapObjectReference(new_value | (old_value & kWeakHeapObjectMask)));
141 
142 #ifdef DEBUG
143  bool weak_after = HasWeakHeapObjectTag((*slot).ptr());
144  DCHECK_EQ(weak_before, weak_after);
145 #endif
146 }
147 
148 } // namespace internal
149 } // namespace v8
150 
151 #endif // V8_OBJECTS_MAYBE_OBJECT_INL_H_
Definition: libplatform.h:13