11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { MessageChannel } = require('worker_threads');
61cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Test that passing native objects and functions to .postMessage() throws
91cb0ef41Sopenharmony_ci// DataCloneError exceptions.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const { port1, port2 } = new MessageChannel();
131cb0ef41Sopenharmony_ci  port2.once('message', common.mustNotCall());
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  assert.throws(() => {
161cb0ef41Sopenharmony_ci    port1.postMessage(function foo() {});
171cb0ef41Sopenharmony_ci  }, {
181cb0ef41Sopenharmony_ci    name: 'DataCloneError',
191cb0ef41Sopenharmony_ci    message: /function foo\(\) \{\} could not be cloned\.$/
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci  port1.close();
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci{
251cb0ef41Sopenharmony_ci  const { port1, port2 } = new MessageChannel();
261cb0ef41Sopenharmony_ci  port2.once('message', common.mustNotCall());
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const nativeObject = new (internalBinding('js_stream').JSStream)();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  assert.throws(() => {
311cb0ef41Sopenharmony_ci    port1.postMessage(nativeObject);
321cb0ef41Sopenharmony_ci  }, {
331cb0ef41Sopenharmony_ci    name: 'DataCloneError',
341cb0ef41Sopenharmony_ci    message: /Cannot transfer object of unsupported type\.$/
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  port1.close();
371cb0ef41Sopenharmony_ci}
38