GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 101 108 93.5 %
Date: 2022-07-28 04:16:57 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
188828
void BaseObject::Detach() {
36
188828
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
37
188828
  pointer_data()->is_detached = true;
38
188828
}
39
40
472215
v8::Global<v8::Object>& BaseObject::persistent() {
41
472215
  return persistent_handle_;
42
}
43
44
45
2494434
v8::Local<v8::Object> BaseObject::object() const {
46
2494434
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
47
}
48
49
117531
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
50
117531
  v8::Local<v8::Object> handle = object();
51
52
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
53
            isolate);
54
  DCHECK_EQ(env()->isolate(), isolate);
55
56
117531
  return handle;
57
}
58
59
16971163
Environment* BaseObject::env() const {
60
16971163
  return env_;
61
}
62
63
10103203
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
64
10103203
  v8::Local<v8::Object> obj = value.As<v8::Object>();
65
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
66
  return static_cast<BaseObject*>(
67
20206406
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
68
}
69
70
71
template <typename T>
72
694488
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
73
694488
  return static_cast<T*>(FromJSObject(object));
74
}
75
76
1256222
void BaseObject::OnGCCollect() {
77
1256222
  delete this;
78
1256222
}
79
80
3510
void BaseObject::ClearWeak() {
81
3510
  if (has_pointer_data())
82
    pointer_data()->wants_weak_jsobj = false;
83
84
3510
  persistent_handle_.ClearWeak();
85
3510
}
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
101812
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
101812
  CHECK(((*value)->*typecheck)());
108
101812
  info.This()->SetInternalField(Field, value);
109
101812
}
110
111
10718024
bool BaseObject::has_pointer_data() const {
112
10718024
  return pointer_data_ != nullptr;
113
}
114
115
template <typename T, bool kIsWeak>
116
BaseObject::PointerData*
117
3912116
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
118
  if (kIsWeak) {
119
2410056
    return data_.pointer_data;
120
  }
121
1502060
  if (get_base_object() == nullptr) {
122
    return nullptr;
123
  }
124
1502060
  return get_base_object()->pointer_data();
125
}
126
127
template <typename T, bool kIsWeak>
128
17818914
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
129
  if (kIsWeak) {
130
1079906
    if (pointer_data() == nullptr) {
131
8
      return nullptr;
132
    }
133
1079898
    return pointer_data()->self;
134
  }
135
16739008
  return data_.target;
136
}
137
138
template <typename T, bool kIsWeak>
139
3584544
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
140
3584544
  if (kIsWeak) {
141
56320
    if (pointer_data() != nullptr &&
142

81284
        --pointer_data()->weak_ptr_count == 0 &&
143
24964
        pointer_data()->self == nullptr) {
144
38
      delete pointer_data();
145
    }
146
3528224
  } else if (get() != nullptr) {
147
1351158
    get()->decrease_refcount();
148
  }
149
3584544
}
150
151
template <typename T, bool kIsWeak>
152
2941843
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
153
2941843
  data_.target = nullptr;
154
2941843
}
155
156
template <typename T, bool kIsWeak>
157
2251323
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
158
2251323
  : BaseObjectPtrImpl() {
159
2251323
  if (target == nullptr) return;
160
  if (kIsWeak) {
161
56310
    data_.pointer_data = target->pointer_data();
162
56310
    CHECK_NOT_NULL(pointer_data());
163
56310
    pointer_data()->weak_ptr_count++;
164
  } else {
165
1502060
    data_.target = target;
166
1502060
    CHECK_NOT_NULL(pointer_data());
167
1502060
    get()->increase_refcount();
168
  }
169
}
170
171
template <typename T, bool kIsWeak>
172
template <typename U, bool kW>
173
45754
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
174
    const BaseObjectPtrImpl<U, kW>& other)
175
45754
  : BaseObjectPtrImpl(other.get()) {}
176
177
template <typename T, bool kIsWeak>
178
885177
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
179
885177
  : 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
920980
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
200
920980
  : data_(other.data_) {
201
920980
  if (kIsWeak)
202
    other.data_.target = nullptr;
203
  else
204
    other.data_.pointer_data = nullptr;
205
920980
}
206
207
template <typename T, bool kIsWeak>
208
446159
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
209
    BaseObjectPtrImpl&& other) {
210
446159
  if (&other == this) return *this;
211
446159
  this->~BaseObjectPtrImpl();
212
446159
  return *new (this) BaseObjectPtrImpl(std::move(other));
213
}
214
215
template <typename T, bool kIsWeak>
216
272600
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
217
272600
  *this = BaseObjectPtrImpl(ptr);
218
272600
}
219
220
template <typename T, bool kIsWeak>
221
15010118
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
222
15010118
  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
4298821
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
232
4298821
  return get();
233
}
234
235
template <typename T, bool kIsWeak>
236
1357400
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
237
1357400
  return get() != nullptr;
238
}
239
240
template <typename T, bool kIsWeak>
241
template <typename U, bool kW>
242
11667
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
243
    const BaseObjectPtrImpl<U, kW>& other) const {
244
11667
  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
43972
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
256
43972
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
257
}
258
259
template <typename T, typename... Args>
260
29021
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
261
29021
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
262
29021
  target->Detach();
263
29021
  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_