From e6f06807b1f158d6a1fcb22fc1f5cd88068e6d57 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Sep 2017 05:05:48 +0200 Subject: [PATCH] src: simplify handle closing Remove one extra closing state and use a smart pointer for deleting `HandleWrap`s. PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Shingo Inoue Reviewed-By: Matteo Collina Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: John-David Dalton Reviewed-By: Gus Caplan --- src/handle_wrap.cc | 16 +++++++--------- src/handle_wrap.h | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc index 20356b94a5775a..bbc124ff69dc89 100644 --- a/src/handle_wrap.cc +++ b/src/handle_wrap.cc @@ -67,7 +67,7 @@ void HandleWrap::Close(const FunctionCallbackInfo& args) { wrap->Close(args[0]); } -void HandleWrap::Close(v8::Local close_callback) { +void HandleWrap::Close(Local close_callback) { if (state_ != kInitialized) return; @@ -77,8 +77,7 @@ void HandleWrap::Close(v8::Local close_callback) { if (!close_callback.IsEmpty() && close_callback->IsFunction()) { object()->Set(env()->context(), env()->onclose_string(), close_callback) - .FromJust(); - state_ = kClosingWithCallback; + .FromMaybe(false); } } @@ -109,24 +108,23 @@ HandleWrap::HandleWrap(Environment* env, void HandleWrap::OnClose(uv_handle_t* handle) { - HandleWrap* wrap = static_cast(handle->data); + std::unique_ptr wrap { static_cast(handle->data) }; Environment* env = wrap->env(); HandleScope scope(env->isolate()); Context::Scope context_scope(env->context()); // The wrap object should still be there. CHECK_EQ(wrap->persistent().IsEmpty(), false); - CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback); + CHECK_EQ(wrap->state_, kClosing); - const bool have_close_callback = (wrap->state_ == kClosingWithCallback); wrap->state_ = kClosed; wrap->OnClose(); - if (have_close_callback) + if (wrap->object()->Has(env->context(), env->onclose_string()) + .FromMaybe(false)) { wrap->MakeCallback(env->onclose_string(), 0, nullptr); - - delete wrap; + } } diff --git a/src/handle_wrap.h b/src/handle_wrap.h index b2b09f5010d1f7..4e177d249f28b5 100644 --- a/src/handle_wrap.h +++ b/src/handle_wrap.h @@ -95,7 +95,7 @@ class HandleWrap : public AsyncWrap { // refer to `doc/guides/node-postmortem-support.md` friend int GenDebugSymbols(); ListNode handle_wrap_queue_; - enum { kInitialized, kClosing, kClosingWithCallback, kClosed } state_; + enum { kInitialized, kClosing, kClosed } state_; uv_handle_t* const handle_; };