GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base_object.h Lines: 3 3 100.0 %
Date: 2020-05-27 22:15:15 Branches: 0 0 - %

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_H_
23
#define SRC_BASE_OBJECT_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "memory_tracker.h"
28
#include "v8.h"
29
#include <type_traits>  // std::remove_reference
30
31
namespace node {
32
33
class Environment;
34
template <typename T, bool kIsWeak>
35
class BaseObjectPtrImpl;
36
37
class BaseObject : public MemoryRetainer {
38
 public:
39
  enum InternalFields { kSlot, kInternalFieldCount };
40
41
  // Associates this object with `object`. It uses the 0th internal field for
42
  // that, and in particular aborts if there is no such field.
43
  inline BaseObject(Environment* env, v8::Local<v8::Object> object);
44
  inline ~BaseObject() override;
45
46
  BaseObject() = delete;
47
48
  // Returns the wrapped object.  Returns an empty handle when
49
  // persistent.IsEmpty() is true.
50
  inline v8::Local<v8::Object> object() const;
51
52
  // Same as the above, except it additionally verifies that this object
53
  // is associated with the passed Isolate in debug mode.
54
  inline v8::Local<v8::Object> object(v8::Isolate* isolate) const;
55
56
  inline v8::Global<v8::Object>& persistent();
57
58
  inline Environment* env() const;
59
60
  // Get a BaseObject* pointer, or subclass pointer, for the JS object that
61
  // was also passed to the `BaseObject()` constructor initially.
62
  // This may return `nullptr` if the C++ object has not been constructed yet,
63
  // e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
64
  static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);
65
  template <typename T>
66
  static inline T* FromJSObject(v8::Local<v8::Value> object);
67
68
  // Make the `v8::Global` a weak reference and, `delete` this object once
69
  // the JS object has been garbage collected and there are no (strong)
70
  // BaseObjectPtr references to it.
71
  inline void MakeWeak();
72
73
  // Undo `MakeWeak()`, i.e. turn this into a strong reference that is a GC
74
  // root and will not be touched by the garbage collector.
75
  inline void ClearWeak();
76
77
  // Utility to create a FunctionTemplate with one internal field (used for
78
  // the `BaseObject*` pointer) and a constructor that initializes that field
79
  // to `nullptr`.
80
  static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
81
      Environment* env);
82
83
  // Setter/Getter pair for internal fields that can be passed to SetAccessor.
84
  template <int Field>
85
  static void InternalFieldGet(v8::Local<v8::String> property,
86
                               const v8::PropertyCallbackInfo<v8::Value>& info);
87
  template <int Field, bool (v8::Value::* typecheck)() const>
88
  static void InternalFieldSet(v8::Local<v8::String> property,
89
                               v8::Local<v8::Value> value,
90
                               const v8::PropertyCallbackInfo<void>& info);
91
92
  // This is a bit of a hack. See the override in async_wrap.cc for details.
93
  virtual bool IsDoneInitializing() const;
94
95
  // Can be used to avoid this object keepling itself alive as a GC root
96
  // indefinitely, for example when this object is owned and deleted by another
97
  // BaseObject once that is torn down. This can only be called when there is
98
  // a BaseObjectPtr to this object.
99
  inline void Detach();
100
101
 protected:
102
  virtual inline void OnGCCollect();
103
104
 private:
105
  v8::Local<v8::Object> WrappedObject() const override;
106
  static void DeleteMe(void* data);
107
108
  // persistent_handle_ needs to be at a fixed offset from the start of the
109
  // class because it is used by src/node_postmortem_metadata.cc to calculate
110
  // offsets and generate debug symbols for BaseObject, which assumes that the
111
  // position of members in memory are predictable. For more information please
112
  // refer to `doc/guides/node-postmortem-support.md`
113
  friend int GenDebugSymbols();
114
  friend class CleanupHookCallback;
115
  template <typename T, bool kIsWeak>
116
  friend class BaseObjectPtrImpl;
117
118
  v8::Global<v8::Object> persistent_handle_;
119
120
  // Metadata that is associated with this BaseObject if there are BaseObjectPtr
121
  // or BaseObjectWeakPtr references to it.
122
  // This object is deleted when the BaseObject itself is destroyed, and there
123
  // are no weak references to it.
