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_; };