11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst { 31cb0ef41Sopenharmony_ci Error, 41cb0ef41Sopenharmony_ci ObjectDefineProperties, 51cb0ef41Sopenharmony_ci ObjectGetOwnPropertyDescriptors, 61cb0ef41Sopenharmony_ci ObjectGetPrototypeOf, 71cb0ef41Sopenharmony_ci ObjectSetPrototypeOf, 81cb0ef41Sopenharmony_ci ObjectValues, 91cb0ef41Sopenharmony_ci ReflectConstruct, 101cb0ef41Sopenharmony_ci StringPrototypeSplit, 111cb0ef41Sopenharmony_ci} = primordials; 121cb0ef41Sopenharmony_ciconst { 131cb0ef41Sopenharmony_ci messaging_deserialize_symbol, 141cb0ef41Sopenharmony_ci messaging_transfer_symbol, 151cb0ef41Sopenharmony_ci messaging_clone_symbol, 161cb0ef41Sopenharmony_ci messaging_transfer_list_symbol, 171cb0ef41Sopenharmony_ci} = internalBinding('symbols'); 181cb0ef41Sopenharmony_ciconst { 191cb0ef41Sopenharmony_ci JSTransferable, 201cb0ef41Sopenharmony_ci setDeserializerCreateObjectFunction, 211cb0ef41Sopenharmony_ci} = internalBinding('messaging'); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cifunction setup() { 241cb0ef41Sopenharmony_ci // Register the handler that will be used when deserializing JS-based objects 251cb0ef41Sopenharmony_ci // from .postMessage() calls. The format of `deserializeInfo` is generally 261cb0ef41Sopenharmony_ci // 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'. 271cb0ef41Sopenharmony_ci setDeserializerCreateObjectFunction((deserializeInfo) => { 281cb0ef41Sopenharmony_ci const { 0: module, 1: ctor } = StringPrototypeSplit(deserializeInfo, ':'); 291cb0ef41Sopenharmony_ci const Ctor = require(module)[ctor]; 301cb0ef41Sopenharmony_ci if (typeof Ctor !== 'function' || 311cb0ef41Sopenharmony_ci typeof Ctor.prototype[messaging_deserialize_symbol] !== 'function') { 321cb0ef41Sopenharmony_ci // Not one of the official errors because one should not be able to get 331cb0ef41Sopenharmony_ci // here without messing with Node.js internals. 341cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 351cb0ef41Sopenharmony_ci throw new Error(`Unknown deserialize spec ${deserializeInfo}`); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci return new Ctor(); 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cifunction makeTransferable(obj) { 431cb0ef41Sopenharmony_ci // If the object is already transferable, skip all this. 441cb0ef41Sopenharmony_ci if (obj instanceof JSTransferable) return obj; 451cb0ef41Sopenharmony_ci const inst = ReflectConstruct(JSTransferable, [], obj.constructor); 461cb0ef41Sopenharmony_ci const properties = ObjectGetOwnPropertyDescriptors(obj); 471cb0ef41Sopenharmony_ci const propertiesValues = ObjectValues(properties); 481cb0ef41Sopenharmony_ci for (let i = 0; i < propertiesValues.length; i++) { 491cb0ef41Sopenharmony_ci // We want to use null-prototype objects to not rely on globally mutable 501cb0ef41Sopenharmony_ci // %Object.prototype%. 511cb0ef41Sopenharmony_ci ObjectSetPrototypeOf(propertiesValues[i], null); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci ObjectDefineProperties(inst, properties); 541cb0ef41Sopenharmony_ci ObjectSetPrototypeOf(inst, ObjectGetPrototypeOf(obj)); 551cb0ef41Sopenharmony_ci return inst; 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cimodule.exports = { 591cb0ef41Sopenharmony_ci makeTransferable, 601cb0ef41Sopenharmony_ci setup, 611cb0ef41Sopenharmony_ci JSTransferable, 621cb0ef41Sopenharmony_ci kClone: messaging_clone_symbol, 631cb0ef41Sopenharmony_ci kDeserialize: messaging_deserialize_symbol, 641cb0ef41Sopenharmony_ci kTransfer: messaging_transfer_symbol, 651cb0ef41Sopenharmony_ci kTransferList: messaging_transfer_list_symbol, 661cb0ef41Sopenharmony_ci}; 67