GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 101 108 93.5 %
Date: 2022-08-30 04:20:47 Branches: 24 42 57.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
179386
void BaseObject::Detach() {
36
179386
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
37
179386
  pointer_data()->is_detached = true;
38
179386
}
39
40
464273
v8::Global<v8::Object>& BaseObject::persistent() {
41
464273
  return persistent_handle_;
42
}
43
44
45
2484410
v8::Local<v8::Object> BaseObject::object() const {
46
2484410
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
47
}
48
49
139285
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
50
139285
  v8::Local<v8::Object> handle = object();
51
52
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
53
            isolate);
54
  DCHECK_EQ(env()->isolate(), isolate);
55
56
139285
  return handle;
57
}
58
59
17009299
Environment* BaseObject::env() const {
60
17009299
  return env_;
61
}
62
63
9889674
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
64
9889674
  v8::Local<v8::Object> obj = value.As<v8::Object>();
65
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
66
  return static_cast<BaseObject*>(
67
19779348
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
68
}
69
70
71
template <typename T>
72
768950
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
73
768950
  return static_cast<T*>(FromJSObject(object));
74
}
75
76
1250397
void BaseObject::OnGCCollect() {
77
1250397
  delete this;
78
1250397
}
79
80
2986
void BaseObject::ClearWeak() {
81
2986
  if (has_pointer_data())
82
    pointer_data()->wants_weak_jsobj = false;
83
84
2986
  persistent_handle_.ClearWeak();
85
2986
}
86
87
bool BaseObject::IsWeakOrDetached() const {
88
  if (persistent_handle_.IsWeak()) return true;
89
90
  if (!has_pointer_data()) return false;
91
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
92
  return pd->wants_weak_jsobj || pd->is_detached;
93
}
94
95
template <int Field>
96
8
void BaseObject::InternalFieldGet(
97
    v8::Local<v8::String> property,
98
    const v8::PropertyCallbackInfo<v8::Value>& info) {
99
8
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
100
8
}
101
102
template <int Field, bool (v8::Value::* typecheck)() const>
103
99343
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
104
                                  v8::Local<v8::Value> value,
105
                                  const v8::PropertyCallbackInfo<void>& info) {
106
  // This could be e.g. value->IsFunction().
107
99343
  CHECK(((*value)->*typecheck)());
108
99343
  info.This()->SetInternalField(Field, value);
109
99343
}
110
111
10412249
bool BaseObject::has_pointer_data() const {
112
10412249
  return pointer_data_ != nullptr;
113
}
114
115
template <typename T, bool kIsWeak>
116
BaseObject::PointerData*
117
3739552
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
118
  if constexpr (kIsWeak) {
119
2306842
    return data_.pointer_data;
120
  }
121
1432710
  if (get_base_object() == nullptr) {
122
    return nullptr;
123
  }
124
1432710
  return get_base_object()->pointer_data();
125
}
126
127
template <typename T, bool kIsWeak>
128
17109271
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
129
  if constexpr (kIsWeak) {
130
1040978
    if (pointer_data() == nullptr) {
131
8
      return nullptr;
132
    }
133
1040970
    return pointer_data()->self;
134
  }
135
16068293
  return data_.target;
136
}
137
138
template <typename T, bool kIsWeak>
139
3423386
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
140
  if constexpr (kIsWeak) {
141
50232
    if (pointer_data() != nullptr &&
142

74160
        --pointer_data()->weak_ptr_count == 0 &&
143
23928
        pointer_data()->self == nullptr) {
144
36
      delete pointer_data();
145
    }
146
3373154
  } else if (get() != nullptr) {
147
1286407
    get()->decrease_refcount();
148
  }
149
3423386
}
150
151
template <typename T, bool kIsWeak>
152
2812977
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
153
2812977
  data_.target = nullptr;
154
2812977
}
155
156
template <typename T, bool kIsWeak>
157
2150489
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
158
2150489
  : BaseObjectPtrImpl() {
159
2150489
  if (target == nullptr) return;
160
  if constexpr (kIsWeak) {
161
50238
    data_.pointer_data = target->pointer_data();
162
50238
    CHECK_NOT_NULL(pointer_data());
163
50238
    pointer_data()->weak_ptr_count++;
164
  } else {
165
1432710
    data_.target = target;
166
1432710
    CHECK_NOT_NULL(pointer_data());
167
1432710
    get()->increase_refcount();
168
  }
169
}
170
171
template <typename T, bool kIsWeak>
172
template <typename U, bool kW>
173
42929
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
174
    const BaseObjectPtrImpl<U, kW>& other)
175
42929
  : BaseObjectPtrImpl(other.get()) {}
176
177
template <typename T, bool kIsWeak>
178
854348
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
179
854348
  : BaseObjectPtrImpl(other.get()) {}
180
181
template <typename T, bool kIsWeak>
182
template <typename U, bool kW>
183
8
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
184
    const BaseObjectPtrImpl<U, kW>& other) {
185
8
  if (other.get() == get()) return *this;
186
8
  this->~BaseObjectPtrImpl();
187
8
  return *new (this) BaseObjectPtrImpl(other);
188
}
189
190
template <typename T, bool kIsWeak>
191
2
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
192
    const BaseObjectPtrImpl& other) {
193
2
  if (other.get() == get()) return *this;
194
2
  this->~BaseObjectPtrImpl();
195
2
  return *new (this) BaseObjectPtrImpl(other);
196
}
197
198
template <typename T, bool kIsWeak>
199
882425
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
200
882425
  : data_(other.data_) {
201
  if constexpr (kIsWeak)
202
4
    other.data_.target = nullptr;
203
  else
204
882421
    other.data_.pointer_data = nullptr;
205
882425
}
206
207
template <typename T, bool kIsWeak>
208
426698
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
209
    BaseObjectPtrImpl&& other) {
210
426698
  if (&other == this) return *this;
211
426698
  this->~BaseObjectPtrImpl();
212
426698
  return *new (this) BaseObjectPtrImpl(std::move(other));
213
}
214
215
template <typename T, bool kIsWeak>
216
260462
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
217
260462
  *this = BaseObjectPtrImpl(ptr);
218
260462
}
219
220
template <typename T, bool kIsWeak>
221
14430059
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
222
14430059
  return static_cast<T*>(get_base_object());
223
}
224
225
template <typename T, bool kIsWeak>
226
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
227
  return *get();
228
}
229
230
template <typename T, bool kIsWeak>
231
4120490
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
232
4120490
  return get();
233
}
234
235
template <typename T, bool kIsWeak>
236
1235344
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
237
1235344
  return get() != nullptr;
238
}
239
240
template <typename T, bool kIsWeak>
241
template <typename U, bool kW>
242
11142
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
243
    const BaseObjectPtrImpl<U, kW>& other) const {
244
11142
  return get() == other.get();
245
}
246
247
template <typename T, bool kIsWeak>
248
template <typename U, bool kW>
249
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
250
    const BaseObjectPtrImpl<U, kW>& other) const {
251
  return get() != other.get();
252
}
253
254
template <typename T, typename... Args>
255
40526
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
256
40526
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
257
}
258
259
template <typename T, typename... Args>
260
26774
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
261
26774
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
262
26774
  target->Detach();
263
26774
  return target;
264
}
265
266
}  // namespace node
267
268
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
269
270
#endif  // SRC_BASE_OBJECT_INL_H_