GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 101 108 93.5 %
Date: 2022-08-29 04:21:03 Branches: 24 42 57.1 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_BASE_OBJECT_INL_H_
23
#define SRC_BASE_OBJECT_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "base_object.h"
28
#include "env-inl.h"
29
#include "util.h"
30
31
#include "v8.h"
32
33
namespace node {
34
35
179513
void BaseObject::Detach() {
36
179513
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
37
179513
  pointer_data()->is_detached = true;
38
179513
}
39
40
464755
v8::Global<v8::Object>& BaseObject::persistent() {
41
464755
  return persistent_handle_;
42
}
43
44
45
2487447
v8::Local<v8::Object> BaseObject::object() const {
46
2487447
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
47
}
48
49
140204
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
50
140204
  v8::Local<v8::Object> handle = object();
51
52
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
53
            isolate);
54
  DCHECK_EQ(env()->isolate(), isolate);
55
56
140204
  return handle;
57
}
58
59
17025872
Environment* BaseObject::env() const {
60
17025872
  return env_;
61
}
62
63
9904774
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
64
9904774
  v8::Local<v8::Object> obj = value.As<v8::Object>();
65
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
66
  return static_cast<BaseObject*>(
67
19809548
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
68
}
69
70
71
template <typename T>
72
769485
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
73
769485
  return static_cast<T*>(FromJSObject(object));
74
}
75
76
1250384
void BaseObject::OnGCCollect() {
77
1250384
  delete this;
78
1250384
}
79
80
3001
void BaseObject::ClearWeak() {
81
3001
  if (has_pointer_data())
82
    pointer_data()->wants_weak_jsobj = false;
83
84
3001
  persistent_handle_.ClearWeak();
85
3001
}
86
87
bool BaseObject::IsWeakOrDetached() const {
88
  if (persistent_handle_.IsWeak()) return true;
89
90
  if (!has_pointer_data()) return false;
91
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
92
  return pd->wants_weak_jsobj || pd->is_detached;
93
}
94
95
template <int Field>
96
8
void BaseObject::InternalFieldGet(
97
    v8::Local<v8::String> property,
98
    const v8::PropertyCallbackInfo<v8::Value>& info) {
99
8
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
100
8
}
101
102
template <int Field, bool (v8::Value::* typecheck)() const>
103
99260
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
104
                                  v8::Local<v8::Value> value,
105
                                  const v8::PropertyCallbackInfo<void>& info) {
106
  // This could be e.g. value->IsFunction().
107
99260
  CHECK(((*value)->*typecheck)());
108
99260
  info.This()->SetInternalField(Field, value);
109
99260
}
110
111
10419300
bool BaseObject::has_pointer_data() const {
112
10419300
  return pointer_data_ != nullptr;
113
}
114
115
template <typename T, bool kIsWeak>
116
BaseObject::PointerData*
117
3740653
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
118
  if constexpr (kIsWeak) {
119
2307254
    return data_.pointer_data;
120
  }
121
1433399
  if (get_base_object() == nullptr) {
122
    return nullptr;
123
  }
124
1433399
  return get_base_object()->pointer_data();
125
}
126
127
template <typename T, bool kIsWeak>
128
17114761
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
129
  if constexpr (kIsWeak) {
130
1041174
    if (pointer_data() == nullptr) {
131
8
      return nullptr;
132
    }
133
1041166
    return pointer_data()->self;
134
  }
135
16073587
  return data_.target;
136
}
137
138
template <typename T, bool kIsWeak>
139
3423973
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
140
  if constexpr (kIsWeak) {
141
50236
    if (pointer_data() != nullptr &&
142

74166
        --pointer_data()->weak_ptr_count == 0 &&
143
23930
        pointer_data()->self == nullptr) {
144
38
      delete pointer_data();
145
    }
146
3373737
  } else if (get() != nullptr) {
147
1286980
    get()->decrease_refcount();
148
  }
149
3423973
}
150
151
template <typename T, bool kIsWeak>
152
2813080
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
153
2813080
  data_.target = nullptr;
154
2813080
}
155
156
template <typename T, bool kIsWeak>
157
2152104
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
158
2152104
  : BaseObjectPtrImpl() {
159
2152104
  if (target == nullptr) return;
160
  if constexpr (kIsWeak) {
161
50242
    data_.pointer_data = target->pointer_data();
162
50242
    CHECK_NOT_NULL(pointer_data());
163
50242
    pointer_data()->weak_ptr_count++;
164
  } else {
165
1433399
    data_.target = target;
166
1433399
    CHECK_NOT_NULL(pointer_data());
167
1433399
    get()->increase_refcount();
168
  }
169
}
170
171
template <typename T, bool kIsWeak>
172
template <typename U, bool kW>
173
42921
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
174
    const BaseObjectPtrImpl<U, kW>& other)
175
42921
  : BaseObjectPtrImpl(other.get()) {}
176
177
template <typename T, bool kIsWeak>
178
855226
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
179
855226
  : BaseObjectPtrImpl(other.get()) {}
180
181
template <typename T, bool kIsWeak>
182
template <typename U, bool kW>
183
8
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
184
    const BaseObjectPtrImpl<U, kW>& other) {
185
8
  if (other.get() == get()) return *this;
186
8
  this->~BaseObjectPtrImpl();
187
8
  return *new (this) BaseObjectPtrImpl(other);
188
}
189
190
template <typename T, bool kIsWeak>
191
2
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
192
    const BaseObjectPtrImpl& other) {
193
2
  if (other.get() == get()) return *this;
194
2
  this->~BaseObjectPtrImpl();
195
2
  return *new (this) BaseObjectPtrImpl(other);
196
}
197
198
template <typename T, bool kIsWeak>
199
883245
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
200
883245
  : data_(other.data_) {
201
  if constexpr (kIsWeak)
202
4
    other.data_.target = nullptr;
203
  else
204
883241
    other.data_.pointer_data = nullptr;
205
883245
}
206
207
template <typename T, bool kIsWeak>
208
426902
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
209
    BaseObjectPtrImpl&& other) {
210
426902
  if (&other == this) return *this;
211
426902
  this->~BaseObjectPtrImpl();
212
426902
  return *new (this) BaseObjectPtrImpl(std::move(other));
213
}
214
215
template <typename T, bool kIsWeak>
216
260534
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
217
260534
  *this = BaseObjectPtrImpl(ptr);
218
260534
}
219
220
template <typename T, bool kIsWeak>
221
14434159
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
222
14434159
  return static_cast<T*>(get_base_object());
223
}
224
225
template <typename T, bool kIsWeak>
226
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
227
  return *get();
228
}
229
230
template <typename T, bool kIsWeak>
231
4121091
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
232
4121091
  return get();
233
}
234
235
template <typename T, bool kIsWeak>
236
1235811
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
237
1235811
  return get() != nullptr;
238
}
239
240
template <typename T, bool kIsWeak>
241
template <typename U, bool kW>
242
11142
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
243
    const BaseObjectPtrImpl<U, kW>& other) const {
244
11142
  return get() == other.get();
245
}
246
247
template <typename T, bool kIsWeak>
248
template <typename U, bool kW>
249
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
250
    const BaseObjectPtrImpl<U, kW>& other) const {
251
  return get() != other.get();
252
}
253
254
template <typename T, typename... Args>
255
40514
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
256
40514
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
257
}
258
259
template <typename T, typename... Args>
260
26766
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
261
26766
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
262
26766
  target->Detach();
263
26766
  return target;
264
}
265
266
}  // namespace node
267
268
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
269
270
#endif  // SRC_BASE_OBJECT_INL_H_