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