GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 123 124 99.2 %
Date: 2022-11-20 04:28:36 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
1466830
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
36
1466830
    : BaseObject(env->principal_realm(), object) {}
37
38
// static
39
34168
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
40
    Environment* env) {
41
34168
  return BaseObject::GetConstructorTemplate(env->isolate_data());
42
}
43
44
261022
void BaseObject::Detach() {
45
261022
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
46
261022
  pointer_data()->is_detached = true;
47
261022
}
48
49
391242
v8::Global<v8::Object>& BaseObject::persistent() {
50
391242
  return persistent_handle_;
51
}
52
53
54
2531648
v8::Local<v8::Object> BaseObject::object() const {
55
2531648
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
56
}
57
58
140640
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
59
140640
  v8::Local<v8::Object> handle = object();
60
61
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
62
            isolate);
63
  DCHECK_EQ(env()->isolate(), isolate);
64
65
140640
  return handle;
66
}
67
68
16092215
Environment* BaseObject::env() const {
69
16092215
  return realm_->env();
70
}
71
72
3277979
Realm* BaseObject::realm() const {
73
3277979
  return realm_;
74
}
75
76
1490621
void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
77
  DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
78
1490621
  object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
79
                                           &kNodeEmbedderId);
80
1490621
}
81
82
1490621
void BaseObject::SetInternalFields(v8::Local<v8::Object> object, void* slot) {
83
1490621
  TagNodeObject(object);
84
1490621
  object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot);
85
1490621
}
86
87
9729421
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
88
9729421
  v8::Local<v8::Object> obj = value.As<v8::Object>();
89
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kInternalFieldCount);
90
  return static_cast<BaseObject*>(
91
19458842
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
92
}
93
94
template <typename T>
95
379039
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
96
379039
  return static_cast<T*>(FromJSObject(object));
97
}
98
99
1272397
void BaseObject::OnGCCollect() {
100
1272397
  delete this;
101
1272397
}
102
103
81871
void BaseObject::ClearWeak() {
104
81871
  if (has_pointer_data())
105
8183
    pointer_data()->wants_weak_jsobj = false;
106
107
81871
  persistent_handle_.ClearWeak();
108
81871
}
109
110
976
bool BaseObject::IsWeakOrDetached() const {
111
1952
  if (persistent_handle_.IsWeak()) return true;
112
113
699
  if (!has_pointer_data()) return false;
114
168
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
115

168
  return pd->wants_weak_jsobj || pd->is_detached;
116
}
117
118
976
v8::EmbedderGraph::Node::Detachedness BaseObject::GetDetachedness() const {
119
976
  return IsWeakOrDetached() ? v8::EmbedderGraph::Node::Detachedness::kDetached
120
976
                            : 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
100176
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
100176
  CHECK(((*value)->*typecheck)());
136
100176
  info.This()->SetInternalField(Field, value);
137
100176
}
138
139
11787282
bool BaseObject::has_pointer_data() const {
140
11787282
  return pointer_data_ != nullptr;
141
}
142
143
template <typename T, bool kIsWeak>
144
BaseObject::PointerData*
145
3945801
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
146
  if constexpr (kIsWeak) {
147
2305210
    return data_.pointer_data;
148
  }
149
1640591
  if (get_base_object() == nullptr) {
150
    return nullptr;
151
  }
152
1640591
  return get_base_object()->pointer_data();
153
}
154
155
template <typename T, bool kIsWeak>
156
18758010
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
157
  if constexpr (kIsWeak) {
158
1040178
    if (pointer_data() == nullptr) {
159
8
      return nullptr;
160
    }
161
1040170
    return pointer_data()->self;
162
  }
163
17717832
  return data_.target;
164
}
165
166
template <typename T, bool kIsWeak>
167
3772486
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
168
  if constexpr (kIsWeak) {
169
50224
    if (pointer_data() != nullptr &&
170

74146
        --pointer_data()->weak_ptr_count == 0 &&
171
23922
        pointer_data()->self == nullptr) {
172
42
      delete pointer_data();
173
    }
174
3722262
  } else if (get() != nullptr) {
175
1555882
    get()->decrease_refcount();
176
  }
177
3772486
}
178
179
template <typename T, bool kIsWeak>
180
2983303
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
181
2983303
  data_.target = nullptr;
182
2983303
}
183
184
template <typename T, bool kIsWeak>
185
2358063
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
186
2358063
  : BaseObjectPtrImpl() {
187
2358063
  if (target == nullptr) return;
188
  if constexpr (kIsWeak) {
189
50230
    data_.pointer_data = target->pointer_data();
190
50230
    CHECK_NOT_NULL(pointer_data());
191
50230
    pointer_data()->weak_ptr_count++;
192
  } else {
193
1640591
    data_.target = target;
194
1640591
    CHECK_NOT_NULL(pointer_data());
195
1640591
    get()->increase_refcount();
196
  }
197
}
198
199
template <typename T, bool kIsWeak>
200
template <typename U, bool kW>
201
44048
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
202
    const BaseObjectPtrImpl<U, kW>& other)
203
44048
  : BaseObjectPtrImpl(other.get()) {}
204
205
template <typename T, bool kIsWeak>
206
875870
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
207
875870
  : 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
885228
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
228
885228
  : data_(other.data_) {
229
  if constexpr (kIsWeak)
230
4
    other.data_.target = nullptr;
231
  else
232
885224
    other.data_.pointer_data = nullptr;
233
885228
}
234
235
template <typename T, bool kIsWeak>
236
430513
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
237
    BaseObjectPtrImpl&& other) {
238
430513
  if (&other == this) return *this;
239
430513
  this->~BaseObjectPtrImpl();
240
430513
  return *new (this) BaseObjectPtrImpl(std::move(other));
241
}
242
243
template <typename T, bool kIsWeak>
244
261704
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
245
261704
  *this = BaseObjectPtrImpl(ptr);
246
261704
}
247
248
template <typename T, bool kIsWeak>
249
15665202
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
250
15665202
  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
4882325
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
260
4882325
  return get();
261
}
262
263
template <typename T, bool kIsWeak>
264
1271686
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
265
1271686
  return get() != nullptr;
266
}
267
268
template <typename T, bool kIsWeak>
269
template <typename U, bool kW>
270
11181
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
271
    const BaseObjectPtrImpl<U, kW>& other) const {
272
11181
  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
42674
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
284
42674
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
285
}
286
287
template <typename T, typename... Args>
288
27629
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
289
27629
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
290
27629
  target->Detach();
291
27629
  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_