diff options
-rw-r--r-- | doc/api/errors.md | 16 | ||||
-rw-r--r-- | src/env.h | 1 | ||||
-rw-r--r-- | src/node_errors.h | 2 | ||||
-rw-r--r-- | src/node_messaging.cc | 2 | ||||
-rw-r--r-- | test/parallel/test-worker-message-port-transfer-native.js | 37 |
5 files changed, 49 insertions, 9 deletions
diff --git a/doc/api/errors.md b/doc/api/errors.md index ebbe215b60..131773143c 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -671,12 +671,6 @@ An operation outside the bounds of a `Buffer` was attempted. An attempt has been made to create a `Buffer` larger than the maximum allowed size. -<a id="ERR_CANNOT_TRANSFER_OBJECT"></a> -### ERR_CANNOT_TRANSFER_OBJECT - -The value passed to `postMessage()` contained an object that is not supported -for transferring. - <a id="ERR_CANNOT_WATCH_SIGINT"></a> ### ERR_CANNOT_WATCH_SIGINT @@ -2013,6 +2007,16 @@ A module file could not be resolved while attempting a [`require()`][] or > Stability: 0 - Deprecated. These error codes are either inconsistent, or have > been removed. +<a id="ERR_CANNOT_TRANSFER_OBJECT"></a> +### ERR_CANNOT_TRANSFER_OBJECT +<!-- +added: v10.5.0 +removed: REPLACEME +--> + +The value passed to `postMessage()` contained an object that is not supported +for transferring. + <a id="ERR_CLOSED_MESSAGE_PORT"></a> ### ERR_CLOSED_MESSAGE_PORT <!-- YAML @@ -158,6 +158,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; V(change_string, "change") \ V(channel_string, "channel") \ V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \ + V(clone_unsupported_type_str, "Cannot transfer object of unsupported type.") \ V(code_string, "code") \ V(commonjs_string, "commonjs") \ V(config_string, "config") \ diff --git a/src/node_errors.h b/src/node_errors.h index 7162d05c1a..0dad93f31f 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -54,7 +54,6 @@ void FatalException(v8::Isolate* isolate, V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \ V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \ V(ERR_BUFFER_TOO_LARGE, Error) \ - V(ERR_CANNOT_TRANSFER_OBJECT, TypeError) \ V(ERR_CONSTRUCT_CALL_REQUIRED, Error) \ V(ERR_INVALID_ARG_VALUE, TypeError) \ V(ERR_INVALID_ARG_TYPE, TypeError) \ @@ -100,7 +99,6 @@ void FatalException(v8::Isolate* isolate, #define PREDEFINED_ERROR_MESSAGES(V) \ V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ "Buffer is not available for the current Context") \ - V(ERR_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\ V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 0e782ef726..ba39d01dc3 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -232,7 +232,7 @@ class SerializerDelegate : public ValueSerializer::Delegate { return WriteMessagePort(Unwrap<MessagePort>(object)); } - THROW_ERR_CANNOT_TRANSFER_OBJECT(env_); + ThrowDataCloneError(env_->clone_unsupported_type_str()); return Nothing<bool>(); } diff --git a/test/parallel/test-worker-message-port-transfer-native.js b/test/parallel/test-worker-message-port-transfer-native.js new file mode 100644 index 0000000000..eb51b2b01a --- /dev/null +++ b/test/parallel/test-worker-message-port-transfer-native.js @@ -0,0 +1,37 @@ +// Flags: --expose-internals +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { MessageChannel } = require('worker_threads'); +const { internalBinding } = require('internal/test/binding'); + +// Test that passing native objects and functions to .postMessage() throws +// DataCloneError exceptions. + +{ + const { port1, port2 } = new MessageChannel(); + port2.once('message', common.mustNotCall()); + + assert.throws(() => { + port1.postMessage(function foo() {}); + }, { + name: 'DataCloneError', + message: /function foo\(\) \{\} could not be cloned\.$/ + }); + port1.close(); +} + +{ + const { port1, port2 } = new MessageChannel(); + port2.once('message', common.mustNotCall()); + + const nativeObject = new (internalBinding('js_stream').JSStream)(); + + assert.throws(() => { + port1.postMessage(nativeObject); + }, { + name: 'DataCloneError', + message: /Cannot transfer object of unsupported type\.$/ + }); + port1.close(); +} |