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: 155 159 97.5 %
Date: 2020-02-19 22:14:06 Branches: 86 124 69.4 %

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
563890
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
43
1127780
    : persistent_handle_(env->isolate(), object), env_(env) {
44
563890
  CHECK_EQ(false, object.IsEmpty());
45
563890
  CHECK_GT(object->InternalFieldCount(), 0);
46
563890
  object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
47
563890
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
48
563890
  env->modify_base_object_count(1);
49
563890
}
50
51

1675853
BaseObject::~BaseObject() {
52
558616
  env()->modify_base_object_count(-1);
53
558616
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
54
55
558619
  if (UNLIKELY(has_pointer_data())) {
56
75219
    PointerData* metadata = pointer_data();
57
75219
    CHECK_EQ(metadata->strong_ptr_count, 0);
58
75219
    metadata->self = nullptr;
59
75219
    if (metadata->weak_ptr_count == 0)
60
75205
      delete metadata;
61
  }
62
63
1117238
  if (persistent_handle_.IsEmpty()) {
64
    // This most likely happened because the weak callback below cleared it.
65
330524
    return;
66
  }
67
68
  {
69
456188
    v8::HandleScope handle_scope(env()->isolate());
70
456190
    object()->SetAlignedPointerInInternalField(0, nullptr);
71
  }
72
558619
}
73
74
73125
void BaseObject::Detach() {
75
73125
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
76
73125
  pointer_data()->is_detached = true;
77
73126
}
78
79
465217
v8::Global<v8::Object>& BaseObject::persistent() {
80
465217
  return persistent_handle_;
81
}
82
83
84
2026254
v8::Local<v8::Object> BaseObject::object() const {
85
2026254
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
86
}
87
88
85254
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
89
85254
  v8::Local<v8::Object> handle = object();
90
91
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
92
  DCHECK_EQ(env()->isolate(), isolate);
93
94
85261
  return handle;
95
}
96
97
10372858
Environment* BaseObject::env() const {
98
10372858
  return env_;
99
}
100
101
3761787
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
102
3761787
  CHECK_GT(obj->InternalFieldCount(), 0);
103
7523571
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
104
}
105
106
107
template <typename T>
108
133523
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
109
133523
  return static_cast<T*>(FromJSObject(object));
110
}
111
112
113
394788
void BaseObject::MakeWeak() {
114
394788
  if (has_pointer_data()) {
115
7584
    pointer_data()->wants_weak_jsobj = true;
116
7584
    if (pointer_data()->strong_ptr_count > 0) return;
117
  }
118
119
789574
  persistent_handle_.SetWeak(
120
      this,
121
1055833
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
122
330523
        BaseObject* obj = data.GetParameter();
123
        // Clear the persistent handle so that ~BaseObject() doesn't attempt
124
        // to mess with internal fields, since the JS object may have
125
        // transitioned into an invalid state.
126
        // Refs: https://github.com/nodejs/node/issues/18897
127
330523
        obj->persistent_handle_.Reset();
128

330523
        CHECK_IMPLIES(obj->has_pointer_data(),
129
                      obj->pointer_data()->strong_ptr_count == 0);
130
330523
        obj->OnGCCollect();
131
1055833
      }, v8::WeakCallbackType::kParameter);
132
}
133
134
330521
void BaseObject::OnGCCollect() {
135
330521
  delete this;
136
330521
}
137
138
1964
void BaseObject::ClearWeak() {
139
1964
  if (has_pointer_data())
140
    pointer_data()->wants_weak_jsobj = false;
141
142
1964
  persistent_handle_.ClearWeak();
143
1964
}
144
145
146
v8::Local<v8::FunctionTemplate>
147
15163
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
148
59177
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
149
    DCHECK(args.IsConstructCall());
150
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
151
44014
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
152
59177
  };
153
154
15163
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
155
30326
  t->InstanceTemplate()->SetInternalFieldCount(1);
156
15163
  return t;
157
}
158
159
template <int Field>
160
void BaseObject::InternalFieldGet(
161
    v8::Local<v8::String> property,
162
    const v8::PropertyCallbackInfo<v8::Value>& info) {
163
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
164
}
165
166
template <int Field, bool (v8::Value::* typecheck)() const>
167
94414
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
168
                                  v8::Local<v8::Value> value,
