From 03fb817a1d7c0e704c380c09b2dfd36533f750ba Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Tue, 13 Mar 2018 12:03:27 +0100 Subject: [PATCH] src: add extractPromiseWrap function Currently PromiseHook extracts the PromiseWrap from a Local in two places. This commit extracts that code into a function instead. PR-URL: https://github.com/nodejs/node/pull/19340 Reviewed-By: James M Snell --- src/async_wrap.cc | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 98bda0ac743a85..2bad64ddbee888 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -277,19 +277,20 @@ void PromiseWrap::getIsChainedPromise(Local property, info.Holder()->GetInternalField(kIsChainedPromiseField)); } -static void PromiseHook(PromiseHookType type, Local promise, - Local parent, void* arg) { - Environment* env = static_cast(arg); +static PromiseWrap* extractPromiseWrap(Local promise) { Local resource_object_value = promise->GetInternalField(0); - PromiseWrap* wrap = nullptr; if (resource_object_value->IsObject()) { - Local resource_object = resource_object_value.As(); - wrap = Unwrap(resource_object); + return Unwrap(resource_object_value.As()); } + return nullptr; +} +static void PromiseHook(PromiseHookType type, Local promise, + Local parent, void* arg) { + Environment* env = static_cast(arg); + PromiseWrap* wrap = extractPromiseWrap(promise); if (type == PromiseHookType::kInit || wrap == nullptr) { bool silent = type != PromiseHookType::kInit; - PromiseWrap* parent_wrap = nullptr; // set parent promise's async Id as this promise's triggerAsyncId if (parent->IsPromise()) { @@ -297,11 +298,7 @@ static void PromiseHook(PromiseHookType type, Local promise, // is a chained promise, so we set parent promise's id as // current promise's triggerAsyncId Local parent_promise = parent.As(); - Local parent_resource = parent_promise->GetInternalField(0); - if (parent_resource->IsObject()) { - parent_wrap = Unwrap(parent_resource.As()); - } - + PromiseWrap* parent_wrap = extractPromiseWrap(parent_promise); if (parent_wrap == nullptr) { parent_wrap = PromiseWrap::New(env, parent_promise, nullptr, true); }