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: 158 162 97.5 %
Date: 2020-05-27 22:15:15 Branches: 105 158 66.5 %

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
#if (__GNUC__ >= 8) && !defined(__clang__)
32
#pragma GCC diagnostic push
33
#pragma GCC diagnostic ignored "-Wcast-function-type"
34
#endif
35
#include "v8.h"
36
#if (__GNUC__ >= 8) && !defined(__clang__)
37
#pragma GCC diagnostic pop
38
#endif
39
40
namespace node {
41
42
593869
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
43
1187738
    : persistent_handle_(env->isolate(), object), env_(env) {
44
593869
  CHECK_EQ(false, object.IsEmpty());
45
593869
  CHECK_GT(object->InternalFieldCount(), 0);
46
593872
  object->SetAlignedPointerInInternalField(
47
      BaseObject::kSlot,
48
593872
      static_cast<void*>(this));
49
593868
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
50
593872
  env->modify_base_object_count(1);
51
593872
}
52
53

1762748
BaseObject::~BaseObject() {
54
587581
  env()->modify_base_object_count(-1);
55
587582
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
56
57
587583
  if (UNLIKELY(has_pointer_data())) {
58
146761
    PointerData* metadata = pointer_data();
59
146761
    CHECK_EQ(metadata->strong_ptr_count, 0);
60
146761
    metadata->self = nullptr;
61
146761
    if (metadata->weak_ptr_count == 0)
62
146748
      delete metadata;
63
  }
64
65
1175164
  if (persistent_handle_.IsEmpty()) {
66
    // This most likely happened because the weak callback below cleared it.
67
349425
    return;
68
  }
69
70
  {
71
476314
    v8::HandleScope handle_scope(env()->isolate());
72
476313
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
73
  }
74
587584
}
75
76
144953
void BaseObject::Detach() {
77
144953
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
78
144953
  pointer_data()->is_detached = true;
79
144953
}
80
81
667139
v8::Global<v8::Object>& BaseObject::persistent() {
82
667139
  return persistent_handle_;
83
}
84
85
86
2483008
v8::Local<v8::Object> BaseObject::object() const {
87
2483008
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
88
}
89
90
83932
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
91
83932
  v8::Local<v8::Object> handle = object();
92
93
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
94
  DCHECK_EQ(env()->isolate(), isolate);
95
96
83931
  return handle;
97
}
98
99
11756172
Environment* BaseObject::env() const {
100
11756172
  return env_;
101
}
102
103
3934157
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
104
3934157
  v8::Local<v8::Object> obj = value.As<v8::Object>();
105
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
106
  return static_cast<BaseObject*>(
107
7868314
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
108
}
109
110
111
template <typename T>
112
134279
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
113
134279
  return static_cast<T*>(FromJSObject(object));
114
}
115
116
117
443082
void BaseObject::MakeWeak() {
118
443082
  if (has_pointer_data()) {
119
37939
    pointer_data()->wants_weak_jsobj = true;
120
37939
    if (pointer_data()->strong_ptr_count > 0) return;
121
  }
122
123
886164
  persistent_handle_.SetWeak(
124
      this,
125
1141931
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
126
349425
        BaseObject* obj = data.GetParameter();
127
        // Clear the persistent handle so that ~BaseObject() doesn't attempt
128
        // to mess with internal fields, since the JS object may have
129
        // transitioned into an invalid state.
130
        // Refs: https://github.com/nodejs/node/issues/18897
131
349425
        obj->persistent_handle_.Reset();
132

349425
        CHECK_IMPLIES(obj->has_pointer_data(),
133
                      obj->pointer_data()->strong_ptr_count == 0);
134
349425
        obj->OnGCCollect();
135
1141931
      }, v8::WeakCallbackType::kParameter);
136
}
137
138
349423
void BaseObject::OnGCCollect() {
139
349423
  delete this;
140
349423
}
141
142
2280
void BaseObject::ClearWeak() {
143
2280
  if (has_pointer_data())
144
    pointer_data()->wants_weak_jsobj = false;
145
146
2280
  persistent_handle_.ClearWeak();
147
2280
}
148
149
150
v8::Local<v8::FunctionTemplate>
151
15675
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
152
60171
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
153
    DCHECK(args.IsConstructCall());
154
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
155
44496
    args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156
60171
  };
157
158
15675
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
159
47025
  t->InstanceTemplate()->SetInternalFieldCount(
160
15675
      BaseObject::kInternalFieldCount);
161
15675
  return t;
