GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 104 110 94.5 %
Date: 2022-09-25 04:23:55 Branches: 25 42 59.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
// static
36
55617
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
37
    Environment* env) {
38
55617
  return BaseObject::GetConstructorTemplate(env->isolate_data());
39
}
40
41
259045
void BaseObject::Detach() {
42
259045
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
43
259045
  pointer_data()->is_detached = true;
44
259045
}
45
46
389824
v8::Global<v8::Object>& BaseObject::persistent() {
47
389824
  return persistent_handle_;
48
}
49
50
51
2509013
v8::Local<v8::Object> BaseObject::object() const {
52
2509013
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
53
}
54
55
137154
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
56
137154
  v8::Local<v8::Object> handle = object();
57
58
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
59
            isolate);
60
  DCHECK_EQ(env()->isolate(), isolate);
61
62
137154
  return handle;
63
}
64
65
17100122
Environment* BaseObject::env() const {
66
17100122
  return env_;
67
}
68
69
10023565
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
70
10023565
  v8::Local<v8::Object> obj = value.As<v8::Object>();
71
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
72
  return static_cast<BaseObject*>(
73
20047130
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
74
}
75
76
77
template <typename T>
78
779328
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
79
779328
  return static_cast<T*>(FromJSObject(object));
80
}
81
82
1267563
void BaseObject::OnGCCollect() {
83
1267563
  delete this;
84
1267563
}
85
86
80952
void BaseObject::ClearWeak() {
87
80952
  if (has_pointer_data())
88
8169
    pointer_data()->wants_weak_jsobj = false;
89
90
80952
  persistent_handle_.ClearWeak();
91
80952
}
92
93
bool BaseObject::IsWeakOrDetached() const {
94
  if (persistent_handle_.IsWeak()) return true;
95
96
  if (!has_pointer_data()) return false;
97
  const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
98
  return pd->wants_weak_jsobj || pd->is_detached;
99
}
100
101
template <int Field>
102
8
void BaseObject::InternalFieldGet(
103
    v8::Local<v8::String> property,
104
    const v8::PropertyCallbackInfo<v8::Value>& info) {
105
8
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
106
8
}
107
108
template <int Field, bool (v8::Value::* typecheck)() const>
109
100080
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
110
                                  v8::Local<v8::Value> value,
111
                                  const v8::PropertyCallbackInfo<void>& info) {
112
  // This could be e.g. value->IsFunction().
113
100080
  CHECK(((*value)->*typecheck)());
114
100080
  info.This()->SetInternalField(Field, value);
115
100080
}
116
117
11645689
bool BaseObject::has_pointer_data() const {
118
11645689
  return pointer_data_ != nullptr;
119
}
120
121
template <typename T, bool kIsWeak>
122
BaseObject::PointerData*
123
3922880
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
124
  if constexpr (kIsWeak) {
125
2311312
    return data_.pointer_data;
126
  }
127
1611568
  if (get_base_object() == nullptr) {
128
    return nullptr;
129
  }
130
1611568
  return get_base_object()->pointer_data();
131
}
132
133
template <typename T, bool kIsWeak>
134
18580664
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
135
  if constexpr (kIsWeak) {
136
1043196
    if (pointer_data() == nullptr) {
137
8
      return nullptr;
138
    }
139
1043188
    return pointer_data()->self;
140
  }
141
17537468
  return data_.target;
142
}
143
144
template <typename T, bool kIsWeak>
145
3788864
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
146
  if constexpr (kIsWeak) {
147
50242
    if (pointer_data() != nullptr &&
148

74174
        --pointer_data()->weak_ptr_count == 0 &&
149
23932
        pointer_data()->self == nullptr) {
150
26
      delete pointer_data();
151
    }
152
3738622
  } else if (get() != nullptr) {
153
1527237
    get()->decrease_refcount();
154
  }
155
3788864
}
156
157
template <typename T, bool kIsWeak>
158
2997093
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
159
2997093
  data_.target = nullptr;
160
2997093
}
161
162
template <typename T, bool kIsWeak>
163
2333365
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
164
2333365
  : BaseObjectPtrImpl() {
165
2333365
  if (target == nullptr) return;
166
  if constexpr (kIsWeak) {
167
50248
    data_.pointer_data = target->pointer_data();
168
50248
    CHECK_NOT_NULL(pointer_data());
169
50248
    pointer_data()->weak_ptr_count++;
170
  } else {
171
1611568
    data_.target = target;
172
1611568
    CHECK_NOT_NULL(pointer_data());
173
1611568
    get()->increase_refcount();
174
  }
175
}
176
177
template <typename T, bool kIsWeak>
178
template <typename U, bool kW>
179
43514
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
180
    const BaseObjectPtrImpl<U, kW>& other)
181
43514
  : BaseObjectPtrImpl(other.get()) {}
182
183
template <typename T, bool kIsWeak>
184
860601
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
185
860601
  : BaseObjectPtrImpl(other.get()) {}
186
187
template <typename T, bool kIsWeak>
188
template <typename U, bool kW>
189
8
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
190
    const BaseObjectPtrImpl<U, kW>& other) {
191
8
  if (other.get() == get()) return *this;
192
8
  this->~BaseObjectPtrImpl();
193
8
  return *new (this) BaseObjectPtrImpl(other);
194
}
195
196
template <typename T, bool kIsWeak>
197
2
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
198
    const BaseObjectPtrImpl& other) {
199
2
  if (other.get() == get()) return *this;
200
2
  this->~BaseObjectPtrImpl();
201
2
  return *new (this) BaseObjectPtrImpl(other);
202
}
203
204
template <typename T, bool kIsWeak>
205
887256
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
206
887256
  : data_(other.data_) {
207
  if constexpr (kIsWeak)
208
4
    other.data_.target = nullptr;
209
  else
210
887252
    other.data_.pointer_data = nullptr;
211
887256
}
212
213
template <typename T, bool kIsWeak>
214
428123
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
215
    BaseObjectPtrImpl&& other) {
216
428123
  if (&other == this) return *this;
217
428123
  this->~BaseObjectPtrImpl();
218
428123
  return *new (this) BaseObjectPtrImpl(std::move(other));
219
}
220
221
template <typename T, bool kIsWeak>
222
261596
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
223
261596
  *this = BaseObjectPtrImpl(ptr);
224
261596
}
225
226
template <typename T, bool kIsWeak>
227
15544674
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
228
15544674
  return static_cast<T*>(get_base_object());
229
}
230
231
template <typename T, bool kIsWeak>
232
T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
233
  return *get();
234
}
235
236
template <typename T, bool kIsWeak>
237
4856094
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
238
4856094
  return get();
239
}
240
241
template <typename T, bool kIsWeak>
242
1248667
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
243
1248667
  return get() != nullptr;
244
}
245
246
template <typename T, bool kIsWeak>
247
template <typename U, bool kW>
248
11144
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
249
    const BaseObjectPtrImpl<U, kW>& other) const {
250
11144
  return get() == other.get();
251
}
252
253
template <typename T, bool kIsWeak>
254
template <typename U, bool kW>
255
bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
256
    const BaseObjectPtrImpl<U, kW>& other) const {
257
  return get() != other.get();
258
}
259
260
template <typename T, typename... Args>
261
41224
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
262
41224
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
263
}
264
265
template <typename T, typename... Args>
266
27240
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
267
27240
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
268
27240
  target->Detach();
269
27240
  return target;
270
}
271
272
}  // namespace node
273
274
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
275
276
#endif  // SRC_BASE_OBJECT_INL_H_