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 <type_traits> // std::remove_reference |
28 |
|
|
#include "memory_tracker.h" |
29 |
|
|
#include "v8.h" |
30 |
|
|
|
31 |
|
|
namespace node { |
32 |
|
|
|
33 |
|
|
class Environment; |
34 |
|
|
template <typename T, bool kIsWeak> |
35 |
|
|
class BaseObjectPtrImpl; |
36 |
|
|
|
37 |
|
|
namespace worker { |
38 |
|
|
class TransferData; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
class BaseObject : public MemoryRetainer { |
42 |
|
|
public: |
43 |
|
|
enum InternalFields { kSlot, kInternalFieldCount }; |
44 |
|
|
|
45 |
|
|
// Associates this object with `object`. It uses the 0th internal field for |
46 |
|
|
// that, and in particular aborts if there is no such field. |
47 |
|
|
BaseObject(Environment* env, v8::Local<v8::Object> object); |
48 |
|
|
~BaseObject() override; |
49 |
|
|
|
50 |
|
|
BaseObject() = delete; |
51 |
|
|
|
52 |
|
|
// Returns the wrapped object. Returns an empty handle when |
53 |
|
|
// persistent.IsEmpty() is true. |
54 |
|
|
inline v8::Local<v8::Object> object() const; |
55 |
|
|
|
56 |
|
|
// Same as the above, except it additionally verifies that this object |
57 |
|
|
// is associated with the passed Isolate in debug mode. |
58 |
|
|
inline v8::Local<v8::Object> object(v8::Isolate* isolate) const; |
59 |
|
|
|
60 |
|
|
inline v8::Global<v8::Object>& persistent(); |
61 |
|
|
|
62 |
|
|
inline Environment* env() const; |
63 |
|
|
|
64 |
|
|
// Get a BaseObject* pointer, or subclass pointer, for the JS object that |
65 |
|
|
// was also passed to the `BaseObject()` constructor initially. |
66 |
|
|
// This may return `nullptr` if the C++ object has not been constructed yet, |
67 |
|
|
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`. |
68 |
|
|
static void LazilyInitializedJSTemplateConstructor( |
69 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args); |
70 |
|
|
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object); |
71 |
|
|
template <typename T> |
72 |
|
|
static inline T* FromJSObject(v8::Local<v8::Value> object); |
73 |
|
|
|
74 |
|
|
// Make the `v8::Global` a weak reference and, `delete` this object once |
75 |
|
|
// the JS object has been garbage collected and there are no (strong) |
76 |
|
|
// BaseObjectPtr references to it. |
77 |
|
|
void MakeWeak(); |
78 |
|
|
|
79 |
|
|
// Undo `MakeWeak()`, i.e. turn this into a strong reference that is a GC |
80 |
|
|
// root and will not be touched by the garbage collector. |
81 |
|
|
inline void ClearWeak(); |
82 |
|
|
|
83 |
|
|
// Reports whether this BaseObject is using a weak reference or detached, |
84 |
|
|
// i.e. whether is can be deleted by GC once no strong BaseObjectPtrs refer |
85 |
|
|
// to it anymore. |
86 |
|
|
inline bool IsWeakOrDetached() const; |
87 |
|
|
|
88 |
|
|
// Utility to create a FunctionTemplate with one internal field (used for |
89 |
|
|
// the `BaseObject*` pointer) and a constructor that initializes that field |
90 |
|
|
// to `nullptr`. |
91 |
|
|
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate( |
92 |
|
|
Environment* env); |
93 |
|
|
|
94 |
|
|
// Setter/Getter pair for internal fields that can be passed to SetAccessor. |
95 |
|
|
template <int Field> |
96 |
|
|
static void InternalFieldGet(v8::Local<v8::String> property, |
97 |
|
|
const v8::PropertyCallbackInfo<v8::Value>& info); |
98 |
|
|
template <int Field, bool (v8::Value::*typecheck)() const> |
99 |
|
|
static void InternalFieldSet(v8::Local<v8::String> property, |
100 |
|
|
v8::Local<v8::Value> value, |
101 |
|
|
const v8::PropertyCallbackInfo<void>& info); |
102 |
|
|
|
103 |
|
|
// This is a bit of a hack. See the override in async_wrap.cc for details. |
104 |
|
|
virtual bool IsDoneInitializing() const; |
105 |
|
|
|
106 |
|
|
// Can be used to avoid this object keeping itself alive as a GC root |
107 |
|
|
// indefinitely, for example when this object is owned and deleted by another |
108 |
|
|
// BaseObject once that is torn down. This can only be called when there is |
109 |
|
|
// a BaseObjectPtr to this object. |
110 |
|
|
inline void Detach(); |
111 |
|
|
|
112 |
|
|
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( |
113 |
|
|
Environment* env); |
114 |
|
|
|
115 |
|
|
// Interface for transferring BaseObject instances using the .postMessage() |
116 |
|
|
// method of MessagePorts (and, by extension, Workers). |
117 |
|
|
// GetTransferMode() returns a transfer mode that indicates how to deal with |
118 |
|
|
// the current object: |
119 |
|
|
// - kUntransferable: |
120 |
|
|
// No transfer is possible, either because this type of BaseObject does |
121 |
|
|
// not know how to be transferred, or because it is not in a state in |
122 |
|
|
// which it is possible to do so (e.g. because it has already been |
123 |
|
|
// transferred). |
124 |
|
|
// - kTransferable: |
125 |
|
|
// This object can be transferred in a destructive fashion, i.e. will be |
126 |
|
|
// rendered unusable on the sending side of the channel in the process |
127 |
|
|
// of being transferred. (In C++ this would be referred to as movable but |
128 |
|
|
// not copyable.) Objects of this type need to be listed in the |
129 |
|
|
// `transferList` argument of the relevant postMessage() call in order to |
130 |
|
|
// make sure that they are not accidentally destroyed on the sending side. |
131 |
|
|
// TransferForMessaging() will be called to get a representation of the |
132 |
|
|
// object that is used for subsequent deserialization. |
133 |
|
|
// The NestedTransferables() method can be used to transfer other objects |
134 |
|
|
// along with this one, if a situation requires it. |
135 |
|
|
// - kCloneable: |
136 |
|
|
// This object can be cloned without being modified. |
137 |
|
|
// CloneForMessaging() will be called to get a representation of the |
138 |
|
|
// object that is used for subsequent deserialization, unless the |
139 |
|
|
// object is listed in transferList, in which case TransferForMessaging() |
140 |
|
|
// is attempted first. |
141 |
|
|
// After a successful clone, FinalizeTransferRead() is called on the receiving |
142 |
|
|
// end, and can read deserialize JS data possibly serialized by a previous |
143 |
|
|
// FinalizeTransferWrite() call. |
144 |
|
|
enum class TransferMode { |
145 |
|
|
kUntransferable, |
146 |
|
|
kTransferable, |
147 |
|
|
kCloneable |
148 |
|
|
}; |
149 |
|
|
virtual TransferMode GetTransferMode() const; |
150 |
|
|
virtual std::unique_ptr<worker::TransferData> TransferForMessaging(); |
151 |
|
|
virtual std::unique_ptr<worker::TransferData> CloneForMessaging() const; |
152 |
|
|
virtual v8::Maybe<std::vector<BaseObjectPtrImpl<BaseObject, false>>> |
153 |
|
|
NestedTransferables() const; |
154 |
|
|
virtual v8::Maybe<bool> FinalizeTransferRead( |
155 |
|
|
v8::Local<v8::Context> context, v8::ValueDeserializer* deserializer); |
156 |
|
|
|
157 |
|
|
// Indicates whether this object is expected to use a strong reference during |
158 |
|
|
// a clean process exit (due to an empty event loop). |
159 |
|
|
virtual bool IsNotIndicativeOfMemoryLeakAtExit() const; |
160 |
|
|
|
161 |
|
|
virtual inline void OnGCCollect(); |
162 |
|
|
|
163 |
|
|
virtual inline bool is_snapshotable() const { return false; } |
164 |
|
|
|
165 |
|
|
private: |
166 |
|
|
v8::Local<v8::Object> WrappedObject() const override; |
167 |
|
|
bool IsRootNode() const override; |
168 |
|
|
static void DeleteMe(void* data); |
169 |
|
|
|
170 |
|
|
// persistent_handle_ needs to be at a fixed offset from the start of the |
171 |
|
|
// class because it is used by src/node_postmortem_metadata.cc to calculate |
172 |
|
|
// offsets and generate debug symbols for BaseObject, which assumes that the |
173 |
|
|
// position of members in memory are predictable. For more information please |
174 |
|
|
// refer to `doc/contributing/node-postmortem-support.md` |
175 |
|
|
friend int GenDebugSymbols(); |
176 |
|
|
friend class CleanupHookCallback; |
177 |
|
|
template <typename T, bool kIsWeak> |
178 |
|
|
friend class BaseObjectPtrImpl; |
179 |
|
|
|
180 |
|
|
v8::Global<v8::Object> persistent_handle_; |
181 |
|
|
|
182 |
|
|
// Metadata that is associated with this BaseObject if there are BaseObjectPtr |
183 |
|
|
// or BaseObjectWeakPtr references to it. |
184 |
|
|
// This object is deleted when the BaseObject itself is destroyed, and there |
185 |
|
|
// are no weak references to it. |
186 |
|
|
struct PointerData { |
187 |
|
|
// Number of BaseObjectPtr instances that refer to this object. If this |
188 |
|
|
// is non-zero, the BaseObject is always a GC root and will not be destroyed |
189 |
|
|
// during cleanup until the count drops to zero again. |
190 |
|
|
unsigned int strong_ptr_count = 0; |
191 |
|
|
// Number of BaseObjectWeakPtr instances that refer to this object. |
192 |
|
|
unsigned int weak_ptr_count = 0; |
193 |
|
|
// Indicates whether MakeWeak() has been called. |
194 |
|
|
bool wants_weak_jsobj = false; |
195 |
|
|
// Indicates whether Detach() has been called. If that is the case, this |
196 |
|
|
// object will be destroyed once the strong pointer count drops to zero. |
197 |
|
|
bool is_detached = false; |
198 |
|
|
// Reference to the original BaseObject. This is used by weak pointers. |
199 |
|
|
BaseObject* self = nullptr; |
200 |
|
|
}; |
201 |
|
|
|
202 |
|
|
inline bool has_pointer_data() const; |
203 |
|
|
// This creates a PointerData struct if none was associated with this |
204 |
|
|
// BaseObject before. |
205 |
|
|
PointerData* pointer_data(); |
206 |
|
|
|
207 |
|
|
// Functions that adjust the strong pointer count. |
208 |
|
|
void decrease_refcount(); |
209 |
|
|
void increase_refcount(); |
210 |
|
|
|
211 |
|
|
Environment* env_; |
212 |
|
|
PointerData* pointer_data_ = nullptr; |
213 |
|
|
}; |
214 |
|
|
|
215 |
|
|
// Global alias for FromJSObject() to avoid churn. |
216 |
|
|
template <typename T> |
217 |
|
180847 |
inline T* Unwrap(v8::Local<v8::Value> obj) { |
218 |
|
180847 |
return BaseObject::FromJSObject<T>(obj); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
#define ASSIGN_OR_RETURN_UNWRAP(ptr, obj, ...) \ |
222 |
|
|
do { \ |
223 |
|
|
*ptr = static_cast<typename std::remove_reference<decltype(*ptr)>::type>( \ |
224 |
|
|
BaseObject::FromJSObject(obj)); \ |
225 |
|
|
if (*ptr == nullptr) return __VA_ARGS__; \ |
226 |
|
|
} while (0) |
227 |
|
|
|
228 |
|
|
// Implementation of a generic strong or weak pointer to a BaseObject. |
229 |
|
|
// If strong, this will keep the target BaseObject alive regardless of other |
230 |
|
|
// circumstances such as the GC or Environment cleanup. |
231 |
|
|
// If weak, destruction behaviour is not affected, but the pointer will be |
232 |
|
|
// reset to nullptr once the BaseObject is destroyed. |
233 |
|
|
// The API matches std::shared_ptr closely. |
234 |
|
|
template <typename T, bool kIsWeak> |
235 |
|
|
class BaseObjectPtrImpl final { |
236 |
|
|
public: |
237 |
|
|
inline BaseObjectPtrImpl(); |
238 |
|
|
inline ~BaseObjectPtrImpl(); |
239 |
|
|
inline explicit BaseObjectPtrImpl(T* target); |
240 |
|
|
|
241 |
|
|
// Copy and move constructors. Note that the templated version is not a copy |
242 |
|
|
// or move constructor in the C++ sense of the word, so an identical |
243 |
|
|
// untemplated version is provided. |
244 |
|
|
template <typename U, bool kW> |
245 |
|
|
inline BaseObjectPtrImpl(const BaseObjectPtrImpl<U, kW>& other); |
246 |
|
|
inline BaseObjectPtrImpl(const BaseObjectPtrImpl& other); |
247 |
|
|
template <typename U, bool kW> |
248 |
|
|
inline BaseObjectPtrImpl& operator=(const BaseObjectPtrImpl<U, kW>& other); |
249 |
|
|
inline BaseObjectPtrImpl& operator=(const BaseObjectPtrImpl& other); |
250 |
|
|
inline BaseObjectPtrImpl(BaseObjectPtrImpl&& other); |
251 |
|
|
inline BaseObjectPtrImpl& operator=(BaseObjectPtrImpl&& other); |
252 |
|
|
|
253 |
|
|
inline void reset(T* ptr = nullptr); |
254 |
|
|
inline T* get() const; |
255 |
|
|
inline T& operator*() const; |
256 |
|
|
inline T* operator->() const; |
257 |
|
|
inline operator bool() const; |
258 |
|
|
|
259 |
|
|
template <typename U, bool kW> |
260 |
|
|
inline bool operator ==(const BaseObjectPtrImpl<U, kW>& other) const; |
261 |
|
|
template <typename U, bool kW> |
262 |
|
|
inline bool operator !=(const BaseObjectPtrImpl<U, kW>& other) const; |
263 |
|
|
|
264 |
|
|
private: |
265 |
|
|
union { |
266 |
|
|
BaseObject* target; // Used for strong pointers. |
267 |
|
|
BaseObject::PointerData* pointer_data; // Used for weak pointers. |
268 |
|
|
} data_; |
269 |
|
|
|
270 |
|
|
inline BaseObject* get_base_object() const; |
271 |
|
|
inline BaseObject::PointerData* pointer_data() const; |
272 |
|
|
}; |
273 |
|
|
|
274 |
|
|
template <typename T> |
275 |
|
|
using BaseObjectPtr = BaseObjectPtrImpl<T, false>; |
276 |
|
|
template <typename T> |
277 |
|
|
using BaseObjectWeakPtr = BaseObjectPtrImpl<T, true>; |
278 |
|
|
|
279 |
|
|
// Create a BaseObject instance and return a pointer to it. |
280 |
|
|
// This variant leaves the object as a GC root by default. |
281 |
|
|
template <typename T, typename... Args> |
282 |
|
|
inline BaseObjectPtr<T> MakeBaseObject(Args&&... args); |
283 |
|
|
// Create a BaseObject instance and return a pointer to it. |
284 |
|
|
// This variant detaches the object by default, meaning that the caller fully |
285 |
|
|
// owns it, and once the last BaseObjectPtr to it is destroyed, the object |
286 |
|
|
// itself is also destroyed. |
287 |
|
|
template <typename T, typename... Args> |
288 |
|
|
inline BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args); |
289 |
|
|
|
290 |
|
|
} // namespace node |
291 |
|
|
|
292 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
293 |
|
|
|
294 |
|
|
#endif // SRC_BASE_OBJECT_H_ |