GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base_object-inl.h Lines: 101 108 93.5 %
Date: 2022-07-31 04:16:25 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
189222
void BaseObject::Detach() {
36
189222
  CHECK_GT(pointer_data()->strong_ptr_count, 0);
37
189222
  pointer_data()->is_detached = true;
38
189222
}
39
40
471321
v8::Global<v8::Object>& BaseObject::persistent() {
41
471321
  return persistent_handle_;
42
}
43
44
45
2508714
v8::Local<v8::Object> BaseObject::object() const {
46
2508714
  return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
47
}
48
49
117681
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
50
117681
  v8::Local<v8::Object> handle = object();
51
52
  DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
53
            isolate);
54
  DCHECK_EQ(env()->isolate(), isolate);
55
56
117681
  return handle;
57
}
58
59
17036044
Environment* BaseObject::env() const {
60
17036044
  return env_;
61
}
62
63
10145333
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
64
10145333
  v8::Local<v8::Object> obj = value.As<v8::Object>();
65
  DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
66
  return static_cast<BaseObject*>(
67
20290666
      obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
68
}
69
70
71
template <typename T>
72
718730
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
73
718730
  return static_cast<T*>(FromJSObject(object));
74
}
75
76
1257492
void BaseObject::OnGCCollect() {
77
1257492
  delete this;
78
1257492
}
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
101960
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
101960
  CHECK(((*value)->*typecheck)());
108
101960
  info.This()->SetInternalField(Field, value);
109
101960
}
110
111
10788993
bool BaseObject::has_pointer_data() const {
112
10788993
  return pointer_data_ != nullptr;
113
}
114
115
template <typename T, bool kIsWeak>
116
BaseObject::PointerData*
117
3925562
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
118
  if (kIsWeak) {
119
2419726
    return data_.pointer_data;
120
  }
121
1505836
  if (get_base_object() == nullptr) {
122
    return nullptr;
123
  }
124
1505836
  return get_base_object()->pointer_data();
125
}
126
127
template <typename T, bool kIsWeak>
128
17859659
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
129
  if (kIsWeak) {
130
1084054
    if (pointer_data() == nullptr) {
131
8
      return nullptr;
132
    }
133
1084046
    return pointer_data()->self;
134
  }
135
16775605
  return data_.target;
136
}
137
138
template <typename T, bool kIsWeak>
139
3589029
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
140
3589029
  if (kIsWeak) {
141
56666
    if (pointer_data() != nullptr &&
142

81622
        --pointer_data()->weak_ptr_count == 0 &&
143
24956
        pointer_data()->self == nullptr) {
144
36
      delete pointer_data();
145
    }
146
3532363
  } else if (get() != nullptr) {
147
1354717
    get()->decrease_refcount();
148
  }
149
3589029
}
150
151
template <typename T, bool kIsWeak>
152
2943047
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
153
2943047
  data_.target = nullptr;
154
2943047
}
155
156
template <typename T, bool kIsWeak>
157
2257095
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
158
2257095
  : BaseObjectPtrImpl() {
159
2257095
  if (target == nullptr) return;
160
  if (kIsWeak) {
161
56656
    data_.pointer_data = target->pointer_data();
162
56656
    CHECK_NOT_NULL(pointer_data());
163
56656
    pointer_data()->weak_ptr_count++;
164
  } else {
165
1505836
    data_.target = target;
166
1505836
    CHECK_NOT_NULL(pointer_data());
167
1505836
    get()->increase_refcount();
168
  }
169
}
170
171
template <typename T, bool kIsWeak>
172
template <typename U, bool kW>
173
45763
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
174
    const BaseObjectPtrImpl<U, kW>& other)
175
45763
  : BaseObjectPtrImpl(other.get()) {}
176
177
template <typename T, bool kIsWeak>
178
887374
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
179
887374
  : 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
924357
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
200
924357
  : data_(other.data_) {
201
924357
  if (kIsWeak)
202
    other.data_.target = nullptr;
203
  else
204
    other.data_.pointer_data = nullptr;
205
924357
}
206
207
template <typename T, bool kIsWeak>
208
447732
BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
209
    BaseObjectPtrImpl&& other) {
210
447732
  if (&other == this) return *this;
211
447732
  this->~BaseObjectPtrImpl();
212
447732
  return *new (this) BaseObjectPtrImpl(std::move(other));
213
}
214
215
template <typename T, bool kIsWeak>
216
273705
void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
217
273705
  *this = BaseObjectPtrImpl(ptr);
218
273705
}
219
220
template <typename T, bool kIsWeak>
221
15043333
T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
222
15043333
  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
4309736
T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
232
4309736
  return get();
233
}
234
235
template <typename T, bool kIsWeak>
236
1362190
BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
237
1362190
  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
43979
BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
256
43979
  return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
257
}
258
259
template <typename T, typename... Args>
260
29026
BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
261
29026
  BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
262
29026
  target->Detach();
263
29026
  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_