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 
12 namespace v8 {
13 namespace internal {
14 
ToSmi(Smi ** value)15 bool MaybeObject::ToSmi(Smi** value) {
16   if (HAS_SMI_TAG(this)) {
17     *value = Smi::cast(reinterpret_cast<Object*>(this));
18     return true;
19   }
20   return false;
21 }
22 
ToSmi()23 Smi* MaybeObject::ToSmi() {
24   DCHECK(HAS_SMI_TAG(this));
25   return Smi::cast(reinterpret_cast<Object*>(this));
26 }
27 
IsStrongOrWeakHeapObject()28 bool MaybeObject::IsStrongOrWeakHeapObject() const {
29   if (IsSmi() || IsClearedWeakHeapObject()) {
30     return false;
31   }
32   return true;
33 }
34 
ToStrongOrWeakHeapObject(HeapObject ** result)35 bool MaybeObject::ToStrongOrWeakHeapObject(HeapObject** result) {
36   if (IsSmi() || IsClearedWeakHeapObject()) {
37     return false;
38   }
39   *result = GetHeapObject();
40   return true;
41 }
42 
ToStrongOrWeakHeapObject(HeapObject ** result,HeapObjectReferenceType * reference_type)43 bool MaybeObject::ToStrongOrWeakHeapObject(
44     HeapObject** result, HeapObjectReferenceType* reference_type) {
45   if (IsSmi() || IsClearedWeakHeapObject()) {
46     return false;
47   }
48   *reference_type = HasWeakHeapObjectTag(this)
49                         ? HeapObjectReferenceType::WEAK
50                         : HeapObjectReferenceType::STRONG;
51   *result = GetHeapObject();
52   return true;
53 }
54 
IsStrongHeapObject()55 bool MaybeObject::IsStrongHeapObject() const {
56   return !HasWeakHeapObjectTag(this) && !IsSmi();
57 }
58 
ToStrongHeapObject(HeapObject ** result)59 bool MaybeObject::ToStrongHeapObject(HeapObject** result) {
60   if (!HasWeakHeapObjectTag(this) && !IsSmi()) {
61     *result = reinterpret_cast<HeapObject*>(this);
62     return true;
63   }
64   return false;
65 }
66 
ToStrongHeapObject()67 HeapObject* MaybeObject::ToStrongHeapObject() {
68   DCHECK(IsStrongHeapObject());
69   return reinterpret_cast<HeapObject*>(this);
70 }
71 
IsWeakHeapObject()72 bool MaybeObject::IsWeakHeapObject() const {
73   return HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject();
74 }
75 
IsWeakOrClearedHeapObject()76 bool MaybeObject::IsWeakOrClearedHeapObject() const {
77   return HasWeakHeapObjectTag(this);
78 }
79 
ToWeakHeapObject(HeapObject ** result)80 bool MaybeObject::ToWeakHeapObject(HeapObject** result) {
81   if (HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject()) {
82     *result = GetHeapObject();
83     return true;
84   }
85   return false;
86 }
87 
ToWeakHeapObject()88 HeapObject* MaybeObject::ToWeakHeapObject() {
89   DCHECK(IsWeakHeapObject());
90   return GetHeapObject();
91 }
92 
GetHeapObject()93 HeapObject* MaybeObject::GetHeapObject() {
94   DCHECK(!IsSmi());
95   DCHECK(!IsClearedWeakHeapObject());
96   return RemoveWeakHeapObjectMask(reinterpret_cast<HeapObjectReference*>(this));
97 }
98 
GetHeapObjectOrSmi()99 Object* MaybeObject::GetHeapObjectOrSmi() {
100   if (IsSmi()) {
101     return reinterpret_cast<Object*>(this);
102   }
103   return GetHeapObject();
104 }
105 
IsObject()106 bool MaybeObject::IsObject() const { return IsSmi() || IsStrongHeapObject(); }
107 
ToObject()108 Object* MaybeObject::ToObject() {
109   DCHECK(!HasWeakHeapObjectTag(this));
110   return reinterpret_cast<Object*>(this);
111 }
112 
MakeWeak(MaybeObject * object)113 MaybeObject* MaybeObject::MakeWeak(MaybeObject* object) {
114   DCHECK(object->IsStrongOrWeakHeapObject());
115   return AddWeakHeapObjectMask(object);
116 }
117 
118 }  // namespace internal
119 }  // namespace v8
120 
121 #endif  // V8_OBJECTS_MAYBE_OBJECT_INL_H_
122