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

74124
        --pointer_data()->weak_ptr_count == 0 &&
143
23916
        pointer_data()->self == nullptr) {
144
38
      delete pointer_data();
145
    }
146
3387357
  } else if (get() != nullptr) {
147
1289611
    get()->decrease_refcount();
148
  }
149
3437565
}
150
151
template <typename T, bool kIsWeak>
152
2822650
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
153
2822650
  data_.target = nullptr;
154
2822650
}
155
156
template <typename T, bool kIsWeak>
157
2156170
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
158
2156170
  : BaseObjectPtrImpl() {
159
2156170
  if (target == nullptr) return;
160
  if (kIsWeak) {
161
50198
    data_.pointer_data = target->pointer_data();
162
50198
    CHECK_NOT_NULL(pointer_data());
163
50198
    pointer_data()->weak_ptr_count++;
164
  } else {
165
1435187
    data_.target = target;
166
1435187
    CHECK_NOT_NULL(pointer_data());
167
1435187
    get()->increase_refcount();
168
  }
169
}
170
171
template <typename T, bool kIsWeak>
172
template <typename U, bool kW>
173
42462
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
174
    const BaseObjectPtrImpl<U, kW>& other)
175
42462
  : BaseObjectPtrImpl(other.get()) {}
176
177
template <typename T, bool kIsWeak>
178
860620
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
179
860620
  : 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
885752
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
200
885752
  : data_(other.data_) {
201
885752
  if (kIsWeak)
202
    other.data_.target = nullptr;
203
  else
204
    other.data_.pointer_data = nullptr;
205
885752
}
206
207
template <typename T, bool kIsWeak>
208
426739
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
209
    BaseObjectPtrImpl&& other) {
210
426739
  if (&other == this) return *this;
211
426739
  this->~BaseObjectPtrImpl();
212
426739
  return *new (this) BaseObjectPtrImpl(std::move(other));
213
}
214
215
template <typename T, bool kIsWeak>
216
260760
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
217
260760
  *this = BaseObjectPtrImpl(ptr);
218
260760
}
219
220
template <typename T, bool kIsWeak>
221
14455284
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
222
14455284
  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
4133319
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
232
4133319
  return get();
233
}
234
235
template <typename T, bool kIsWeak>
236
1301436
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
237
1301436
  return get() != nullptr;
238
}
239
240
template <typename T, bool kIsWeak>
241
template <typename U, bool kW>
242
11139
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
243
    const BaseObjectPtrImpl<U, kW>& other) const {
244
11139
  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
39953
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
256
39953
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
257
}
258
259
template <typename T, typename... Args>
260
26394
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
261
26394
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
262
26394
  target->Detach();
263
26394
  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_