GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 165 172 95.9 %
Date: 2022-05-19 04:15:38 Branches: 65 92 70.7 %

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
1410969
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
36
2821938
    : persistent_handle_(env->isolate(), object), env_(env) {
37
1410969
  CHECK_EQ(false, object.IsEmpty());
38
1410969
  CHECK_GT(object->InternalFieldCount(), 0);
39
1410969
  object->SetAlignedPointerInInternalField(
40
      BaseObject::kSlot,
41
      static_cast<void*>(this));
42
1410969
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
43
1410969
  env->modify_base_object_count(1);
44
1410969
}
45
46

7726050
BaseObject::~BaseObject() {
47
2800524
  env()->modify_base_object_count(-1);
48
2800524
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
49
50
2800524
  if (UNLIKELY(has_pointer_data())) {
51
377974
    PointerData* metadata = pointer_data();
52
377974
    CHECK_EQ(metadata->strong_ptr_count, 0);
53
377974
    metadata->self = nullptr;
54
377974
    if (metadata->weak_ptr_count == 0)
55
377932
      delete metadata;
56
  }
57
58
2800524
  if (persistent_handle_.IsEmpty()) {
59
    // This most likely happened because the weak callback below cleared it.
60
2125002
    return;
61
  }
62
63
  {
64
675522
    v8::HandleScope handle_scope(env()->isolate());
65
1351044
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
66
  }
67
}
68
69
185023
void BaseObject::Detach() {
70
185023
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
71
185023
  pointer_data()->is_detached = true;
72
185023
}
73
74
471700
v8::Global<v8::Object>& BaseObject::persistent() {
75
471700
  return persistent_handle_;
76
}
77
78
79
2423412
v8::Local<v8::Object> BaseObject::object() const {
80
2423412
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
81
}
82
83
111998
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
84
111998
  v8::Local<v8::Object> handle = object();
85
86
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
87
            isolate);
88
  DCHECK_EQ(env()->isolate(), isolate);
89
90
111998
  return handle;
91
}
92
93
16531133
Environment* BaseObject::env() const {
94
16531133
  return env_;
95
}
96
97
9643852
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
98
9643852
  v8::Local<v8::Object> obj = value.As<v8::Object>();
99
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
100
  return static_cast<BaseObject*>(
101
19287704
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
102
}
103
104
105
template <typename T>
106
713622
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
107
713622
  return static_cast<T*>(FromJSObject(object));
108
}
109
110
111
1208548
void BaseObject::MakeWeak() {
112
1208548
  if (has_pointer_data()) {
113
45587
    pointer_data()->wants_weak_jsobj = true;
114
45587
    if (pointer_data()->strong_ptr_count > 0) return;
115
  }
116
117
2417094
  persistent_handle_.SetWeak(
118
      this,
119
1062500
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
120
1062500
        BaseObject* obj = data.GetParameter();
121
        // Clear the persistent handle so that ~BaseObject() doesn't attempt
122
        // to mess with internal fields, since the JS object may have
123
        // transitioned into an invalid state.
124
        // Refs: https://github.com/nodejs/node/issues/18897
125
1062500
        obj->persistent_handle_.Reset();
126

1062500
        CHECK_IMPLIES(obj->has_pointer_data(),
127
                      obj->pointer_data()->strong_ptr_count == 0);
128
1062500
        obj->OnGCCollect();
129
1062500
      }, v8::WeakCallbackType::kParameter);
