GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base_object-inl.h Lines: 161 174 92.5 %
Date: 2021-05-02 04:12:27 Branches: 134 228 58.8 %

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

1030773
BaseObject::~BaseObject() {
47
343584
  env()->modify_base_object_count(-1);
48
343582
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
49
50
343590
  if (UNLIKELY(has_pointer_data())) {
51
161553
    PointerData* metadata = pointer_data();
52
161553
    CHECK_EQ(metadata->strong_ptr_count, 0);
53
161553
    metadata->self = nullptr;
54
161553
    if (metadata->weak_ptr_count == 0)
55
161534
      delete metadata;
56
  }
57
58
687198
  if (persistent_handle_.IsEmpty()) {
59
    // This most likely happened because the weak callback below cleared it.
60
76444
    return;
61
  }
62
63
  {
64
534306
    v8::HandleScope handle_scope(env()->isolate());
65
534296
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
66
  }
67
343595
}
68
69
157410
void BaseObject::Detach() {
70
157410
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
71
157410
  pointer_data()->is_detached = true;
72
157410
}
73
74
457991
v8::Global<v8::Object>& BaseObject::persistent() {
75
457991
  return persistent_handle_;
76
}
77
78
79
2202865
v8::Local<v8::Object> BaseObject::object() const {
80
2202865
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
81
}
82
83
94665
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
84
94665
  v8::Local<v8::Object> handle = object();
85
86
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
87
            isolate);
88
  DCHECK_EQ(env()->isolate(), isolate);
89
90
94640
  return handle;
91
}
92
93
10467858
Environment* BaseObject::env() const {
94
10467858
  return env_;
95
}
96
97
5190540
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
98
5190540
  v8::Local<v8::Object> obj = value.As<v8::Object>();
99
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
100
  return static_cast<BaseObject*>(
101
10381081
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
102
}
103
104
105
template <typename T>
106
853333
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
107
853333
  return static_cast<T*>(FromJSObject(object));
108
}
109
110
111
177929
void BaseObject::MakeWeak() {
112
177929
  if (has_pointer_data()) {
113
41593
    pointer_data()->wants_weak_jsobj = true;
114
41593
    if (pointer_data()->strong_ptr_count > 0) return;
115
  }
116
117
355856
  persistent_handle_.SetWeak(
118
      this,
119
330800
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
120
76436
        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
76436
        obj->persistent_handle_.Reset();
126

76436
        CHECK_IMPLIES(obj->has_pointer_data(),
127
                      obj->pointer_data()->strong_ptr_count == 0);
128
76436
        obj->OnGCCollect();
129
330800
      }, v8::WeakCallbackType::kParameter);
130
}
131
132
76434
void BaseObject::OnGCCollect() {
133
76434
  delete this;
134
76434
}
135
136
2495
void BaseObject::ClearWeak() {
137
2495
  if (has_pointer_data())
138
    pointer_data()->wants_weak_jsobj = false;
139
140
2495
  persistent_handle_.ClearWeak();
141
2495
}
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
v8::Local<v8::FunctionTemplate>
152
17482
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
153
64082
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
154
    DCHECK(args.IsConstructCall());
155
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
156
46600
    args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
157
64082
  };
158
159
17482
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
160
34964
  t->Inherit(BaseObject::GetConstructorTemplate(env));
161
52446
  t->InstanceTemplate()->SetInternalFieldCount(
162
17482
      BaseObject::kInternalFieldCount);
163
17482
  return t;
164
}
165
166
template <int Field>
167
void BaseObject::InternalFieldGet(
168
    v8::Local<v8::String> property,
169
    const v8::PropertyCallbackInfo<v8::Value>& info) {
170
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
171
}
172
173
template <int Field, bool (v8::Value::* typecheck)() const>
174
98153
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
175
                                  v8::Local<v8::Value> value,
176
                                  const v8::PropertyCallbackInfo<void>& info) {
177
  // This could be e.g. value->IsFunction().
178
98153
  CHECK(((*value)->*typecheck)());
179
196306
  info.This()->SetInternalField(Field, value);
180
98153
}
181
182
6747691
bool BaseObject::has_pointer_data() const {
183
6747691
  return pointer_data_ != nullptr;
184
}
185
186
2678000
BaseObject::PointerData* BaseObject::pointer_data() {
187
2678000
  if (!has_pointer_data()) {
188
162805
    PointerData* metadata = new PointerData();
189
325600
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
190
162800
    metadata->self = this;
191
162800
    pointer_data_ = metadata;
192
  }
193
2677942
  CHECK(has_pointer_data());
194
2677928
  return pointer_data_;
195
}
196
197
695675
void BaseObject::decrease_refcount() {
198
695675
  CHECK(has_pointer_data());
199
695690
  PointerData* metadata = pointer_data();
200
695690
  CHECK_GT(metadata->strong_ptr_count, 0);
201
695690
  unsigned int new_refcount = --metadata->strong_ptr_count;
202
695690
  if (new_refcount == 0) {
203
227315
    if (metadata->is_detached) {
204
156065
      delete this;
205

112842
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
206
41592
      MakeWeak();
207
    }
208
  }
209
695699
}
210
211
697023
void BaseObject::increase_refcount() {
212
697023
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
213

925575
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
214
228524
    persistent_handle_.ClearWeak();
215
697052
}
216
217
template <typename T, bool kIsWeak>
218
BaseObject::PointerData*
219
1842760
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
220
  if (kIsWeak) {
221
1145692
    return data_.pointer_data;
222
  }
