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_H_ 6 #define V8_OBJECTS_MAYBE_OBJECT_H_ 7 8 #include "src/objects/tagged-impl.h" 9 10 namespace v8 { 11 namespace internal { 12 13 // A MaybeObject is either a SMI, a strong reference to a HeapObject, a weak 14 // reference to a HeapObject, or a cleared weak reference. It's used for 15 // implementing in-place weak references (see design doc: goo.gl/j6SdcK ) 16 class MaybeObject : public TaggedImpl<HeapObjectReferenceType::WEAK, Address> { 17 public: MaybeObject()18 constexpr MaybeObject() : TaggedImpl(kNullAddress) {} MaybeObject(Address ptr)19 constexpr explicit MaybeObject(Address ptr) : TaggedImpl(ptr) {} 20 21 // These operator->() overloads are required for handlified code. 22 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 V8_INLINE static MaybeObject Create(MaybeObject o); 31 V8_INLINE static MaybeObject Create(Object o); 32 V8_INLINE static MaybeObject Create(Smi smi); 33 34 #ifdef VERIFY_HEAP 35 static void VerifyMaybeObjectPointer(Isolate* isolate, MaybeObject p); 36 #endif 37 38 private: 39 template <typename TFieldType, int kFieldOffset> 40 friend class TaggedField; 41 }; 42 43 // A HeapObjectReference is either a strong reference to a HeapObject, a weak 44 // reference to a HeapObject, or a cleared weak reference. 45 class HeapObjectReference : public MaybeObject { 46 public: HeapObjectReference(Address address)47 explicit HeapObjectReference(Address address) : MaybeObject(address) {} 48 V8_INLINE explicit HeapObjectReference(Object object); 49 50 V8_INLINE static HeapObjectReference Strong(Object object); 51 52 V8_INLINE static HeapObjectReference Weak(Object object); 53 54 V8_INLINE static HeapObjectReference From(Object object, 55 HeapObjectReferenceType type); 56 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 } // namespace internal 64 } // namespace v8 65 66 #endif // V8_OBJECTS_MAYBE_OBJECT_H_ 67