124
147368
  struct PointerData {
125
    // Number of BaseObjectPtr instances that refer to this object. If this
126
    // is non-zero, the BaseObject is always a GC root and will not be destroyed
127
    // during cleanup until the count drops to zero again.
128
    unsigned int strong_ptr_count = 0;
129
    // Number of BaseObjectWeakPtr instances that refer to this object.
130
    unsigned int weak_ptr_count = 0;
131
    // Indicates whether MakeWeak() has been called.
132
    bool wants_weak_jsobj = false;
133
    // Indicates whether Detach() has been called. If that is the case, this
134
    // object will be destryoed once the strong pointer count drops to zero.
135
    bool is_detached = false;
136
    // Reference to the original BaseObject. This is used by weak pointers.
137
    BaseObject* self = nullptr;
138
  };
139
140
  inline bool has_pointer_data() const;
141
  // This creates a PointerData struct if none was associated with this
142
  // BaseObject before.
143
  inline PointerData* pointer_data();
144
145
  // Functions that adjust the strong pointer count.
146
  inline void decrease_refcount();
147
  inline void increase_refcount();
148
149
  Environment* env_;
150
  PointerData* pointer_data_ = nullptr;
151
};
152
153
// Global alias for FromJSObject() to avoid churn.
154
template <typename T>
155
134279
inline T* Unwrap(v8::Local<v8::Value> obj) {
156
134279
  return BaseObject::FromJSObject<T>(obj);
157
}
158
159
160
#define ASSIGN_OR_RETURN_UNWRAP(ptr, obj, ...)                                \
161
  do {                                                                        \
162
    *ptr = static_cast<typename std::remove_reference<decltype(*ptr)>::type>( \
163
        BaseObject::FromJSObject(obj));                                       \
164
    if (*ptr == nullptr)                                                      \
165
      return __VA_ARGS__;                                                     \
166
  } while (0)
167
168
// Implementation of a generic strong or weak pointer to a BaseObject.
169
// If strong, this will keep the target BaseObject alive regardless of other
170
// circumstances such as the GC or Environment cleanup.
171
// If weak, destruction behaviour is not affected, but the pointer will be
172
// reset to nullptr once the BaseObject is destroyed.
173
// The API matches std::shared_ptr closely.
174
template <typename T, bool kIsWeak>
175
class BaseObjectPtrImpl final {
176
 public:
177
  inline BaseObjectPtrImpl();
178
  inline ~BaseObjectPtrImpl();
179
  inline explicit BaseObjectPtrImpl(T* target);
180
181
  // Copy and move constructors. Note that the templated version is not a copy
182
  // or move constructor in the C++ sense of the word, so an identical
183
  // untemplated version is provided.
184
  template <typename U, bool kW>
185
  inline BaseObjectPtrImpl(const BaseObjectPtrImpl<U, kW>& other);
186
  inline BaseObjectPtrImpl(const BaseObjectPtrImpl& other);
187
  template <typename U, bool kW>
188
  inline BaseObjectPtrImpl& operator=(const BaseObjectPtrImpl<U, kW>& other);
189
  inline BaseObjectPtrImpl& operator=(const BaseObjectPtrImpl& other);
190
  inline BaseObjectPtrImpl(BaseObjectPtrImpl&& other);
191
  inline BaseObjectPtrImpl& operator=(BaseObjectPtrImpl&& other);
192
193
  inline void reset(T* ptr = nullptr);
194
  inline T* get() const;
195
  inline T& operator*() const;
196
  inline T* operator->() const;
197
  inline operator bool() const;
198
199
 private:
200
  union {
201
    BaseObject* target;  // Used for strong pointers.
202
    BaseObject::PointerData* pointer_data;  // Used for weak pointers.
203
  } data_;
204
205
  inline BaseObject* get_base_object() const;
206
  inline BaseObject::PointerData* pointer_data() const;
207
};
208
209
template <typename T>
210
using BaseObjectPtr = BaseObjectPtrImpl<T, false>;
211
template <typename T>
212
using BaseObjectWeakPtr = BaseObjectPtrImpl<T, true>;
213
214
// Create a BaseObject instance and return a pointer to it.
215
// This variant leaves the object as a GC root by default.
216
template <typename T, typename... Args>
217
inline BaseObjectPtr<T> MakeBaseObject(Args&&... args);
218
// Create a BaseObject instance and return a pointer to it.
219
// This variant detaches the object by default, meaning that the caller fully
220
// owns it, and once the last BaseObjectPtr to it is destroyed, the object
221
// itself is also destroyed.
222
template <typename T, typename... Args>
223
inline BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args);
224
225
}  // namespace node
226
227
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
228
229
#endif  // SRC_BASE_OBJECT_H_