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 169 95.3 %
Date: 2020-06-24 22:13:30 Branches: 105 162 64.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
#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
605522
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
43
1211044
    : persistent_handle_(env->isolate(), object), env_(env) {
44
605522
  CHECK_EQ(false, object.IsEmpty());
45
605522
  CHECK_GT(object->InternalFieldCount(), 0);
46
605522
  object->SetAlignedPointerInInternalField(
47
      BaseObject::kSlot,
48
605522
      static_cast<void*>(this));
49
605522
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
50
605522
  env->modify_base_object_count(1);
51
605522
}
52
53

1795188
BaseObject::~BaseObject() {
54
598393
  env()->modify_base_object_count(-1);
55
598391
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
56
57
598396
  if (UNLIKELY(has_pointer_data())) {
58
147328
    PointerData* metadata = pointer_data();
59
147328
    CHECK_EQ(metadata->strong_ptr_count, 0);
60
147328
    metadata->self = nullptr;
61
147328
    if (metadata->weak_ptr_count == 0)
62
147315
      delete metadata;
63
  }
64
65
1196802
  if (persistent_handle_.IsEmpty()) {
66
    // This most likely happened because the weak callback below cleared it.
67
353939
    return;
68
  }
69
70
  {
71
488922
    v8::HandleScope handle_scope(env()->isolate());
72
488923
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
73
  }
74
598397
}
75
76
145513
void BaseObject::Detach() {
77
145513
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
78
145513
  pointer_data()->is_detached = true;
79
145513
}
80
81
668366
v8::Global<v8::Object>& BaseObject::persistent() {
82
668366
  return persistent_handle_;
83
}
84
85
86
2495032
v8::Local<v8::Object> BaseObject::object() const {
87
2495032
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
88
}
89
90
83914
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
91
83914
  v8::Local<v8::Object> handle = object();
92
93
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
94
  DCHECK_EQ(env()->isolate(), isolate);
95
96
83915
  return handle;
97
}
98
99
11907370
Environment* BaseObject::env() const {
100
11907370
  return env_;
101
}
102
103
5317601
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
104
5317601
  v8::Local<v8::Object> obj = value.As<v8::Object>();
105
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
106
  return static_cast<BaseObject*>(
107
10635201
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
108
}
109
110
111
template <typename T>
112
1429602
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
113
1429602
  return static_cast<T*>(FromJSObject(object));
114
}
115
116
117
452962
void BaseObject::MakeWeak() {
118
452962
  if (has_pointer_data()) {
119
37968
    pointer_data()->wants_weak_jsobj = true;
120
37968
    if (pointer_data()->strong_ptr_count > 0) return;
121
  }
122
123
905922
  persistent_handle_.SetWeak(
124
      this,
125
1160829
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
126
353934
        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
353934
        obj->persistent_handle_.Reset();
132

353934
        CHECK_IMPLIES(obj->has_pointer_data(),
133
                      obj->pointer_data()->strong_ptr_count == 0);
134
353934
        obj->OnGCCollect();
135
1160829
      }, v8::WeakCallbackType::kParameter);
136
}
137
138
353932
void BaseObject::OnGCCollect() {
139
353932
  delete this;
140
353932
}
141
142
2285
void BaseObject::ClearWeak() {
143
2285
  if (has_pointer_data())
144
    pointer_data()->wants_weak_jsobj = false;
145
146
2285
  persistent_handle_.ClearWeak();
147
2285
}
148
149
150
v8::Local<v8::FunctionTemplate>
151
15970
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
152
60764
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
153
    DCHECK(args.IsConstructCall());
154
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
155
44794
    args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156
60764
  };
157
158
15970
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
159
31940
  t->Inherit(BaseObject::GetConstructorTemplate(env));
160
47910
  t->InstanceTemplate()->SetInternalFieldCount(
161
15970
      BaseObject::kInternalFieldCount);
162
15970
  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
95018
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
95018
  CHECK(((*value)->*typecheck)());
178
190036
  info.This()->SetInternalField(Field, value);
179
95018
}
180
181
7019990
bool BaseObject::has_pointer_data() const {
182
7019990
  return pointer_data_ != nullptr;
183
}
184
185
2444798
BaseObject::PointerData* BaseObject::pointer_data() {
186
2444798
  if (!has_pointer_data()) {
187
147973
    PointerData* metadata = new PointerData();
188
295946
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
189
147973
    metadata->self = this;
190
147973
    pointer_data_ = metadata;
191
  }
192
2444874
  CHECK(has_pointer_data());
193
2444927
  return pointer_data_;
194
}
195
196
634190
void BaseObject::decrease_refcount() {
197
634190
  CHECK(has_pointer_data());
198
634212
  PointerData* metadata = pointer_data();
199
634211
  CHECK_GT(metadata->strong_ptr_count, 0);
200
634211
  unsigned int new_refcount = --metadata->strong_ptr_count;
201
634211
  if (new_refcount == 0) {
202
203950
    if (metadata->is_detached) {
203
144784
      delete this;
204

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

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



634896
  if (get_base_object() == nullptr) {
223
    return nullptr;
224
  }
225
634897
  return get_base_object()->pointer_data();
226
}
227
228
template <typename T, bool kIsWeak>
229
7489282
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
230
  if (kIsWeak) {
231
516467
    if (pointer_data() == nullptr) {
232
4
      return nullptr;
233
    }
234
516463
    return pointer_data()->self;
235
  }
236
6972815
  return data_.target;
237
}
238
239
template <typename T, bool kIsWeak>
240
1131002
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
241
  if (kIsWeak) {
242

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

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



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




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



634896
    CHECK_NOT_NULL(pointer_data());
268
634896
    get()->increase_refcount();
269
  }
270
}
271
272
template <typename T, bool kIsWeak>
273
template <typename U, bool kW>
274
16501
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
275
    const BaseObjectPtrImpl<U, kW>& other)
276
16501
  : BaseObjectPtrImpl(other.get()) {}
277
278
template <typename T, bool kIsWeak>
279
269950
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
280
269950
  : 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
263221
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
301
263221
  : data_(other.data_) {
302
  if (kIsWeak)
303
2
    other.data_.target = nullptr;
304
  else
305
263219
    other.data_.pointer_data = nullptr;
306
263221
}
307
308
template <typename T, bool kIsWeak>
309
184232
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
310
    BaseObjectPtrImpl&& other) {
311


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


184232
  return *new (this) BaseObjectPtrImpl(std::move(other));
314
}
315
316
template <typename T, bool kIsWeak>
317
125427
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
318
125427
  *this = BaseObjectPtrImpl(ptr);
319
125427
}
320
321
template <typename T, bool kIsWeak>
322
6219499
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
323
6219499
  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
2245115
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
333
2245115
  return get();
334
}
335
336
template <typename T, bool kIsWeak>
337
574455
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
338
574455
  return get() != nullptr;
339
}
340
341
template <typename T, bool kIsWeak>
342
template <typename U, bool kW>
343
10593
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
344
    const BaseObjectPtrImpl<U, kW>& other) const {
345
10593
  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
6817
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
357
6817
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
358
}
359
360
template <typename T, typename... Args>
361
6802
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
362
6802
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
363
6802
  target->Detach();
364
6802
  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_