GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 123 124 99.2 %
Date: 2022-11-04 03:21:42 Branches: 35 44 79.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
#include "v8.h"
32
33
namespace node {
34
35
1462235
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
36
1462235
    : BaseObject(env->principal_realm(), object) {}
37
38
// static
39
33890
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
40
    Environment* env) {
41
33890
  return BaseObject::GetConstructorTemplate(env->isolate_data());
42
}
43
44
260888
void BaseObject::Detach() {
45
260888
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
46
260888
  pointer_data()->is_detached = true;
47
260888
}
48
49
392063
v8::Global<v8::Object>& BaseObject::persistent() {
50
392063
  return persistent_handle_;
51
}
52
53
54
2524681
v8::Local<v8::Object> BaseObject::object() const {
55
2524681
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
56
}
57
58
139975
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
59
139975
  v8::Local<v8::Object> handle = object();
60
61
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
62
            isolate);
63
  DCHECK_EQ(env()->isolate(), isolate);
64
65
139975
  return handle;
66
}
67
68
16080512
Environment* BaseObject::env() const {
69
16080512
  return realm_->env();
70
}
71
72
3264898
Realm* BaseObject::realm() const {
73
3264898
  return realm_;
74
}
75
76
1486375
void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
77
  DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
78
1486375
  object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
79
                                           &kNodeEmbedderId);
80
1486375
}
81
82
1486375
void BaseObject::SetInternalFields(v8::Local<v8::Object> object, void* slot) {
83
1486375
  TagNodeObject(object);
84
1486375
  object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot);
85
1486375
}
86
87
9621833
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
88
9621833
  v8::Local<v8::Object> obj = value.As<v8::Object>();
89
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kInternalFieldCount);
90
  return static_cast<BaseObject*>(
91
19243666
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
92
}
93
94
template <typename T>
95
366052
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
96
366052
  return static_cast<T*>(FromJSObject(object));
97
}
98
99
1271662
void BaseObject::OnGCCollect() {
100
1271662
  delete this;
101
1271662
}
102
103
81845
void BaseObject::ClearWeak() {
104
81845
  if (has_pointer_data())
105
8107
    pointer_data()->wants_weak_jsobj = false;
106
107
81845
  persistent_handle_.ClearWeak();
108
81845
}
109
110
645
bool BaseObject::IsWeakOrDetached() const {
111
1290
  if (persistent_handle_.IsWeak()) return true;
112
113
457
  if (!has_pointer_data()) return false;
114
118
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
115

118
  return pd->wants_weak_jsobj || pd->is_detached;
116
}
117
118
645
v8::EmbedderGraph::Node::Detachedness BaseObject::GetDetachedness() const {
119
645
  return IsWeakOrDetached() ? v8::EmbedderGraph::Node::Detachedness::kDetached
120
645
                            : v8::EmbedderGraph::Node::Detachedness::kUnknown;
121
}
122
123
template <int Field>
124
8
void BaseObject::InternalFieldGet(
125
    v8::Local<v8::String> property,
126
    const v8::PropertyCallbackInfo<v8::Value>& info) {
127
8
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
128
8
}
129
130
template <int Field, bool (v8::Value::* typecheck)() const>
131
100038
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
132
                                  v8::Local<v8::Value> value,
133
                                  const v8::PropertyCallbackInfo<void>& info) {
134
  // This could be e.g. value->IsFunction().
135
100038
  CHECK(((*value)->*typecheck)());
136
100038
  info.This()->SetInternalField(Field, value);
137
100038
}
138
139
11782367
bool BaseObject::has_pointer_data() const {
140
11782367
  return pointer_data_ != nullptr;
141
}
142
143
template <typename T, bool kIsWeak>
144
BaseObject::PointerData*
145
3952224
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
146
  if constexpr (kIsWeak) {
147
2308508
    return data_.pointer_data;
148
  }
149
1643716
  if (get_base_object() == nullptr) {
150
    return nullptr;
151
  }
152
1643716
  return get_base_object()->pointer_data();
153
}
154
155
template <typename T, bool kIsWeak>
156
18828327
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
157
  if constexpr (kIsWeak) {
158
1041844
    if (pointer_data() == nullptr) {
159
8
      return nullptr;
160
    }
161
1041836
    return pointer_data()->self;
162
  }
