worker: fix type check in receiveMessageOnPort

Use the same type check we use in `MoveToContext()` in
`ReceiveMessage()`.

Fixes: https://github.com/nodejs/node/issues/32742

PR-URL: https://github.com/nodejs/node/pull/32745
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
pull/32815/head
Anna Henningsen 2020-04-09 18:44:28 +02:00
parent cf888ac105
commit a46345d8af
No known key found for this signature in database
GPG Key ID: A94130F0BFC8EBE9
2 changed files with 14 additions and 1 deletions

View File

@ -865,7 +865,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
}
void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject() ||
!env->message_port_constructor_template()->HasInstance(args[0])) {
return THROW_ERR_INVALID_ARG_TYPE(env,
"First argument needs to be a MessagePort instance");
}
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
if (port == nullptr) {
// Return 'no messages' for a closed port.

View File

@ -23,3 +23,11 @@ port2.on('message', common.mustNotCall());
port1.postMessage(message1);
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
port1.close();
for (const value of [null, 0, -1, {}, []]) {
assert.throws(() => receiveMessageOnPort(value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'First argument needs to be a MessagePort instance'
});
}