maybe-object.h 2.21 KB
Newer Older
1 2 3 4 5 6 7
// 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_

8
#include "src/objects/tagged-impl.h"
9 10 11 12 13 14 15

namespace v8 {
namespace internal {

// 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 )
16
class MaybeObject : public TaggedImpl<HeapObjectReferenceType::WEAK, Address> {
17
 public:
18 19
  constexpr MaybeObject() : TaggedImpl(kNullAddress) {}
  constexpr explicit MaybeObject(Address ptr) : TaggedImpl(ptr) {}
20

21 22
  // These operator->() overloads are required for handlified code.
  constexpr const MaybeObject* operator->() const { return this; }
23

24
  V8_INLINE static MaybeObject FromSmi(Smi smi);
25

26
  V8_INLINE static MaybeObject FromObject(Object object);
27

28
  V8_INLINE static MaybeObject MakeWeak(MaybeObject object);
29

30 31 32 33
  V8_INLINE static MaybeObject Create(MaybeObject o);
  V8_INLINE static MaybeObject Create(Object o);
  V8_INLINE static MaybeObject Create(Smi smi);

34
#ifdef VERIFY_HEAP
35
  static void VerifyMaybeObjectPointer(Isolate* isolate, MaybeObject p);
36
#endif
37 38 39 40

 private:
  template <typename TFieldType, int kFieldOffset>
  friend class TaggedField;
41 42 43 44 45 46
};

// 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:
47
  explicit HeapObjectReference(Address address) : MaybeObject(address) {}
48 49 50 51 52
  V8_INLINE explicit HeapObjectReference(Object object);

  V8_INLINE static HeapObjectReference Strong(Object object);

  V8_INLINE static HeapObjectReference Weak(Object object);
53

54 55 56
  V8_INLINE static HeapObjectReference From(Object object,
                                            HeapObjectReferenceType type);

57
  V8_INLINE static HeapObjectReference ClearedValue(IsolateRoot isolate);
58

59
  template <typename THeapObjectSlot>
60
  V8_INLINE static void Update(THeapObjectSlot slot, HeapObject value);
61 62 63 64 65 66
};

}  // namespace internal
}  // namespace v8

#endif  // V8_OBJECTS_MAYBE_OBJECT_H_