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