11cb0ef41Sopenharmony_ci/* global port */ 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst vm = require('vm'); 61cb0ef41Sopenharmony_ciconst { 71cb0ef41Sopenharmony_ci MessagePort, MessageChannel, moveMessagePortToContext 81cb0ef41Sopenharmony_ci} = require('worker_threads'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst context = vm.createContext(); 111cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 121cb0ef41Sopenharmony_cicontext.port = moveMessagePortToContext(port1, context); 131cb0ef41Sopenharmony_cicontext.global = context; 141cb0ef41Sopenharmony_ciObject.assign(context, { 151cb0ef41Sopenharmony_ci global: context, 161cb0ef41Sopenharmony_ci assert, 171cb0ef41Sopenharmony_ci MessagePort, 181cb0ef41Sopenharmony_ci MessageChannel 191cb0ef41Sopenharmony_ci}); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_civm.runInContext('(' + function() { 221cb0ef41Sopenharmony_ci { 231cb0ef41Sopenharmony_ci assert(port.postMessage instanceof Function); 241cb0ef41Sopenharmony_ci assert(port.constructor instanceof Function); 251cb0ef41Sopenharmony_ci for (let obj = port; obj !== null; obj = Object.getPrototypeOf(obj)) { 261cb0ef41Sopenharmony_ci for (const key of Object.getOwnPropertyNames(obj)) { 271cb0ef41Sopenharmony_ci if (typeof obj[key] === 'object' && obj[key] !== null) { 281cb0ef41Sopenharmony_ci assert(obj[key] instanceof Object); 291cb0ef41Sopenharmony_ci } else if (typeof obj[key] === 'function') { 301cb0ef41Sopenharmony_ci assert(obj[key] instanceof Function); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci assert(!(port instanceof MessagePort)); 361cb0ef41Sopenharmony_ci assert.strictEqual(port.onmessage, undefined); 371cb0ef41Sopenharmony_ci port.onmessage = function({ data, ports }) { 381cb0ef41Sopenharmony_ci assert(data instanceof Object); 391cb0ef41Sopenharmony_ci assert(ports instanceof Array); 401cb0ef41Sopenharmony_ci assert.strictEqual(ports.length, 1); 411cb0ef41Sopenharmony_ci assert.strictEqual(ports[0], data.p); 421cb0ef41Sopenharmony_ci assert(!(data.p instanceof MessagePort)); 431cb0ef41Sopenharmony_ci port.postMessage({}); 441cb0ef41Sopenharmony_ci }; 451cb0ef41Sopenharmony_ci port.start(); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci { 491cb0ef41Sopenharmony_ci let threw = false; 501cb0ef41Sopenharmony_ci try { 511cb0ef41Sopenharmony_ci port.postMessage(global); 521cb0ef41Sopenharmony_ci } catch (e) { 531cb0ef41Sopenharmony_ci assert.strictEqual(e.constructor.name, 'DOMException'); 541cb0ef41Sopenharmony_ci assert(e instanceof Object); 551cb0ef41Sopenharmony_ci assert(e instanceof Error); 561cb0ef41Sopenharmony_ci threw = true; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci assert(threw); 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci} + ')()', context); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ciconst otherChannel = new MessageChannel(); 631cb0ef41Sopenharmony_ciport2.on('message', common.mustCall((msg) => { 641cb0ef41Sopenharmony_ci assert(msg instanceof Object); 651cb0ef41Sopenharmony_ci port2.close(); 661cb0ef41Sopenharmony_ci otherChannel.port2.close(); 671cb0ef41Sopenharmony_ci})); 681cb0ef41Sopenharmony_ciport2.postMessage({ p: otherChannel.port1 }, [ otherChannel.port1 ]); 69