Skip to content

Commit e6f0680

Browse files
addaleaxtargos
authored andcommitted
src: simplify handle closing
Remove one extra closing state and use a smart pointer for deleting `HandleWrap`s. PR-URL: #20876 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: John-David Dalton <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 65924c7 commit e6f0680

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/handle_wrap.cc

+7-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
6767
wrap->Close(args[0]);
6868
}
6969

70-
void HandleWrap::Close(v8::Local<v8::Value> close_callback) {
70+
void HandleWrap::Close(Local<Value> close_callback) {
7171
if (state_ != kInitialized)
7272
return;
7373

@@ -77,8 +77,7 @@ void HandleWrap::Close(v8::Local<v8::Value> close_callback) {
7777

7878
if (!close_callback.IsEmpty() && close_callback->IsFunction()) {
7979
object()->Set(env()->context(), env()->onclose_string(), close_callback)
80-
.FromJust();
81-
state_ = kClosingWithCallback;
80+
.FromMaybe(false);
8281
}
8382
}
8483

@@ -109,24 +108,23 @@ HandleWrap::HandleWrap(Environment* env,
109108

110109

111110
void HandleWrap::OnClose(uv_handle_t* handle) {
112-
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
111+
std::unique_ptr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
113112
Environment* env = wrap->env();
114113
HandleScope scope(env->isolate());
115114
Context::Scope context_scope(env->context());
116115

117116
// The wrap object should still be there.
118117
CHECK_EQ(wrap->persistent().IsEmpty(), false);
119-
CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback);
118+
CHECK_EQ(wrap->state_, kClosing);
120119

121-
const bool have_close_callback = (wrap->state_ == kClosingWithCallback);
122120
wrap->state_ = kClosed;
123121

124122
wrap->OnClose();
125123

126-
if (have_close_callback)
124+
if (wrap->object()->Has(env->context(), env->onclose_string())
125+
.FromMaybe(false)) {
127126
wrap->MakeCallback(env->onclose_string(), 0, nullptr);
128-
129-
delete wrap;
127+
}
130128
}
131129

132130

src/handle_wrap.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class HandleWrap : public AsyncWrap {
9595
// refer to `doc/guides/node-postmortem-support.md`
9696
friend int GenDebugSymbols();
9797
ListNode<HandleWrap> handle_wrap_queue_;
98-
enum { kInitialized, kClosing, kClosingWithCallback, kClosed } state_;
98+
enum { kInitialized, kClosing, kClosed } state_;
9999
uv_handle_t* const handle_;
100100
};
101101

0 commit comments

Comments
 (0)