worker: add `markAsUncloneable` api · nodejs/node@5fda4a1 · GitHub
Skip to content

Commit 5fda4a1

Browse files
jazellytargos
authored andcommitted
worker: add markAsUncloneable api
External modules need a way to decorate their objects so that node can recognize it as a host object for serialization process. Exposing a way for turning off instead of turning on is much safer. PR-URL: #55234 Refs: #55178 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
1 parent b36f8c2 commit 5fda4a1

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

doc/api/worker_threads.md

Lines changed: 32 additions & 0 deletions

lib/internal/worker/io.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ const {
2929
oninit: onInitSymbol,
3030
no_message_symbol: noMessageSymbol,
3131
} = internalBinding('symbols');
32+
const {
33+
privateSymbols: {
34+
transfer_mode_private_symbol,
35+
},
36+
constants: {
37+
kCloneable,
38+
},
39+
} = internalBinding('util');
3240
const {
3341
MessagePort,
3442
MessageChannel,
@@ -447,13 +455,21 @@ ObjectDefineProperties(BroadcastChannel.prototype, {
447455
defineEventHandler(BroadcastChannel.prototype, 'message');
448456
defineEventHandler(BroadcastChannel.prototype, 'messageerror');
449457

458+
function markAsUncloneable(obj) {
459+
if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null) {
460+
return;
461+
}
462+
obj[transfer_mode_private_symbol] &= ~kCloneable;
463+
}
464+
450465
module.exports = {
451466
drainMessagePort,
452467
messageTypes,
453468
kPort,
454469
kIncrementsPortRef,
455470
kWaitingStreams,
456471
kStdioWantsMoreDataCallback,
472+
markAsUncloneable,
457473
moveMessagePortToContext,
458474
MessagePort,
459475
MessageChannel,

lib/worker_threads.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
const {
1414
MessagePort,
1515
MessageChannel,
16+
markAsUncloneable,
1617
moveMessagePortToContext,
1718
receiveMessageOnPort,
1819
BroadcastChannel,
@@ -31,6 +32,7 @@ module.exports = {
3132
isMainThread,
3233
MessagePort,
3334
MessageChannel,
35+
markAsUncloneable,
3436
markAsUntransferable,
3537
isMarkedAsUntransferable,
3638
moveMessagePortToContext,
Lines changed: 70 additions & 0 deletions

0 commit comments

Comments
 (0)