130
}
131
132
1245184
void BaseObject::OnGCCollect() {
133
1245184
  delete this;
134
1245184
}
135
136
2928
void BaseObject::ClearWeak() {
137
2928
  if (has_pointer_data())
138
    pointer_data()->wants_weak_jsobj = false;
139
140
2928
  persistent_handle_.ClearWeak();
141
2928
}
142
143
bool BaseObject::IsWeakOrDetached() const {
144
  if (persistent_handle_.IsWeak()) return true;
145
146
  if (!has_pointer_data()) return false;
147
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
148
  return pd->wants_weak_jsobj || pd->is_detached;
149
}
150
151
23839
void BaseObject::LazilyInitializedJSTemplateConstructor(
152
    const v8::FunctionCallbackInfo<v8::Value>& args) {
153
  DCHECK(args.IsConstructCall());
154
  DCHECK_GT(args.This()->InternalFieldCount(), 0);
155
23839
  args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156
23839
}
157
158
v8::Local<v8::FunctionTemplate>
159
21530
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
160
  v8::Local<v8::FunctionTemplate> t =
161
21530
      env->NewFunctionTemplate(LazilyInitializedJSTemplateConstructor);
162
21530
  t->Inherit(BaseObject::GetConstructorTemplate(env));
163
43060
  t->InstanceTemplate()->SetInternalFieldCount(
164
      BaseObject::kInternalFieldCount);
165
21530
  return t;
166
}
167
168
template <int Field>
169
8
void BaseObject::InternalFieldGet(
170
    v8::Local<v8::String> property,
171
    const v8::PropertyCallbackInfo<v8::Value>& info) {
172
8
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
173
8
}
174
175
template <int Field, bool (v8::Value::* typecheck)() const>
176
102469
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
177
                                  v8::Local<v8::Value> value,
178
                                  const v8::PropertyCallbackInfo<void>& info) {
179
  // This could be e.g. value->IsFunction().
180
102469
  CHECK(((*value)->*typecheck)());
181
102469
  info.This()->SetInternalField(Field, value);
182
102469
}
183
184
10574270
bool BaseObject::has_pointer_data() const {
185
10574270
  return pointer_data_ != nullptr;
186
}
187
188
2995791
BaseObject::PointerData* BaseObject::pointer_data() {
189
2995791
  if (!has_pointer_data()) {
190
191197
    PointerData* metadata = new PointerData();
191
191197
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
192
191197
    metadata->self = this;
193
191197
    pointer_data_ = metadata;
194
  }
195
2995791
  CHECK(has_pointer_data());
196
2995791
  return pointer_data_;
197
}
198
199
768045
void BaseObject::decrease_refcount() {
200
768045
  CHECK(has_pointer_data());
201
768045
  PointerData* metadata = pointer_data();
202
768045
  CHECK_GT(metadata->strong_ptr_count, 0);
203
768045
  unsigned int new_refcount = --metadata->strong_ptr_count;
204
768045
  if (new_refcount == 0) {
205
261192
    if (metadata->is_detached) {
206
182686
      OnGCCollect();
207

78506
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
208
45586
      MakeWeak();
209
    }
210
  }
211
768045
}
212
213
770338
void BaseObject::increase_refcount() {
214
770338
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
215

770338
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
216
263388
    persistent_handle_.ClearWeak();
217
770338
}
218
219
template <typename T, bool kIsWeak>
220
BaseObject::PointerData*
221
3889265
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
222
  if (kIsWeak) {
223
2403784
    return data_.pointer_data;
224
  }
225
1485481
  if (get_base_object() == nullptr) {
226
    return nullptr;
227
  }
228
1485481
  return get_base_object()->pointer_data();
229
}
230
231
template <typename T, bool kIsWeak>
232
17471110
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
233
  if (kIsWeak) {
234
1077166
    if (pointer_data() == nullptr) {
235
8
      return nullptr;
236
    }
237
1077158
    return pointer_data()->self;
238
  }
239
16393944
  return data_.target;
240
}
241
242
template <typename T, bool kIsWeak>
243
3567502
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
244
3567502
  if (kIsWeak) {
245
56124
    if (pointer_data() != nullptr &&
246

81076
        --pointer_data()->weak_ptr_count == 0 &&
247
24952
        pointer_data()->self == nullptr) {
248
42
      delete pointer_data();
249
    }
250
3511378
  } else if (get() != nullptr) {
251
1340403
    get()->decrease_refcount();
252
  }
