mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 17:10:40 +00:00

Co-authored-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/44048 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
'use strict';
|
|
const {
|
|
Error,
|
|
ObjectDefineProperties,
|
|
ObjectGetOwnPropertyDescriptors,
|
|
ObjectGetPrototypeOf,
|
|
ObjectSetPrototypeOf,
|
|
ObjectValues,
|
|
ReflectConstruct,
|
|
StringPrototypeSplit,
|
|
} = primordials;
|
|
const {
|
|
messaging_deserialize_symbol,
|
|
messaging_transfer_symbol,
|
|
messaging_clone_symbol,
|
|
messaging_transfer_list_symbol
|
|
} = internalBinding('symbols');
|
|
const {
|
|
JSTransferable,
|
|
setDeserializerCreateObjectFunction
|
|
} = internalBinding('messaging');
|
|
|
|
function setup() {
|
|
// Register the handler that will be used when deserializing JS-based objects
|
|
// from .postMessage() calls. The format of `deserializeInfo` is generally
|
|
// 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'.
|
|
setDeserializerCreateObjectFunction((deserializeInfo) => {
|
|
const { 0: module, 1: ctor } = StringPrototypeSplit(deserializeInfo, ':');
|
|
const Ctor = require(module)[ctor];
|
|
if (typeof Ctor !== 'function' ||
|
|
typeof Ctor.prototype[messaging_deserialize_symbol] !== 'function') {
|
|
// Not one of the official errors because one should not be able to get
|
|
// here without messing with Node.js internals.
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
throw new Error(`Unknown deserialize spec ${deserializeInfo}`);
|
|
}
|
|
|
|
return new Ctor();
|
|
});
|
|
}
|
|
|
|
function makeTransferable(obj) {
|
|
// If the object is already transferable, skip all this.
|
|
if (obj instanceof JSTransferable) return obj;
|
|
const inst = ReflectConstruct(JSTransferable, [], obj.constructor);
|
|
const properties = ObjectGetOwnPropertyDescriptors(obj);
|
|
const propertiesValues = ObjectValues(properties);
|
|
for (let i = 0; i < propertiesValues.length; i++) {
|
|
// We want to use null-prototype objects to not rely on globally mutable
|
|
// %Object.prototype%.
|
|
ObjectSetPrototypeOf(propertiesValues[i], null);
|
|
}
|
|
ObjectDefineProperties(inst, properties);
|
|
ObjectSetPrototypeOf(inst, ObjectGetPrototypeOf(obj));
|
|
return inst;
|
|
}
|
|
|
|
module.exports = {
|
|
makeTransferable,
|
|
setup,
|
|
JSTransferable,
|
|
kClone: messaging_clone_symbol,
|
|
kDeserialize: messaging_deserialize_symbol,
|
|
kTransfer: messaging_transfer_symbol,
|
|
kTransferList: messaging_transfer_list_symbol,
|
|
};
|