• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { MessageChannel } = require('worker_threads');
6const { internalBinding } = require('internal/test/binding');
7
8// Test that passing native objects and functions to .postMessage() throws
9// DataCloneError exceptions.
10
11{
12  const { port1, port2 } = new MessageChannel();
13  port2.once('message', common.mustNotCall());
14
15  assert.throws(() => {
16    port1.postMessage(function foo() {});
17  }, {
18    name: 'DataCloneError',
19    message: /function foo\(\) \{\} could not be cloned\.$/
20  });
21  port1.close();
22}
23
24{
25  const { port1, port2 } = new MessageChannel();
26  port2.once('message', common.mustNotCall());
27
28  const nativeObject = new (internalBinding('js_stream').JSStream)();
29
30  assert.throws(() => {
31    port1.postMessage(nativeObject);
32  }, {
33    name: 'DataCloneError',
34    message: /Cannot transfer object of unsupported type\.$/
35  });
36  port1.close();
37}
38