From ea575b7941502263d34f6651f690d263e8f530e9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 2 Aug 2018 01:07:57 +0200 Subject: [PATCH] src: yield empty maybes for failed AsyncWrap::MakeCallback calls Use an empty `MaybeLocal` 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). --- src/callback_scope.cc | 2 +- src/env-inl.h | 4 ++-- src/env.h | 3 ++- src/node.cc | 18 +++++++++++------- src/node_internals.h | 3 --- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/callback_scope.cc b/src/callback_scope.cc index 7929fd67d7667e..eca405dfc14124 100644 --- a/src/callback_scope.cc +++ b/src/callback_scope.cc @@ -87,7 +87,7 @@ void InternalCallbackScope::Close() { AsyncWrap::EmitAfter(env_, async_context_.async_id); } - if (IsInnerMakeCallback()) { + if (env_->makecallback_depth() > 1) { return; } diff --git a/src/env-inl.h b/src/env-inl.h index c38e37f786374d..d65d7e7562f286 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -218,8 +218,8 @@ inline Environment::AsyncCallbackScope::~AsyncCallbackScope() { env_->makecallback_cntr_--; } -inline bool Environment::AsyncCallbackScope::in_makecallback() const { - return env_->makecallback_cntr_ > 1; +inline size_t Environment::makecallback_depth() const { + return makecallback_cntr_; } inline Environment::ImmediateInfo::ImmediateInfo(v8::Isolate* isolate) diff --git a/src/env.h b/src/env.h index 91ae7824281cb7..7445809d80e5eb 100644 --- a/src/env.h +++ b/src/env.h @@ -513,7 +513,6 @@ class Environment { AsyncCallbackScope() = delete; explicit AsyncCallbackScope(Environment* env); ~AsyncCallbackScope(); - inline bool in_makecallback() const; private: Environment* env_; @@ -521,6 +520,8 @@ class Environment { DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope); }; + inline size_t makecallback_depth() const; + class ImmediateInfo { public: inline AliasedBuffer& fields(); diff --git a/src/node.cc b/src/node.cc index 886243fd5cdb50..a9523dd98b8cd7 100644 --- a/src/node.cc +++ b/src/node.cc @@ -757,7 +757,7 @@ MaybeLocal InternalMakeCallback(Environment* env, CHECK(!recv.IsEmpty()); InternalCallbackScope scope(env, recv, asyncContext); if (scope.Failed()) { - return Undefined(env->isolate()); + return MaybeLocal(); } Local domain_cb = env->domain_callback(); @@ -772,15 +772,13 @@ MaybeLocal InternalMakeCallback(Environment* env, } if (ret.IsEmpty()) { - // NOTE: For backwards compatibility with public API we return Undefined() - // if the top level call threw. scope.MarkAsFailed(); - return scope.IsInnerMakeCallback() ? ret : Undefined(env->isolate()); + return MaybeLocal(); } scope.Close(); if (scope.Failed()) { - return Undefined(env->isolate()); + return MaybeLocal(); } return ret; @@ -832,8 +830,14 @@ MaybeLocal MakeCallback(Isolate* isolate, // the two contexts need not be the same. Environment* env = Environment::GetCurrent(callback->CreationContext()); Context::Scope context_scope(env->context()); - return InternalMakeCallback(env, recv, callback, - argc, argv, asyncContext); + MaybeLocal ret = InternalMakeCallback(env, recv, callback, + argc, argv, asyncContext); + if (ret.IsEmpty() && env->makecallback_depth() == 0) { + // This is only for legacy compatiblity and we may want to look into + // removing/adjusting it. + return Undefined(env->isolate()); + } + return ret; } diff --git a/src/node_internals.h b/src/node_internals.h index a27e5f3567b673..c2b5cf98140d2f 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -543,9 +543,6 @@ class InternalCallbackScope { inline bool Failed() const { return failed_; } inline void MarkAsFailed() { failed_ = true; } - inline bool IsInnerMakeCallback() const { - return callback_scope_.in_makecallback(); - } private: Environment* env_;