162
}
163
164
template <int Field>
165
void BaseObject::InternalFieldGet(
166
    v8::Local<v8::String> property,
167
    const v8::PropertyCallbackInfo<v8::Value>& info) {
168
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
169
}
170
171
template <int Field, bool (v8::Value::* typecheck)() const>
172
94946
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
173
                                  v8::Local<v8::Value> value,
174
                                  const v8::PropertyCallbackInfo<void>& info) {
175
  // This could be e.g. value->IsFunction().
176
94946
  CHECK(((*value)->*typecheck)());
177
189892
  info.This()->SetInternalField(Field, value);
178
94946
}
179
180
6539351
bool BaseObject::has_pointer_data() const {
181
6539351
  return pointer_data_ != nullptr;
182
}
183
184
2251716
BaseObject::PointerData* BaseObject::pointer_data() {
185
2251716
  if (!has_pointer_data()) {
186
147368
    PointerData* metadata = new PointerData();
187
294736
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
188
147368
    metadata->self = this;
189
147368
    pointer_data_ = metadata;
190
  }
191
2251719
  CHECK(has_pointer_data());
192
2251716
  return pointer_data_;
193
}
194
195
570336
void BaseObject::decrease_refcount() {
196
570336
  CHECK(has_pointer_data());
197
570336
  PointerData* metadata = pointer_data();
198
570334
  CHECK_GT(metadata->strong_ptr_count, 0);
199
570334
  unsigned int new_refcount = --metadata->strong_ptr_count;
200
570334
  if (new_refcount == 0) {
201
182475
    if (metadata->is_detached) {
202
144269
      delete this;
203

76144
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
204
37938
      MakeWeak();
205
    }
206
  }
207
570334
}
208
209
570967
void BaseObject::increase_refcount() {
210
570967
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
211

754024
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
212
183055
    persistent_handle_.ClearWeak();
213
570967
}
214
215
template <typename T, bool kIsWeak>
216
BaseObject::PointerData*
217
1713419
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
218
  if (kIsWeak) {
219
1142453
    return data_.pointer_data;
220
  }
221



570966
  if (get_base_object() == nullptr) {
222
    return nullptr;
223
  }
224
570966
  return get_base_object()->pointer_data();
225
}
226
227
template <typename T, bool kIsWeak>
228
6949767
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
229
  if (kIsWeak) {
230
516435
    if (pointer_data() == nullptr) {
231
4
      return nullptr;
232
    }
233
516431
    return pointer_data()->self;
234
  }
235
6433332
  return data_.target;
236
}
237
238
template <typename T, bool kIsWeak>
239
1024823
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
240
  if (kIsWeak) {
241

97766
    if (pointer_data() != nullptr &&
242

60696
        --pointer_data()->weak_ptr_count == 0 &&
243
11813
        pointer_data()->self == nullptr) {
244
13
      delete pointer_data();
245
    }
246



1000379
  } else if (get() != nullptr) {
247
570336
    get()->decrease_refcount();
248
  }
249
1024824
}
250
251
template <typename T, bool kIsWeak>
252
794090
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
253
794090
  data_.target = nullptr;
254
794090
}
255
256
template <typename T, bool kIsWeak>
257
720726
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
258
720726
  : BaseObjectPtrImpl() {
259




720726
  if (target == nullptr) return;
260
  if (kIsWeak) {
261
24439
    data_.pointer_data = target->pointer_data();
262
24439
    CHECK_NOT_NULL(pointer_data());
263
24439
    pointer_data()->weak_ptr_count++;
264
  } else {
265
570966
    data_.target = target;
266



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

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

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


173770
  if (&other == this) return *this;
311
173770
  this->~BaseObjectPtrImpl();
312


173770
  return *new (this) BaseObjectPtrImpl(std::move(other));
313
}
314
315
template <typename T, bool kIsWeak>
316
125321
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
317
125321
  *this = BaseObjectPtrImpl(ptr);
318
125321
}
319
320
template <typename T, bool kIsWeak>
321
5807840
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
322
5807840
  return static_cast<T*>(get_base_object());
323
}
324
325
template <typename T, bool kIsWeak>
326
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
327
  return *get();
328
}
329
330
template <typename T, bool kIsWeak>
331
2189115
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
332
2189115
  return get();
333
}
334
335
template <typename T, bool kIsWeak>
336
542778
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
337
542778
  return get() != nullptr;
338
}
339
340
template <typename T, typename... Args>
341
6677
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
342
6677
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
343
}
344
345
template <typename T, typename... Args>
346
6662
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
347
6662
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
348
6662
  target->Detach();
349
6662
  return target;
350
}
351
352
}  // namespace node
353
354
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
355
356
#endif  // SRC_BASE_OBJECT_INL_H_