-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: implement worker.moveMessagePortToContext()
This enables using `MessagePort`s in different `vm.Context`s, aiding with the isolation that the `vm` module seeks to provide. Refs: ayojs/ayo#111 PR-URL: #26497 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
6 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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); | ||
} | ||
|
||
{ | ||
const newDummyPort = new (port.constructor)(); | ||
assert(!(newDummyPort instanceof MessagePort)); | ||
assert(newDummyPort.close instanceof Function); | ||
newDummyPort.close(); | ||
} | ||
} + ')()', context); | ||
|
||
port2.on('message', common.mustCall((msg) => { | ||
assert(msg instanceof Object); | ||
port2.close(); | ||
})); | ||
port2.postMessage({}); |