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

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

42728
        CHECK_IMPLIES(obj->has_pointer_data(),
126
                      obj->pointer_data()->strong_ptr_count == 0);
127
42728
        obj->OnGCCollect();
128
249365
      }, v8::WeakCallbackType::kParameter);
129
}
130
131
42726
void BaseObject::OnGCCollect() {
132
42726
  delete this;
133
42726
}
134
135
2493
void BaseObject::ClearWeak() {
136
2493
  if (has_pointer_data())
137
    pointer_data()->wants_weak_jsobj = false;
138
139
2493
  persistent_handle_.ClearWeak();
140
2493
}
141
142
bool BaseObject::IsWeakOrDetached() const {
143
  if (persistent_handle_.IsWeak()) return true;
144
145
  if (!has_pointer_data()) return false;
146
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
147
  return pd->wants_weak_jsobj || pd->is_detached;
148
}
149
150
v8::Local<v8::FunctionTemplate>
151
17028
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
152
63176
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
153
    DCHECK(args.IsConstructCall());
154
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
155
46148
    args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156
63176
  };
157
158
17028
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
159
34056
  t->Inherit(BaseObject::GetConstructorTemplate(env));
160
51084
  t->InstanceTemplate()->SetInternalFieldCount(
161
17028
      BaseObject::kInternalFieldCount);
162
17028
  return t;
163
}
164
165
template <int Field>
166
void BaseObject::InternalFieldGet(
167
    v8::Local<v8::String> property,
168
    const v8::PropertyCallbackInfo<v8::Value>& info) {
169
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
170
}
171
172
template <int Field, bool (v8::Value::* typecheck)() const>
173
97124
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
174
                                  v8::Local<v8::Value> value,
175
                                  const v8::PropertyCallbackInfo<void>& info) {
176
  // This could be e.g. value->IsFunction().
177
97124
  CHECK(((*value)->*typecheck)());
178
194248
  info.This()->SetInternalField(Field, value);
179
97124
}
180
181
6577170
bool BaseObject::has_pointer_data() const {
182
6577170
  return pointer_data_ != nullptr;
183
}
184
185
2627781
BaseObject::PointerData* BaseObject::pointer_data() {
186
2627781
  if (!has_pointer_data()) {
187
157213
    PointerData* metadata = new PointerData();
188
314420
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
189
157210
    metadata->self = this;
190
157210
    pointer_data_ = metadata;
191
  }
192
2627733
  CHECK(has_pointer_data());
193
2627583
  return pointer_data_;
194
}
195
196
685018
void BaseObject::decrease_refcount() {
197
685018
  CHECK(has_pointer_data());
198
685012
  PointerData* metadata = pointer_data();
199
684948
  CHECK_GT(metadata->strong_ptr_count, 0);
200
684948
  unsigned int new_refcount = --metadata->strong_ptr_count;
201
684948
  if (new_refcount == 0) {
202
222169
    if (metadata->is_detached) {
203
151236
      delete this;
204

112424
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
205
41491
      MakeWeak();
206
    }
207
  }
208
684957
}
209
210
685628
void BaseObject::increase_refcount() {
211
685628
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
212

908358
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
213
222691
    persistent_handle_.ClearWeak();
214
685687
}
215
216
template <typename T, bool kIsWeak>
217
BaseObject::PointerData*
218
1831067
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
219
  if (kIsWeak) {
220
1145338
    return data_.pointer_data;
221
  }
222



685729
  if (get_base_object() == nullptr) {
223
    return nullptr;
224
  }
225
685730
  return get_base_object()->pointer_data();
226
}
227
228
template <typename T, bool kIsWeak>
229
9235531
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
230
  if (kIsWeak) {
231
517848
    if (pointer_data() == nullptr) {
232
4
      return nullptr;
233
    }
234
517844
    return pointer_data()->self;
235
  }
236
8717683
  return data_.target;
237
}
238
239
template <typename T, bool kIsWeak>
240
2064069
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
241
  if (kIsWeak) {
242

97854
    if (pointer_data() != nullptr &&
243

60711
        --pointer_data()->weak_ptr_count == 0 &&
244
11784
        pointer_data()->self == nullptr) {
245
13
      delete pointer_data();
246
    }
247



2039603
  } else if (get() != nullptr) {
248
685092
    get()->decrease_refcount();
249
  }
250
2064079
}
251
252
template <typename T, bool kIsWeak>
253
1613708
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
254
1613708
  data_.target = nullptr;
255
1613708
}
256
257
template <typename T, bool kIsWeak>
258
1040764
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
259
1040764
  : BaseObjectPtrImpl() {
260




1040764
  if (target == nullptr) return;
261
  if (kIsWeak) {
262
24461
    data_.pointer_data = target->pointer_data();
263
24461
    CHECK_NOT_NULL(pointer_data());
264
24461
    pointer_data()->weak_ptr_count++;
265
  } else {
266
685731
    data_.target = target;
267



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

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

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



212470
  if (&other == this) return *this;
312
212470
  this->~BaseObjectPtrImpl();
313



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