maybe-handles.h 4.21 KB
Newer Older
1 2 3 4
// Copyright 2011 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.

5 6
#ifndef V8_HANDLES_MAYBE_HANDLES_H_
#define V8_HANDLES_MAYBE_HANDLES_H_
7 8 9

#include <type_traits>

10
#include "src/handles/handles.h"
11 12 13 14

namespace v8 {
namespace internal {

15 16 17 18
struct NullMaybeHandleType {};

constexpr NullMaybeHandleType kNullMaybeHandle;

19 20 21 22 23 24 25 26 27 28 29
// ----------------------------------------------------------------------------
// A Handle can be converted into a MaybeHandle. Converting a MaybeHandle
// into a Handle requires checking that it does not point to nullptr.  This
// ensures nullptr checks before use.
//
// Also note that Handles do not provide default equality comparison or hashing
// operators on purpose. Such operators would be misleading, because intended
// semantics is ambiguous between Handle location and object identity.
template <typename T>
class MaybeHandle final {
 public:
30
  V8_INLINE MaybeHandle() = default;
31

32 33
  V8_INLINE MaybeHandle(NullMaybeHandleType) {}

34 35 36
  // Constructor for handling automatic up casting from Handle.
  // Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected.
  template <typename S, typename = typename std::enable_if<
37
                            std::is_convertible<S*, T*>::value>::type>
38
  V8_INLINE MaybeHandle(Handle<S> handle) : location_(handle.location_) {}
39 40 41

  // Constructor for handling automatic up casting.
  // Ex. MaybeHandle<JSArray> can be passed when Handle<Object> is expected.
42 43
  template <typename S, typename = typename std::enable_if<
                            std::is_convertible<S*, T*>::value>::type>
44
  V8_INLINE MaybeHandle(MaybeHandle<S> maybe_handle)
45
      : location_(maybe_handle.location_) {}
46

47
  V8_INLINE MaybeHandle(T object, Isolate* isolate);
48
  V8_INLINE MaybeHandle(T object, LocalHeap* local_heap);
49 50 51 52 53 54 55 56 57 58 59

  V8_INLINE void Assert() const { DCHECK_NOT_NULL(location_); }
  V8_INLINE void Check() const { CHECK_NOT_NULL(location_); }

  V8_INLINE Handle<T> ToHandleChecked() const {
    Check();
    return Handle<T>(location_);
  }

  // Convert to a Handle with a type that can be upcasted to.
  template <typename S>
60
  V8_WARN_UNUSED_RESULT V8_INLINE bool ToHandle(Handle<S>* out) const {
61 62 63 64 65 66 67 68 69 70 71
    if (location_ == nullptr) {
      *out = Handle<T>::null();
      return false;
    } else {
      *out = Handle<T>(location_);
      return true;
    }
  }

  // Returns the raw address where this handle is stored. This should only be
  // used for hashing handles; do not ever try to dereference it.
72 73 74
  V8_INLINE Address address() const {
    return reinterpret_cast<Address>(location_);
  }
75 76 77 78

  bool is_null() const { return location_ == nullptr; }

 protected:
79
  Address* location_ = nullptr;
80 81 82 83 84 85 86 87 88 89 90

  // MaybeHandles of different classes are allowed to access each
  // other's location_.
  template <typename>
  friend class MaybeHandle;
};

// A handle which contains a potentially weak pointer. Keeps it alive (strongly)
// while the MaybeObjectHandle is alive.
class MaybeObjectHandle {
 public:
91 92
  inline MaybeObjectHandle()
      : reference_type_(HeapObjectReferenceType::STRONG) {}
93
  inline MaybeObjectHandle(MaybeObject object, Isolate* isolate);
94
  inline MaybeObjectHandle(Object object, Isolate* isolate);
95 96
  inline MaybeObjectHandle(MaybeObject object, LocalHeap* local_heap);
  inline MaybeObjectHandle(Object object, LocalHeap* local_heap);
97 98
  inline explicit MaybeObjectHandle(Handle<Object> object);

99
  static inline MaybeObjectHandle Weak(Object object, Isolate* isolate);
100 101
  static inline MaybeObjectHandle Weak(Handle<Object> object);

102 103
  inline MaybeObject operator*() const;
  inline MaybeObject operator->() const;
104 105
  inline Handle<Object> object() const;

106
  inline bool is_identical_to(const MaybeObjectHandle& other) const;
107 108 109
  bool is_null() const { return handle_.is_null(); }

 private:
110
  inline MaybeObjectHandle(Object object,
111 112 113 114 115 116 117 118 119 120 121 122
                           HeapObjectReferenceType reference_type,
                           Isolate* isolate);
  inline MaybeObjectHandle(Handle<Object> object,
                           HeapObjectReferenceType reference_type);

  HeapObjectReferenceType reference_type_;
  MaybeHandle<Object> handle_;
};

}  // namespace internal
}  // namespace v8

123
#endif  // V8_HANDLES_MAYBE_HANDLES_H_