1/* global port */ 2'use strict'; 3const common = require('../common'); 4const assert = require('assert'); 5const vm = require('vm'); 6const { 7 MessagePort, MessageChannel, moveMessagePortToContext 8} = require('worker_threads'); 9 10const context = vm.createContext(); 11const { port1, port2 } = new MessageChannel(); 12context.port = moveMessagePortToContext(port1, context); 13context.global = context; 14Object.assign(context, { 15 global: context, 16 assert, 17 MessagePort, 18 MessageChannel 19}); 20 21vm.runInContext('(' + function() { 22 { 23 assert(port.postMessage instanceof Function); 24 assert(port.constructor instanceof Function); 25 for (let obj = port; obj !== null; obj = Object.getPrototypeOf(obj)) { 26 for (const key of Object.getOwnPropertyNames(obj)) { 27 if (typeof obj[key] === 'object' && obj[key] !== null) { 28 assert(obj[key] instanceof Object); 29 } else if (typeof obj[key] === 'function') { 30 assert(obj[key] instanceof Function); 31 } 32 } 33 } 34 35 assert(!(port instanceof MessagePort)); 36 assert.strictEqual(port.onmessage, undefined); 37 port.onmessage = function({ data }) { 38 assert(data instanceof Object); 39 port.postMessage(data); 40 }; 41 port.start(); 42 } 43 44 { 45 let threw = false; 46 try { 47 port.postMessage(global); 48 } catch (e) { 49 assert.strictEqual(e.constructor.name, 'DOMException'); 50 assert(e instanceof Object); 51 assert(e instanceof Error); 52 threw = true; 53 } 54 assert(threw); 55 } 56} + ')()', context); 57 58port2.on('message', common.mustCall((msg) => { 59 assert(msg instanceof Object); 60 port2.close(); 61})); 62port2.postMessage({}); 63