GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 103 110 93.6 %
Date: 2022-09-02 04:23:42 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
// static
36
54441
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
37
    Environment* env) {
38
54441
  return BaseObject::GetConstructorTemplate(env->isolate_data());
39
}
40
41
178523
void BaseObject::Detach() {
42
178523
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
43
178523
  pointer_data()->is_detached = true;
44
178523
}
45
46
460931
v8::Global<v8::Object>& BaseObject::persistent() {
47
460931
  return persistent_handle_;
48
}
49
50
51
2484563
v8::Local<v8::Object> BaseObject::object() const {
52
2484563
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
53
}
54
55
138145
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
56
138145
  v8::Local<v8::Object> handle = object();
57
58
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
59
            isolate);
60
  DCHECK_EQ(env()->isolate(), isolate);
61
62
138145
  return handle;
63
}
64
65
16973497
Environment* BaseObject::env() const {
66
16973497
  return env_;
67
}
68
69
9886361
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
70
9886361
  v8::Local<v8::Object> obj = value.As<v8::Object>();
71
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
72
  return static_cast<BaseObject*>(
73
19772722
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
74
}
75
76
77
template <typename T>
78
765199
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
79
765199
  return static_cast<T*>(FromJSObject(object));
80
}
81
82
1249427
void BaseObject::OnGCCollect() {
83
1249427
  delete this;
84
1249427
}
85
86
2983
void BaseObject::ClearWeak() {
87
2983
  if (has_pointer_data())
88
    pointer_data()->wants_weak_jsobj = false;
89
90
2983
  persistent_handle_.ClearWeak();
91
2983
}
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
98593
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
98593
  CHECK(((*value)->*typecheck)());
114
98593
  info.This()->SetInternalField(Field, value);
115
98593
}
116
117
10430347
bool BaseObject::has_pointer_data() const {
118
10430347
  return pointer_data_ != nullptr;
119
}
120
121
template <typename T, bool kIsWeak>
122
BaseObject::PointerData*
123
3738820
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
124
  if constexpr (kIsWeak) {
125
2307264
    return data_.pointer_data;
126
  }
127
1431556
  if (get_base_object() == nullptr) {
128
    return nullptr;
129
  }
130
1431556
  return get_base_object()->pointer_data();
131
}
132
133
template <typename T, bool kIsWeak>
134
17070483
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
135
  if constexpr (kIsWeak) {
136
1041206
    if (pointer_data() == nullptr) {
137
8
      return nullptr;
138
    }
139
1041198
    return pointer_data()->self;
140
  }
141
16029277
  return data_.target;
142
}
143
144
template <typename T, bool kIsWeak>
145
3425072
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
146
  if constexpr (kIsWeak) {
147
50224
    if (pointer_data() != nullptr &&
148

74148
        --pointer_data()->weak_ptr_count == 0 &&
149
23924
        pointer_data()->self == nullptr) {
150
38
      delete pointer_data();
151
    }
152
3374848
  } else if (get() != nullptr) {
153
1285647
    get()->decrease_refcount();
154
  }
155
3425072
}
156
157
template <typename T, bool kIsWeak>
158
2813839
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
159
2813839
  data_.target = nullptr;
160
2813839
}
161
162
template <typename T, bool kIsWeak>
163
2150585
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
164
2150585
  : BaseObjectPtrImpl() {
165
2150585
  if (target == nullptr) return;
166
  if constexpr (kIsWeak) {
167
50230
    data_.pointer_data = target->pointer_data();
168
50230
    CHECK_NOT_NULL(pointer_data());
169
50230
    pointer_data()->weak_ptr_count++;
170
  } else {
171
1431556
    data_.target = target;
172
1431556
    CHECK_NOT_NULL(pointer_data());
173
1431556
    get()->increase_refcount();
174
  }
175
}
176
177
template <typename T, bool kIsWeak>
178
template <typename U, bool kW>
179
42627
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
180
    const BaseObjectPtrImpl<U, kW>& other)
181
42627
  : BaseObjectPtrImpl(other.get()) {}
182
183
template <typename T, bool kIsWeak>
184
856311
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
185
856311
  : 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
882967
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
206
882967
  : data_(other.data_) {
207
  if constexpr (kIsWeak)
208
4
    other.data_.target = nullptr;
209
  else
210
882963
    other.data_.pointer_data = nullptr;
211
882967
}
212
213
template <typename T, bool kIsWeak>
214
426418
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
215
    BaseObjectPtrImpl&& other) {
216
426418
  if (&other == this) return *this;
217
426418
  this->~BaseObjectPtrImpl();
218
426418
  return *new (this) BaseObjectPtrImpl(std::move(other));
219
}
220
221
template <typename T, bool kIsWeak>
222
260140
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
223
260140
  *this = BaseObjectPtrImpl(ptr);
224
260140
}
225
226
template <typename T, bool kIsWeak>
227
14393045
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
228
14393045
  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
4118072
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
238
4118072
  return get();
239
}
240
241
template <typename T, bool kIsWeak>
242
1236325
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
243
1236325
  return get() != nullptr;
244
}
245
246
template <typename T, bool kIsWeak>
247
template <typename U, bool kW>
248
11139
bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
249
    const BaseObjectPtrImpl<U, kW>& other) const {
250
11139
  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
40171
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
262
40171
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
263
}
264
265
template <typename T, typename... Args>
266
26537
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
267
26537
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
268
26537
  target->Detach();
269
26537
  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_