worker: use DataCloneError for unknown native objects

This aligns the behaviour better with the web.

PR-URL: https://github.com/nodejs/node/pull/28025
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
This commit is contained in:
Anna Henningsen 2019-06-02 16:46:13 +02:00
parent 55de20999b
commit 7bd2a3fcb4
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
5 changed files with 49 additions and 9 deletions

View File

@ -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

View File

@ -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") \

View File

@ -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") \

View File

@ -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>();
}

View File

@ -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();
}