diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-04-09 19:45:15 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-06-14 15:53:38 +0300 |
commit | 57e7c63f74255e3287a11393bb4c18d0ab4e7b1d (patch) | |
tree | bb671cf3d3762180c73e16a1a171df23884f9f41 /src/base_object.h | |
parent | 8a7201b25fa2e5342b5d737742586bcd5ea1da5e (diff) |
worker: allow transferring/cloning generic BaseObjects
Extend support for transferring objects à la `MessagePort` to other
types of `BaseObject` subclasses, as well as implement cloning
support for cases in which destructive transferring is not needed
or optional.
PR-URL: https://github.com/nodejs/node/pull/33772
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'src/base_object.h')
-rw-r--r-- | src/base_object.h | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/base_object.h b/src/base_object.h index e02fd8d209f..39450540323 100644 --- a/src/base_object.h +++ b/src/base_object.h @@ -34,6 +34,10 @@ class Environment; template <typename T, bool kIsWeak> class BaseObjectPtrImpl; +namespace worker { +class TransferData; +} + class BaseObject : public MemoryRetainer { public: enum InternalFields { kSlot, kInternalFieldCount }; @@ -101,7 +105,39 @@ class BaseObject : public MemoryRetainer { static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( Environment* env); - protected: + // Interface for transferring BaseObject instances using the .postMessage() + // method of MessagePorts (and, by extension, Workers). + // GetTransferMode() returns a transfer mode that indicates how to deal with + // the current object: + // - kUntransferable: + // No transfer is possible, either because this type of BaseObject does + // not know how to be transfered, or because it is not in a state in + // which it is possible to do so (e.g. because it has already been + // transfered). + // - kTransferable: + // This object can be transfered in a destructive fashion, i.e. will be + // rendered unusable on the sending side of the channel in the process + // of being transfered. (In C++ this would be referred to as movable but + // not copyable.) Objects of this type need to be listed in the + // `transferList` argument of the relevant postMessage() call in order to + // make sure that they are not accidentally destroyed on the sending side. + // TransferForMessaging() will be called to get a representation of the + // object that is used for subsequent deserialization. + // - kCloneable: + // This object can be cloned without being modified. + // CloneForMessaging() will be called to get a representation of the + // object that is used for subsequent deserialization, unless the + // object is listed in transferList, in which case TransferForMessaging() + // is attempted first. + enum class TransferMode { + kUntransferable, + kTransferable, + kCloneable + }; + virtual TransferMode GetTransferMode() const; + virtual std::unique_ptr<worker::TransferData> TransferForMessaging(); + virtual std::unique_ptr<worker::TransferData> CloneForMessaging() const; + virtual inline void OnGCCollect(); private: |