Skip to content

Commit 45d0b4a

Browse files
addaleaxjasnell
authored andcommitted
src: yield empty maybes for failed AsyncWrap::MakeCallback calls
Use an empty `MaybeLocal<Value>` as the return value for `AsyncWrap::MakeCallback()` if an exception occurred, regardless of `MakeCallback` depth. Unfortunately, the public API can not make this switch without a breaking change (and possibly some kind of deprecation/renaming). PR-URL: #22078 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent 801c490 commit 45d0b4a

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

src/callback_scope.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void InternalCallbackScope::Close() {
8787
AsyncWrap::EmitAfter(env_, async_context_.async_id);
8888
}
8989

90-
if (IsInnerMakeCallback()) {
90+
if (env_->makecallback_depth() > 1) {
9191
return;
9292
}
9393

src/env-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
218218
env_->makecallback_cntr_--;
219219
}
220220

221-
inline bool Environment::AsyncCallbackScope::in_makecallback() const {
222-
return env_->makecallback_cntr_ > 1;
221+
inline size_t Environment::makecallback_depth() const {
222+
return makecallback_cntr_;
223223
}
224224

225225
inline Environment::ImmediateInfo::ImmediateInfo(v8::Isolate* isolate)

src/env.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,15 @@ class Environment {
499499
AsyncCallbackScope() = delete;
500500
explicit AsyncCallbackScope(Environment* env);
501501
~AsyncCallbackScope();
502-
inline bool in_makecallback() const;
503502

504503
private:
505504
Environment* env_;
506505

507506
DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope);
508507
};
509508

509+
inline size_t makecallback_depth() const;
510+
510511
class ImmediateInfo {
511512
public:
512513
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();

src/node.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
757757
CHECK(!recv.IsEmpty());
758758
InternalCallbackScope scope(env, recv, asyncContext);
759759
if (scope.Failed()) {
760-
return Undefined(env->isolate());
760+
return MaybeLocal<Value>();
761761
}
762762

763763
Local<Function> domain_cb = env->domain_callback();
@@ -772,15 +772,13 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
772772
}
773773

774774
if (ret.IsEmpty()) {
775-
// NOTE: For backwards compatibility with public API we return Undefined()
776-
// if the top level call threw.
777775
scope.MarkAsFailed();
778-
return scope.IsInnerMakeCallback() ? ret : Undefined(env->isolate());
776+
return MaybeLocal<Value>();
779777
}
780778

781779
scope.Close();
782780
if (scope.Failed()) {
783-
return Undefined(env->isolate());
781+
return MaybeLocal<Value>();
784782
}
785783

786784
return ret;
@@ -832,8 +830,14 @@ MaybeLocal<Value> MakeCallback(Isolate* isolate,
832830
// the two contexts need not be the same.
833831
Environment* env = Environment::GetCurrent(callback->CreationContext());
834832
Context::Scope context_scope(env->context());
835-
return InternalMakeCallback(env, recv, callback,
836-
argc, argv, asyncContext);
833+
MaybeLocal<Value> ret = InternalMakeCallback(env, recv, callback,
834+
argc, argv, asyncContext);
835+
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
836+
// This is only for legacy compatiblity and we may want to look into
837+
// removing/adjusting it.
838+
return Undefined(env->isolate());
839+
}
840+
return ret;
837841
}
838842

839843

src/node_internals.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,6 @@ class InternalCallbackScope {
543543

544544
inline bool Failed() const { return failed_; }
545545
inline void MarkAsFailed() { failed_ = true; }
546-
inline bool IsInnerMakeCallback() const {
547-
return callback_scope_.in_makecallback();
548-
}
549546

550547
private:
551548
Environment* env_;

0 commit comments

Comments
 (0)