169
                                  const v8::PropertyCallbackInfo<void>& info) {
170
  // This could be e.g. value->IsFunction().
171
94414
  CHECK(((*value)->*typecheck)());
172
188828
  info.This()->SetInternalField(Field, value);
173
94414
}
174
175
2695886
bool BaseObject::has_pointer_data() const {
176
2695886
  return pointer_data_ != nullptr;
177
}
178
179
606258
BaseObject::PointerData* BaseObject::pointer_data() {
180
606258
  if (!has_pointer_data()) {
181
75248
    PointerData* metadata = new PointerData();
182
150496
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
183
75248
    metadata->self = this;
184
75248
    pointer_data_ = metadata;
185
  }
186
606262
  CHECK(has_pointer_data());
187
606259
  return pointer_data_;
188
}
189
190
114541
void BaseObject::decrease_refcount() {
191
114541
  CHECK(has_pointer_data());
192
114542
  PointerData* metadata = pointer_data();
193
114543
  CHECK_GT(metadata->strong_ptr_count, 0);
194
114543
  unsigned int new_refcount = --metadata->strong_ptr_count;
195
114543
  if (new_refcount == 0) {
196
80603
    if (metadata->is_detached) {
197
73019
      delete this;
198

15167
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
199
7583
      MakeWeak();
200
    }
201
  }
202
114543
}
203
204
114545
void BaseObject::increase_refcount() {
205
114545
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
206

195152
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
207
80604
    persistent_handle_.ClearWeak();
208
114546
}
209
210
template <typename T, bool kIsWeak>
211
BaseObject::PointerData*
212
1317886
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
213
  if (kIsWeak) {
214
1203341
    return data_.pointer_data;
215
  } else {
216


114545
    if (get_base_object() == nullptr) return nullptr;
217
114546
    return get_base_object()->pointer_data();
218
  }
219
}
220
221
template <typename T, bool kIsWeak>
222
1649172
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
223
  if (kIsWeak) {
224
560327
    if (pointer_data() == nullptr) return nullptr;
225
560321
    return pointer_data()->self;
226
  } else {
227
1088845
    return data_.target;
228
  }
229
}
230
231
template <typename T, bool kIsWeak>
232
185118
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
233


185118
  if (get() == nullptr) return;
234
  if (kIsWeak) {
235

35299
    if (--pointer_data()->weak_ptr_count == 0 &&
236
11616
        pointer_data()->self == nullptr) {
237
      delete pointer_data();
238
    }
239
  } else {
240
114543
    get()->decrease_refcount();
241
  }
242
138226
}
243
244
template <typename T, bool kIsWeak>
245
151485
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
246
151485
  data_.target = nullptr;
247
151485
}
248
249
template <typename T, bool kIsWeak>
250
138244
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
251
138244
  : BaseObjectPtrImpl() {
252


138244
  if (target == nullptr) return;
253
  if (kIsWeak) {
254
23697
    data_.pointer_data = target->pointer_data();
255
23697
    CHECK_NOT_NULL(pointer_data());
256
23697
    pointer_data()->weak_ptr_count++;
257
  } else {
258
114545
    data_.target = target;
259


114545
    CHECK_NOT_NULL(pointer_data());
260
114546
    get()->increase_refcount();
261
  }
262
}
263
264
template <typename T, bool kIsWeak>
265
template <typename U, bool kW>
266
17
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
267
    const BaseObjectPtrImpl<U, kW>& other)
268
17
  : BaseObjectPtrImpl(other.get()) {}
269
270
template <typename T, bool kIsWeak>
271
32537
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
272
32537
  : BaseObjectPtrImpl(other.get()) {}
273
274
template <typename T, bool kIsWeak>
275
template <typename U, bool kW>
276
4
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
277
    const BaseObjectPtrImpl<U, kW>& other) {
278

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

4
  return *new (this) BaseObjectPtrImpl(other);
281
}
282
283
template <typename T, bool kIsWeak>
284
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
285
    const BaseObjectPtrImpl& other) {
286
  if (other.get() == get()) return *this;
287
  this->~BaseObjectPtrImpl();
288
  return *new (this) BaseObjectPtrImpl(other);
289
}
290
291
template <typename T, bool kIsWeak>
292
33662
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
293
33662
  : data_(other.data_) {
294
  if (kIsWeak)
295
1
    other.data_.target = nullptr;
296
  else
297
33661
    other.data_.pointer_data = nullptr;
298
33662
}
299
300
template <typename T, bool kIsWeak>
301
512
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
302
    BaseObjectPtrImpl&& other) {
303

512
  if (&other == this) return *this;
304
512
  this->~BaseObjectPtrImpl();
305

512
  return *new (this) BaseObjectPtrImpl(std::move(other));
306
}
307
308
template <typename T, bool kIsWeak>
309
2
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
310
2
  *this = BaseObjectPtrImpl(ptr);
311
2
}
312
313
template <typename T, bool kIsWeak>
314
1420090
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
315
1420090
  return static_cast<T*>(get_base_object());
316
}
317
318
template <typename T, bool kIsWeak>
319
62962
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
320
62962
  return *get();
321
}
322
323
template <typename T, bool kIsWeak>
324
748591
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
325
748591
  return get();
326
}
327
328
template <typename T, bool kIsWeak>
329
24777
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
330
24777
  return get() != nullptr;
331
}
332
333
template <typename T, typename... Args>
334
633
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
335
633
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
336
}
337
338
template <typename T, typename... Args>
339
618
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
340
618
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
341
618
  target->Detach();
342
618
  return target;
343
}
344
345
}  // namespace node
346
347
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
348
349
#endif  // SRC_BASE_OBJECT_INL_H_