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_MAYBE_HANDLES_INL_H_
6#define V8_MAYBE_HANDLES_INL_H_
7
8#include "src/maybe-handles.h"
9
10#include "src/handles-inl.h"
11#include "src/objects/maybe-object-inl.h"
12
13namespace v8 {
14namespace internal {
15template <typename T>
16MaybeHandle<T>::MaybeHandle(T object, Isolate* isolate)
17 : MaybeHandle(handle(object, isolate)) {}
18
19MaybeObjectHandle::MaybeObjectHandle()
20 : reference_type_(HeapObjectReferenceType::STRONG),
21 handle_(Handle<Object>::null()) {}
22
23MaybeObjectHandle::MaybeObjectHandle(MaybeObject object, Isolate* isolate) {
24 HeapObject heap_object;
25 DCHECK(!object->IsCleared());
26 if (object->GetHeapObjectIfWeak(&heap_object)) {
27 handle_ = handle(heap_object, isolate);
28 reference_type_ = HeapObjectReferenceType::WEAK;
29 } else {
30 handle_ = handle(object->cast<Object>(), isolate);
31 reference_type_ = HeapObjectReferenceType::STRONG;
32 }
33}
34
35MaybeObjectHandle::MaybeObjectHandle(Handle<Object> object)
36 : reference_type_(HeapObjectReferenceType::STRONG), handle_(object) {}
37
38MaybeObjectHandle::MaybeObjectHandle(Object object, Isolate* isolate)
39 : reference_type_(HeapObjectReferenceType::STRONG),
40 handle_(object, isolate) {}
41
42MaybeObjectHandle::MaybeObjectHandle(Object object,
43 HeapObjectReferenceType reference_type,
44 Isolate* isolate)
45 : reference_type_(reference_type), handle_(handle(object, isolate)) {}
46
47MaybeObjectHandle::MaybeObjectHandle(Handle<Object> object,
48 HeapObjectReferenceType reference_type)
49 : reference_type_(reference_type), handle_(object) {}
50
51MaybeObjectHandle MaybeObjectHandle::Weak(Handle<Object> object) {
52 return MaybeObjectHandle(object, HeapObjectReferenceType::WEAK);
53}
54
55MaybeObjectHandle MaybeObjectHandle::Weak(Object object, Isolate* isolate) {
56 return MaybeObjectHandle(object, HeapObjectReferenceType::WEAK, isolate);
57}
58
59MaybeObject MaybeObjectHandle::operator*() const {
60 if (reference_type_ == HeapObjectReferenceType::WEAK) {
61 return HeapObjectReference::Weak(*handle_.ToHandleChecked());
62 } else {
63 return MaybeObject::FromObject(*handle_.ToHandleChecked());
64 }
65}
66
67MaybeObject MaybeObjectHandle::operator->() const {
68 if (reference_type_ == HeapObjectReferenceType::WEAK) {
69 return HeapObjectReference::Weak(*handle_.ToHandleChecked());
70 } else {
71 return MaybeObject::FromObject(*handle_.ToHandleChecked());
72 }
73}
74
75Handle<Object> MaybeObjectHandle::object() const {
76 return handle_.ToHandleChecked();
77}
78
79inline MaybeObjectHandle handle(MaybeObject object, Isolate* isolate) {
80 return MaybeObjectHandle(object, isolate);
81}
82
83} // namespace internal
84} // namespace v8
85
86#endif // V8_MAYBE_HANDLES_INL_H_
87