GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 104 110 94.5 %
Date: 2022-09-22 04:22:24 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
55638
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
37
    Environment* env) {
38
55638
  return BaseObject::GetConstructorTemplate(env->isolate_data());
39
}
40
41
258974
void BaseObject::Detach() {
42
258974
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
43
258974
  pointer_data()->is_detached = true;
44
258974
}
45
46
389610
v8::Global<v8::Object>& BaseObject::persistent() {
47
389610
  return persistent_handle_;
48
}
49
50
51
2505829
v8::Local<v8::Object> BaseObject::object() const {
52
2505829
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
53
}
54
55
138816
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
56
138816
  v8::Local<v8::Object> handle = object();
57
58
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
59
            isolate);
60
  DCHECK_EQ(env()->isolate(), isolate);
61
62
138816
  return handle;
63
}
64
65
17093890
Environment* BaseObject::env() const {
66
17093890
  return env_;
67
}
68
69
9942200
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
70
9942200
  v8::Local<v8::Object> obj = value.As<v8::Object>();
71
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
72
  return static_cast<BaseObject*>(
73
19884400
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
74
}
75
76
77
template <typename T>
78
761022
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
79
761022
  return static_cast<T*>(FromJSObject(object));
80
}
81
82
1267636
void BaseObject::OnGCCollect() {
83
1267636
  delete this;
84
1267636
}
85
86
80898
void BaseObject::ClearWeak() {
87
80898
  if (has_pointer_data())
88
8127
    pointer_data()->wants_weak_jsobj = false;
89
90
80898
  persistent_handle_.ClearWeak();
91
80898
}
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
100015
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
100015
  CHECK(((*value)->*typecheck)());
114
100015
  info.This()->SetInternalField(Field, value);
115
100015
}
116
117
11637714
bool BaseObject::has_pointer_data() const {
118
11637714
  return pointer_data_ != nullptr;
119
}
120
121
template <typename T, bool kIsWeak>
122
BaseObject::PointerData*
123
3921049
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
124
  if constexpr (kIsWeak) {
125
2310162
    return data_.pointer_data;
126
  }
127
1610887
  if (get_base_object() == nullptr) {
128
    return nullptr;
129
  }
130
1610887
  return get_base_object()->pointer_data();
131
}
132
133
template <typename T, bool kIsWeak>
134
18571494
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
135
  if constexpr (kIsWeak) {
136
1042664
    if (pointer_data() == nullptr) {
137
8
      return nullptr;
138
    }
139
1042656
    return pointer_data()->self;
140
  }
141
17528830
  return data_.target;
142
}
143
144
template <typename T, bool kIsWeak>
145
3790188
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
146
  if constexpr (kIsWeak) {
147
50222
    if (pointer_data() != nullptr &&
148

74144
        --pointer_data()->weak_ptr_count == 0 &&
149
23922
        pointer_data()->self == nullptr) {
150
30
      delete pointer_data();
151
    }
152
3739966
  } else if (get() != nullptr) {
153
1526901
    get()->decrease_refcount();
154
  }
155
3790188
}
156
157
template <typename T, bool kIsWeak>
158
2998394
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
159
2998394
  data_.target = nullptr;
160
2998394
}
161
162
template <typename T, bool kIsWeak>
163
2332721
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
164
2332721
  : BaseObjectPtrImpl() {
165
2332721
  if (target == nullptr) return;
166
  if constexpr (kIsWeak) {
167
50228
    data_.pointer_data = target->pointer_data();
168
50228
    CHECK_NOT_NULL(pointer_data());
169
50228
    pointer_data()->weak_ptr_count++;
170
  } else {
171
1610887
    data_.target = target;
172
1610887
    CHECK_NOT_NULL(pointer_data());
173
1610887
    get()->increase_refcount();
174
  }
175
}
176
177
template <typename T, bool kIsWeak>
178
template <typename U, bool kW>
179
43523
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
180
    const BaseObjectPtrImpl<U, kW>& other)
181
43523
  : BaseObjectPtrImpl(other.get()) {}
182
183
template <typename T, bool kIsWeak>
184
860626
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
185
860626
  : 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
886934
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
206
886934
  : data_(other.data_) {
207
  if constexpr (kIsWeak)
208
4
    other.data_.target = nullptr;
209
  else
210
886930
    other.data_.pointer_data = nullptr;
211
886934
}
212
213
template <typename T, bool kIsWeak>
214
427969
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
215
    BaseObjectPtrImpl&& other) {
216
427969
  if (&other == this) return *this;
217
427969
  this->~BaseObjectPtrImpl();
218
427969
  return *new (this) BaseObjectPtrImpl(std::move(other));
219
}
220
221
template <typename T, bool kIsWeak>
222
261469
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
223
261469
  *this = BaseObjectPtrImpl(ptr);
224
261469
}
225
226
template <typename T, bool kIsWeak>
227
15536880
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
228
15536880
  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
4854386
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
238
4854386
  return get();
239
}
240
241
template <typename T, bool kIsWeak>
242
1248128
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
243
1248128
  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
41235
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
262
41235
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
263
}
264
265
template <typename T, typename... Args>
266
27247
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
267
27247
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
268
27247
  target->Detach();
269
27247
  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_