163
17786483
  return data_.target;
164
}
165
166
template <typename T, bool kIsWeak>
167
3827951
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
168
  if constexpr (kIsWeak) {
169
50216
    if (pointer_data() != nullptr &&
170

74136
        --pointer_data()->weak_ptr_count == 0 &&
171
23920
        pointer_data()->self == nullptr) {
172
42
      delete pointer_data();
173
    }
174
3777735
  } else if (get() != nullptr) {
175
1559385
    get()->decrease_refcount();
176
  }
177
3827951
}
178
179
template <typename T, bool kIsWeak>
180
3034844
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
181
3034844
  data_.target = nullptr;
182
3034844
}
183
184
template <typename T, bool kIsWeak>
185
2365818
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
186
2365818
  : BaseObjectPtrImpl() {
187
2365818
  if (target == nullptr) return;
188
  if constexpr (kIsWeak) {
189
50222
    data_.pointer_data = target->pointer_data();
190
50222
    CHECK_NOT_NULL(pointer_data());
191
50222
    pointer_data()->weak_ptr_count++;
192
  } else {
193
1643716
    data_.target = target;
194
1643716
    CHECK_NOT_NULL(pointer_data());
195
1643716
    get()->increase_refcount();
196
  }
197
}
198
199
template <typename T, bool kIsWeak>
200
template <typename U, bool kW>
201
43789
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
202
    const BaseObjectPtrImpl<U, kW>& other)
203
43789
  : BaseObjectPtrImpl(other.get()) {}
204
205
template <typename T, bool kIsWeak>
206
883260
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
207
883260
  : BaseObjectPtrImpl(other.get()) {}
208
209
template <typename T, bool kIsWeak>
210
template <typename U, bool kW>
211
8
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
212
    const BaseObjectPtrImpl<U, kW>& other) {
213
8
  if (other.get() == get()) return *this;
214
8
  this->~BaseObjectPtrImpl();
215
8
  return *new (this) BaseObjectPtrImpl(other);
216
}
217
218
template <typename T, bool kIsWeak>
219
2
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
220
    const BaseObjectPtrImpl& other) {
221
2
  if (other.get() == get()) return *this;
222
2
  this->~BaseObjectPtrImpl();
223
2
  return *new (this) BaseObjectPtrImpl(other);
224
}
225
226
template <typename T, bool kIsWeak>
227
888705
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
228
888705
  : data_(other.data_) {
229
  if constexpr (kIsWeak)
230
4
    other.data_.target = nullptr;
231
  else
232
888701
    other.data_.pointer_data = nullptr;
233
888705
}
234
235
template <typename T, bool kIsWeak>
236
430562
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
237
    BaseObjectPtrImpl&& other) {
238
430562
  if (&other == this) return *this;
239
430562
  this->~BaseObjectPtrImpl();
240
430562
  return *new (this) BaseObjectPtrImpl(std::move(other));
241
}
242
243
template <typename T, bool kIsWeak>
244
261926
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
245
261926
  *this = BaseObjectPtrImpl(ptr);
246
261926
}
247
248
template <typename T, bool kIsWeak>
249
15728539
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
250
15728539
  return static_cast<T*>(get_base_object());
251
}
252
253
template <typename T, bool kIsWeak>
254
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
255
  return *get();
256
}
257
258
template <typename T, bool kIsWeak>
259
4897861
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
260
4897861
  return get();
261
}
262
263
template <typename T, bool kIsWeak>
264
1275852
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
265
1275852
  return get() != nullptr;
266
}
267
268
template <typename T, bool kIsWeak>
269
template <typename U, bool kW>
270
11150
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
271
    const BaseObjectPtrImpl<U, kW>& other) const {
272
11150
  return get() == other.get();
273
}
274
275
template <typename T, bool kIsWeak>
276
template <typename U, bool kW>
277
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
278
    const BaseObjectPtrImpl<U, kW>& other) const {
279
  return get() != other.get();
280
}
281
282
template <typename T, typename... Args>
283
42223
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
284
42223
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
285
}
286
287
template <typename T, typename... Args>
288
27464
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
289
27464
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
290
27464
  target->Detach();
291
27464
  return target;
292
}
293
294
}  // namespace node
295
296
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
297
298
#endif  // SRC_BASE_OBJECT_INL_H_