summaryrefslogtreecommitdiff
path: root/deps/v8/src/objects/maybe-object.h
blob: c40ae0a5aadc37e26e86f85e634362dc1a6d0191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_MAYBE_OBJECT_H_
#define V8_OBJECTS_MAYBE_OBJECT_H_

#include "include/v8-internal.h"
#include "include/v8.h"
#include "src/globals.h"
#include "src/objects.h"
#include "src/objects/slots.h"
#include "src/objects/smi.h"

namespace v8 {
namespace internal {

class HeapObject;
class Isolate;
class StringStream;

// A MaybeObject is either a SMI, a strong reference to a HeapObject, a weak
// reference to a HeapObject, or a cleared weak reference. It's used for
// implementing in-place weak references (see design doc: goo.gl/j6SdcK )
class MaybeObject {
 public:
  MaybeObject() : ptr_(kNullAddress) {}
  explicit MaybeObject(Address ptr) : ptr_(ptr) {}

  bool operator==(const MaybeObject& other) const { return ptr_ == other.ptr_; }
  bool operator!=(const MaybeObject& other) const { return ptr_ != other.ptr_; }

  Address ptr() const { return ptr_; }

  // Enable incremental transition of client code.
  MaybeObject* operator->() { return this; }
  const MaybeObject* operator->() const { return this; }

  bool IsSmi() const { return HAS_SMI_TAG(ptr_); }
  inline bool ToSmi(Smi* value);
  inline Smi ToSmi() const;

  bool IsCleared() const {
    return static_cast<uint32_t>(ptr_) == kClearedWeakHeapObjectLower32;
  }

  inline bool IsStrongOrWeak() const;
  inline bool IsStrong() const;

  // If this MaybeObject is a strong pointer to a HeapObject, returns true and
  // sets *result. Otherwise returns false.
  inline bool GetHeapObjectIfStrong(HeapObject* result) const;

  // DCHECKs that this MaybeObject is a strong pointer to a HeapObject and
  // returns the HeapObject.
  inline HeapObject GetHeapObjectAssumeStrong() const;

  inline bool IsWeak() const;
  inline bool IsWeakOrCleared() const;

  // If this MaybeObject is a weak pointer to a HeapObject, returns true and
  // sets *result. Otherwise returns false.
  inline bool GetHeapObjectIfWeak(HeapObject* result) const;

  // DCHECKs that this MaybeObject is a weak pointer to a HeapObject and
  // returns the HeapObject.
  inline HeapObject GetHeapObjectAssumeWeak() const;

  // If this MaybeObject is a strong or weak pointer to a HeapObject, returns
  // true and sets *result. Otherwise returns false.
  inline bool GetHeapObject(HeapObject* result) const;
  inline bool GetHeapObject(HeapObject* result,
                            HeapObjectReferenceType* reference_type) const;

  // DCHECKs that this MaybeObject is a strong or a weak pointer to a HeapObject
  // and returns the HeapObject.
  inline HeapObject GetHeapObject() const;

  // DCHECKs that this MaybeObject is a strong or a weak pointer to a HeapObject
  // or a SMI and returns the HeapObject or SMI.
  inline Object GetHeapObjectOrSmi() const;

  inline bool IsObject() const;
  template <typename T>
  T cast() const {
    DCHECK(!HasWeakHeapObjectTag(ptr_));
    return T::cast(Object(ptr_));
  }

  static MaybeObject FromSmi(Smi smi) {
    DCHECK(HAS_SMI_TAG(smi->ptr()));
    return MaybeObject(smi->ptr());
  }

  static MaybeObject FromObject(Object object) {
    DCHECK(!HasWeakHeapObjectTag(object.ptr()));
    return MaybeObject(object.ptr());
  }

  static inline MaybeObject MakeWeak(MaybeObject object);

#ifdef VERIFY_HEAP
  static void VerifyMaybeObjectPointer(Isolate* isolate, MaybeObject p);
#endif

  // Prints this object without details.
  void ShortPrint(FILE* out = stdout);

  // Prints this object without details to a message accumulator.
  void ShortPrint(StringStream* accumulator);

  void ShortPrint(std::ostream& os);

#ifdef OBJECT_PRINT
  void Print();
  void Print(std::ostream& os);
#else
  void Print() { ShortPrint(); }
  void Print(std::ostream& os) { ShortPrint(os); }
#endif

 private:
  Address ptr_;
};

// A HeapObjectReference is either a strong reference to a HeapObject, a weak
// reference to a HeapObject, or a cleared weak reference.
class HeapObjectReference : public MaybeObject {
 public:
  explicit HeapObjectReference(Address address) : MaybeObject(address) {}
  explicit HeapObjectReference(Object object) : MaybeObject(object->ptr()) {}

  static HeapObjectReference Strong(Object object) {
    DCHECK(!object->IsSmi());
    DCHECK(!HasWeakHeapObjectTag(object));
    return HeapObjectReference(object);
  }

  static HeapObjectReference Weak(Object object) {
    DCHECK(!object->IsSmi());
    DCHECK(!HasWeakHeapObjectTag(object));
    return HeapObjectReference(object->ptr() | kWeakHeapObjectMask);
  }

  V8_INLINE static HeapObjectReference ClearedValue(Isolate* isolate);

  template <typename THeapObjectSlot>
  V8_INLINE static void Update(THeapObjectSlot slot, HeapObject value);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_OBJECTS_MAYBE_OBJECT_H_