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>
This commit is contained in:
Anna Henningsen authored and Beth Griggs committed 2020-04-14 11:03:55 +01:00
1 parent 0538dd1182
commit 7147df53e8
2 files changed
+14 -1

No files matched your search

+6 -1
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.
@@ -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'
});
}