• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { MessageChannel, markAsUntransferable } = require('worker_threads');
5
6{
7  const ab = new ArrayBuffer(8);
8
9  markAsUntransferable(ab);
10  assert.strictEqual(ab.byteLength, 8);
11
12  const { port1, port2 } = new MessageChannel();
13  port1.postMessage(ab, [ ab ]);
14
15  assert.strictEqual(ab.byteLength, 8);  // The AB is not detached.
16  port2.once('message', common.mustCall());
17}
18
19{
20  const channel1 = new MessageChannel();
21  const channel2 = new MessageChannel();
22
23  markAsUntransferable(channel2.port1);
24
25  assert.throws(() => {
26    channel1.port1.postMessage(channel2.port1, [ channel2.port1 ]);
27  }, /was found in message but not listed in transferList/);
28
29  channel2.port1.postMessage('still works, not closed/transferred');
30  channel2.port2.once('message', common.mustCall());
31}
32
33{
34  for (const value of [0, null, false, true, undefined, [], {}]) {
35    markAsUntransferable(value);  // Has no visible effect.
36  }
37}
38