GeezerVison is showing github.com — worker: allow passing JS wrapper objects via postMessage · subson/node@9129cf2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,7 @@ process._exiting = false; | 58 | 58 |
| 59 | 59 | // process.config is serialized config.gypi | 60 | 60 | process.config = JSONParse(internalBinding('native_module').config); | | 61 | +require('internal/worker/js_transferable').setup(); | 61 | 62 |
| 62 | 63 | // Bootstrappers for all threads, including worker threads and main thread | 63 | 64 | const perThreadSetup = require('internal/process/per_thread'); | |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const { | | 3 | + messaging_deserialize_symbol, | | 4 | + messaging_transfer_symbol, | | 5 | + messaging_clone_symbol, | | 6 | + messaging_transfer_list_symbol | | 7 | +} = internalBinding('symbols'); | | 8 | +const { | | 9 | + JSTransferable, | | 10 | + setDeserializerCreateObjectFunction | | 11 | +} = internalBinding('messaging'); | | 12 | + | | 13 | +function setup() { | | 14 | + // Register the handler that will be used when deserializing JS-based objects | | 15 | + // from .postMessage() calls. The format of `deserializeInfo` is generally | | 16 | + // 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'. | | 17 | + setDeserializerCreateObjectFunction((deserializeInfo) => { | | 18 | + const [ module, ctor ] = deserializeInfo.split(':'); | | 19 | + const Ctor = require(module)[ctor]; | | 20 | + return new Ctor(); | | 21 | + }); | | 22 | +} | | 23 | + | | 24 | +module.exports = { | | 25 | + setup, | | 26 | + JSTransferable, | | 27 | + kClone: messaging_clone_symbol, | | 28 | + kDeserialize: messaging_deserialize_symbol, | | 29 | + kTransfer: messaging_transfer_symbol, | | 30 | + kTransferList: messaging_transfer_list_symbol | | 31 | +}; |
Collapse file Copy file name to clipboardExpand all lines: node.gyp +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 220 | 220 | 'lib/internal/vm/module.js', | 221 | 221 | 'lib/internal/worker.js', | 222 | 222 | 'lib/internal/worker/io.js', | | 223 | + 'lib/internal/worker/js_transferable.js', | 223 | 224 | 'lib/internal/watchdog.js', | 224 | 225 | 'lib/internal/streams/lazy_transform.js', | 225 | 226 | 'lib/internal/streams/async_iterator.js', | |
Collapse file Copy file name to clipboardExpand all lines: src/base_object.h +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,12 +123,17 @@ class BaseObject : public MemoryRetainer { | 123 | 123 | // make sure that they are not accidentally destroyed on the sending side. | 124 | 124 | // TransferForMessaging() will be called to get a representation of the | 125 | 125 | // object that is used for subsequent deserialization. | | 126 | + // The NestedTransferables() method can be used to transfer other objects | | 127 | + // along with this one, if a situation requires it. | 126 | 128 | // - kCloneable: | 127 | 129 | // This object can be cloned without being modified. | 128 | 130 | // CloneForMessaging() will be called to get a representation of the | 129 | 131 | // object that is used for subsequent deserialization, unless the | 130 | 132 | // object is listed in transferList, in which case TransferForMessaging() | 131 | 133 | // is attempted first. | | 134 | + // After a successful clone, FinalizeTransferRead() is called on the receiving | | 135 | + // end, and can read deserialize JS data possibly serialized by a previous | | 136 | + // FinalizeTransferWrite() call. | 132 | 137 | enum class TransferMode { | 133 | 138 | kUntransferable, | 134 | 139 | kTransferable, | @@ -137,6 +142,10 @@ class BaseObject : public MemoryRetainer { | 137 | 142 | virtual TransferMode GetTransferMode() const; | 138 | 143 | virtual std::unique_ptr<worker::TransferData> TransferForMessaging(); | 139 | 144 | virtual std::unique_ptr<worker::TransferData> CloneForMessaging() const; | | 145 | + virtual v8::Maybe<std::vector<BaseObjectPtrImpl<BaseObject, false>>> | | 146 | + NestedTransferables() const; | | 147 | + virtual v8::Maybe<bool> FinalizeTransferRead( | | 148 | + v8::Local<v8::Context> context, v8::ValueDeserializer* deserializer); | 140 | 149 |
| 141 | 150 | virtual inline void OnGCCollect(); | 142 | 151 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -159,6 +159,10 @@ constexpr size_t kFsStatsBufferLength = | 159 | 159 | V(async_id_symbol, "async_id_symbol") \ | 160 | 160 | V(handle_onclose_symbol, "handle_onclose") \ | 161 | 161 | V(no_message_symbol, "no_message_symbol") \ | | 162 | + V(messaging_deserialize_symbol, "messaging_deserialize_symbol") \ | | 163 | + V(messaging_transfer_symbol, "messaging_transfer_symbol") \ | | 164 | + V(messaging_clone_symbol, "messaging_clone_symbol") \ | | 165 | + V(messaging_transfer_list_symbol, "messaging_transfer_list_symbol") \ | 162 | 166 | V(oninit_symbol, "oninit") \ | 163 | 167 | V(owner_symbol, "owner_symbol") \ | 164 | 168 | V(onpskexchange_symbol, "onpskexchange") \ | @@ -201,6 +205,7 @@ constexpr size_t kFsStatsBufferLength = | 201 | 205 | V(crypto_rsa_pss_string, "rsa-pss") \ | 202 | 206 | V(cwd_string, "cwd") \ | 203 | 207 | V(data_string, "data") \ | | 208 | + V(deserialize_info_string, "deserializeInfo") \ | 204 | 209 | V(dest_string, "dest") \ | 205 | 210 | V(destroyed_string, "destroyed") \ | 206 | 211 | V(detached_string, "detached") \ | @@ -453,6 +458,7 @@ constexpr size_t kFsStatsBufferLength = | 453 | 458 | V(internal_binding_loader, v8::Function) \ | 454 | 459 | V(immediate_callback_function, v8::Function) \ | 455 | 460 | V(inspector_console_extension_installer, v8::Function) \ | | 461 | + V(messaging_deserialize_create_object, v8::Function) \ | 456 | 462 | V(message_port, v8::Object) \ | 457 | 463 | V(native_module_require, v8::Function) \ | 458 | 464 | V(performance_entry_callback, v8::Function) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,7 +93,8 @@ void OnFatalError(const char* location, const char* message); | 93 | 93 | V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ | 94 | 94 | V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \ | 95 | 95 | V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \ | 96 | | - "MessagePort was found in message but not listed in transferList") \ | | 96 | + "Object that needs transfer was found in message but not listed " \ | | 97 | + "in transferList") \ | 97 | 98 | V(ERR_MISSING_PLATFORM_FOR_WORKER, \ | 98 | 99 | "The V8 platform used by this instance of Node does not support " \ | 99 | 100 | "creating Workers") \ | |
|