Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! src: do proper error checking in AsyncWrap::MakeCallback
  • Loading branch information
addaleax committed Jun 11, 2018
commit 032b3900c6f8d0e8ce0b7b4a58711a314fb36960
5 changes: 1 addition & 4 deletions src/async_wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
if (!object()->Get(env()->context(), symbol).ToLocal(&cb_v))
return v8::MaybeLocal<v8::Value>();
if (!cb_v->IsFunction()) {
// Due to V8’s error handling mechanisms, this will not show up as an error
// in the common case, which is that this is outside of any JS frame.
// So, the exception here is mostly just there to fulfill the
// TODO(addaleax): We should throw an error here to fulfill the
// `MaybeLocal<>` API contract.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, this is something I’d like to fix by adding a verbose TryCatch around the event loop (if I can make it work – not sure about that). But that would be a semver-major change.

THROW_ERR_MISSING_METHOD(env()->isolate(), symbol);
return v8::MaybeLocal<v8::Value>();
}
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
Expand Down
14 changes: 1 addition & 13 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace node {
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
V(ERR_MISSING_ARGS, TypeError) \
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, TypeError) \
V(ERR_MISSING_METHOD, TypeError) \
V(ERR_MISSING_MODULE, Error) \
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
Expand All @@ -53,11 +52,8 @@ namespace node {
js_code).FromJust(); \
return e; \
} \
inline void THROW_ ## code(v8::Isolate* isolate, const char* message) { \
isolate->ThrowException(code(isolate, message)); \
} \
inline void THROW_ ## code(Environment* env, const char* message) { \
THROW_ ## code (env->isolate(), message); \
env->isolate()->ThrowException(code(env->isolate(), message)); \
}
ERRORS_WITH_CODE(V)
#undef V
Expand Down Expand Up @@ -116,14 +112,6 @@ inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
return ERR_STRING_TOO_LONG(isolate, message);
}

inline void THROW_ERR_MISSING_METHOD(v8::Isolate* isolate,
v8::Local<v8::Name> name) {
Utf8Value name_str(isolate, name);
std::string message("Missing method: ");
message += *name_str;
THROW_ERR_MISSING_METHOD(isolate, message.c_str());
}

#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix) \
do { \
if (!Buffer::HasInstance(val)) \
Expand Down
28 changes: 23 additions & 5 deletions test/parallel/test-async-wrap-missing-method.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
// Flags: --experimental-worker
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

const { MessageChannel } = require('worker_threads');

{
const { port1, port2 } = new MessageChannel();

// Throwing in the getter should not crash.
// Returning a non-function in the getter should not crash.
Object.defineProperty(port1, 'onmessage', {
get() {
throw new Error('eyecatcher');
port1.unref();
return 42;
}
});

port2.postMessage({ foo: 'bar' });

// We need to start the port manually because .onmessage assignment tracking
// has been overridden.
port1.start();
port1.ref();
}

{
const err = new Error('eyecatcher');
process.on('uncaughtException', common.mustCall((exception) => {
port1.unref();
assert.strictEqual(exception, err);
}));

const { port1, port2 } = new MessageChannel();

// Returning a non-function in the getter should not crash.
// Throwing in the getter should not crash.
Object.defineProperty(port1, 'onmessage', {
get() {
return 42;
throw err;
}
});

port2.postMessage({ foo: 'bar' });

// We need to start the port manually because .onmessage assignment tracking
// has been overridden.
port1.start();
port1.ref();
}