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-27 22:14:15 Branches: 87 124 70.2 %

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

1677417
BaseObject::~BaseObject() {
52
559138
  env()->modify_base_object_count(-1);
53
559137
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
54
55
559138
  if (UNLIKELY(has_pointer_data())) {
56
75128
    PointerData* metadata = pointer_data();
57
75127
    CHECK_EQ(metadata->strong_ptr_count, 0);
58
75127
    metadata->self = nullptr;
59
75127
    if (metadata->weak_ptr_count == 0)
60
75113
      delete metadata;
61
  }
62
63
1118272
  if (persistent_handle_.IsEmpty()) {
64
    // This most likely happened because the weak callback below cleared it.
65
331119
    return;
66
  }
67
68
  {
69
456035
    v8::HandleScope handle_scope(env()->isolate());
70
456039
    object()->SetAlignedPointerInInternalField(0, nullptr);
71
  }
72
559139
}
73
74
73029
void BaseObject::Detach() {
75
73029
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
76
73028
  pointer_data()->is_detached = true;
77
73030
}
78
79
465075
v8::Global<v8::Object>& BaseObject::persistent() {
80
465075
  return persistent_handle_;
81
}
82
83
84
2025109
v8::Local<v8::Object> BaseObject::object() const {
85
2025109
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
86
}
87
88
85037
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
89
85037
  v8::Local<v8::Object> handle = object();
90
91
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
92
  DCHECK_EQ(env()->isolate(), isolate);
93
94
85039
  return handle;
95
}
96
97
10363014
Environment* BaseObject::env() const {
98
10363014
  return env_;
99
}
100
101
3774937
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
102
3774937
  CHECK_GT(obj->InternalFieldCount(), 0);
103
7549902
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
104
}
105
106
107
template <typename T>
108
133587
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
109
133587
  return static_cast<T*>(FromJSObject(object));
110
}
111
112
113
395184
void BaseObject::MakeWeak() {
114
395184
  if (has_pointer_data()) {
115
7560
    pointer_data()->wants_weak_jsobj = true;
116
7560
    if (pointer_data()->strong_ptr_count > 0) return;
117
  }
118
119
790369
  persistent_handle_.SetWeak(
120
      this,
121
1057424
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
122
331119
        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
331119
        obj->persistent_handle_.Reset();
128

331119
        CHECK_IMPLIES(obj->has_pointer_data(),
129
                      obj->pointer_data()->strong_ptr_count == 0);
130
331119
        obj->OnGCCollect();
131
1057424
      }, v8::WeakCallbackType::kParameter);
132
}
133
134
331117
void BaseObject::OnGCCollect() {
135
331117
  delete this;
136
331117
}
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
15211
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
148
59169
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
149
    DCHECK(args.IsConstructCall());
150
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
151
43958
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
152
59169
  };
153
154
15211
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
155
30422
  t->InstanceTemplate()->SetInternalFieldCount(1);
156
15211
  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
94318
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
94318
  CHECK(((*value)->*typecheck)());
172
188636
  info.This()->SetInternalField(Field, value);
173
94318
}
174
175
2694991
bool BaseObject::has_pointer_data() const {
176
2694991
  return pointer_data_ != nullptr;
177
}
178
179
605108
BaseObject::PointerData* BaseObject::pointer_data() {
180
605108
  if (!has_pointer_data()) {
181
75155
    PointerData* metadata = new PointerData();
182
150312
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
183
75156
    metadata->self = this;
184
75156
    pointer_data_ = metadata;
185
  }
186
605114
  CHECK(has_pointer_data());
187
605111
  return pointer_data_;
188
}
189
190
114301
void BaseObject::decrease_refcount() {
191
114301
  CHECK(has_pointer_data());
192
114301
  PointerData* metadata = pointer_data();
193
114300
  CHECK_GT(metadata->strong_ptr_count, 0);
194
114300
  unsigned int new_refcount = --metadata->strong_ptr_count;
195
114300
  if (new_refcount == 0) {
196
80484
    if (metadata->is_detached) {
197
72924
      delete this;
198

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

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


114303
    if (get_base_object() == nullptr) return nullptr;
217
114303
    return get_base_object()->pointer_data();
218
  }
219
}
220
221
template <typename T, bool kIsWeak>
222
1645691
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
223
  if (kIsWeak) {
224
558383
    if (pointer_data() == nullptr) return nullptr;
225
558377
    return pointer_data()->self;
226
  } else {
227
1087308
    return data_.target;
228
  }
229
}
230
231
template <typename T, bool kIsWeak>
232
184665
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
233


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

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


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


114302
    CHECK_NOT_NULL(pointer_data());
260
114305
    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
32417
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
272
32417
  : 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
33542
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
293
33542
  : data_(other.data_) {
294
  if (kIsWeak)
295
1
    other.data_.target = nullptr;
296
  else
297
33541
    other.data_.pointer_data = nullptr;
298
33542
}
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
1417104
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
315
1417104
  return static_cast<T*>(get_base_object());
316
}
317
318
template <typename T, bool kIsWeak>
319
62720
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
320
62720
  return *get();
321
}
322
323
template <typename T, bool kIsWeak>
324
747438
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
325
747438
  return get();
326
}
327
328
template <typename T, bool kIsWeak>
329
24683
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
330
24683
  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_