223







697068
  if (get_base_object() == nullptr) {
224
    return nullptr;
225
  }
226
697068
  return get_base_object()->pointer_data();
227
}
228
229
template <typename T, bool kIsWeak>
230
9114331
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
231
  if (kIsWeak) {
232
518026
    if (pointer_data() == nullptr) {
233
4
      return nullptr;
234
    }
235
518022
    return pointer_data()->self;
236
  }
237
8596305
  return data_.target;
238
}
239
240
template <typename T, bool kIsWeak>
241
1826696
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
242
  if (kIsWeak) {
243

97846
    if (pointer_data() != nullptr &&
244

60707
        --pointer_data()->weak_ptr_count == 0 &&
245
11784
        pointer_data()->self == nullptr) {
246
19
      delete pointer_data();
247
    }
248







1802232
  } else if (get() != nullptr) {
249
695735
    get()->decrease_refcount();
250
  }
251
1826705
}
252
253
template <typename T, bool kIsWeak>
254
1376465
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
255
1376465
  data_.target = nullptr;
256
1376465
}
257
258
template <typename T, bool kIsWeak>
259
1052143
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
260
1052143
  : BaseObjectPtrImpl() {
261







1052143
  if (target == nullptr) return;
262
  if (kIsWeak) {
263
24459
    data_.pointer_data = target->pointer_data();
264
24459
    CHECK_NOT_NULL(pointer_data());
265
24459
    pointer_data()->weak_ptr_count++;
266
  } else {
267
697067
    data_.target = target;
268







697067
    CHECK_NOT_NULL(pointer_data());
269
697068
    get()->increase_refcount();
270
  }
271
}
272
273
template <typename T, bool kIsWeak>
274
template <typename U, bool kW>
275
21910
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
276
    const BaseObjectPtrImpl<U, kW>& other)
277
21910
  : BaseObjectPtrImpl(other.get()) {}
278
279
template <typename T, bool kIsWeak>
280
438734
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
281
438734
  : BaseObjectPtrImpl(other.get()) {}
282
283
template <typename T, bool kIsWeak>
284
template <typename U, bool kW>
285
4
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
286
    const BaseObjectPtrImpl<U, kW>& other) {
287

4
  if (other.get() == get()) return *this;
288
4
  this->~BaseObjectPtrImpl();
289

4
  return *new (this) BaseObjectPtrImpl(other);
290
}
291
292
template <typename T, bool kIsWeak>
293
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
294
    const BaseObjectPtrImpl& other) {
295
  if (other.get() == get()) return *this;
296
  this->~BaseObjectPtrImpl();
297
  return *new (this) BaseObjectPtrImpl(other);
298
}
299
300
template <typename T, bool kIsWeak>
301
451751
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
302
451751
  : data_(other.data_) {
303
  if (kIsWeak)
304
2
    other.data_.target = nullptr;
305
  else
306
451749
    other.data_.pointer_data = nullptr;
307
451751
}
308
309
template <typename T, bool kIsWeak>
310
212743
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
311
    BaseObjectPtrImpl&& other) {
312



212743
  if (&other == this) return *this;
313
212743
  this->~BaseObjectPtrImpl();
314



212743
  return *new (this) BaseObjectPtrImpl(std::move(other));
315
}
316
317
template <typename T, bool kIsWeak>
318
153894
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
319
153894
  *this = BaseObjectPtrImpl(ptr);
320
153894
}
321
322
template <typename T, bool kIsWeak>
323
7720245
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
324
7720245
  return static_cast<T*>(get_base_object());
325
}
326
327
template <typename T, bool kIsWeak>
328
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
329
  return *get();
330
}
331
332
template <typename T, bool kIsWeak>
333
2403733
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
334
2403733
  return get();
335
}
336
337
template <typename T, bool kIsWeak>
338
729943
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
339
729943
  return get() != nullptr;
340
}
341
342
template <typename T, bool kIsWeak>
343
template <typename U, bool kW>
344
10723
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
345
    const BaseObjectPtrImpl<U, kW>& other) const {
346
10723
  return get() == other.get();
347
}
348
349
template <typename T, bool kIsWeak>
350
template <typename U, bool kW>
351
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
352
    const BaseObjectPtrImpl<U, kW>& other) const {
353
  return get() != other.get();
354
}
355
356
template <typename T, typename... Args>
357
12250
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
358
12250
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
359
}
360
361
template <typename T, typename... Args>
362
12180
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
363
12180
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
364
12180
  target->Detach();
365
12180
  return target;
366
}
367
368
}  // namespace node
369
370
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
371
372
#endif  // SRC_BASE_OBJECT_INL_H_