node/test/parallel/test-worker-message-port-move.js
Anna Henningsen 0640526303 worker: make MessagePort constructor non-callable
Refactor the C++ code for creating `MessagePort`s to skip calling the
constructor and instead directly instantiating the `InstanceTemplate`,
and always throw an error from the `MessagePort` constructor.

This aligns behaviour with the web, and creating single `MessagePort`s
does not make sense anyway.

PR-URL: https://github.com/nodejs/node/pull/28032
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-06-12 20:56:44 -07:00

63 lines
1.6 KiB
JavaScript

/* global port */
'use strict';
const common = require('../common');
const assert = require('assert');
const vm = require('vm');
const {
MessagePort, MessageChannel, moveMessagePortToContext
} = require('worker_threads');
const context = vm.createContext();
const { port1, port2 } = new MessageChannel();
context.port = moveMessagePortToContext(port1, context);
context.global = context;
Object.assign(context, {
global: context,
assert,
MessagePort,
MessageChannel
});
vm.runInContext('(' + function() {
{
assert(port.postMessage instanceof Function);
assert(port.constructor instanceof Function);
for (let obj = port; obj !== null; obj = Object.getPrototypeOf(obj)) {
for (const key of Object.getOwnPropertyNames(obj)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
assert(obj[key] instanceof Object);
} else if (typeof obj[key] === 'function') {
assert(obj[key] instanceof Function);
}
}
}
assert(!(port instanceof MessagePort));
assert.strictEqual(port.onmessage, undefined);
port.onmessage = function({ data }) {
assert(data instanceof Object);
port.postMessage(data);
};
port.start();
}
{
let threw = false;
try {
port.postMessage(global);
} catch (e) {
assert.strictEqual(e.constructor.name, 'DOMException');
assert(e instanceof Object);
assert(e instanceof Error);
threw = true;
}
assert(threw);
}
} + ')()', context);
port2.on('message', common.mustCall((msg) => {
assert(msg instanceof Object);
port2.close();
}));
port2.postMessage({});