253
3567502
}
254
255
template <typename T, bool kIsWeak>
256
2917822
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
257
2917822
  data_.target = nullptr;
258
2917822
}
259
260
template <typename T, bool kIsWeak>
261
2230519
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
262
2230519
  : BaseObjectPtrImpl() {
263
2230519
  if (target == nullptr) return;
264
  if (kIsWeak) {
265
56114
    data_.pointer_data = target->pointer_data();
266
56114
    CHECK_NOT_NULL(pointer_data());
267
56114
    pointer_data()->weak_ptr_count++;
268
  } else {
269
1485481
    data_.target = target;
270
1485481
    CHECK_NOT_NULL(pointer_data());
271
1485481
    get()->increase_refcount();
272
  }
273
}
274
275
template <typename T, bool kIsWeak>
276
template <typename U, bool kW>
277
42117
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
278
    const BaseObjectPtrImpl<U, kW>& other)
279
42117
  : BaseObjectPtrImpl(other.get()) {}
280
281
template <typename T, bool kIsWeak>
282
880609
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
283
880609
  : BaseObjectPtrImpl(other.get()) {}
284
285
template <typename T, bool kIsWeak>
286
template <typename U, bool kW>
287
8
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
288
    const BaseObjectPtrImpl<U, kW>& other) {
289
8
  if (other.get() == get()) return *this;
290
8
  this->~BaseObjectPtrImpl();
291
8
  return *new (this) BaseObjectPtrImpl(other);
292
}
293
294
template <typename T, bool kIsWeak>
295
2
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
296
    const BaseObjectPtrImpl& other) {
297
2
  if (other.get() == get()) return *this;
298
2
  this->~BaseObjectPtrImpl();
299
2
  return *new (this) BaseObjectPtrImpl(other);
300
}
301
302
template <typename T, bool kIsWeak>
303
919953
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
304
919953
  : data_(other.data_) {
305
919953
  if (kIsWeak)
306
    other.data_.target = nullptr;
307
  else
308
    other.data_.pointer_data = nullptr;
309
919953
}
310
311
template <typename T, bool kIsWeak>
312
447798
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
313
    BaseObjectPtrImpl&& other) {
314
447798
  if (&other == this) return *this;
315
447798
  this->~BaseObjectPtrImpl();
316
447798
  return *new (this) BaseObjectPtrImpl(std::move(other));
317
}
318
319
template <typename T, bool kIsWeak>
320
275843
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
321
275843
  *this = BaseObjectPtrImpl(ptr);
322
275843
}
323
324
template <typename T, bool kIsWeak>
325
14685796
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
326
14685796
  return static_cast<T*>(get_base_object());
327
}
328
329
template <typename T, bool kIsWeak>
330
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
331
  return *get();
332
}
333
334
template <typename T, bool kIsWeak>
335
4199375
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
336
4199375
  return get();
337
}
338
339
template <typename T, bool kIsWeak>
340
1348246
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
341
1348246
  return get() != nullptr;
342
}
343
344
template <typename T, bool kIsWeak>
345
template <typename U, bool kW>
346
11209
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
347
    const BaseObjectPtrImpl<U, kW>& other) const {
348
11209
  return get() == other.get();
349
}
350
351
template <typename T, bool kIsWeak>
352
template <typename U, bool kW>
353
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
354
    const BaseObjectPtrImpl<U, kW>& other) const {
355
  return get() != other.get();
356
}
357
358
template <typename T, typename... Args>
359
40297
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
360
40297
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
361
}
362
363
template <typename T, typename... Args>
364
26467
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
365
26467
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
366
26467
  target->Detach();
367
26467
  return target;
368
}
369
370
}  // namespace node
371
372
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
373
374
#endif  // SRC_BASE_OBJECT_INL_H_