Skip to content

Commit

Permalink
src: simplify handle closing
Browse files Browse the repository at this point in the history
Remove one extra closing state and use a smart pointer for
deleting `HandleWrap`s.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information
addaleax authored and targos committed Jun 13, 2018
1 parent 65924c7 commit e6f0680
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
wrap->Close(args[0]);
}

void HandleWrap::Close(v8::Local<v8::Value> close_callback) {
void HandleWrap::Close(Local<Value> close_callback) {
if (state_ != kInitialized)
return;

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

if (!close_callback.IsEmpty() && close_callback->IsFunction()) {
object()->Set(env()->context(), env()->onclose_string(), close_callback)
.FromJust();
state_ = kClosingWithCallback;
.FromMaybe(false);
}
}

Expand Down Expand Up @@ -109,24 +108,23 @@ HandleWrap::HandleWrap(Environment* env,


void HandleWrap::OnClose(uv_handle_t* handle) {
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
std::unique_ptr<HandleWrap> wrap { static_cast<HandleWrap*>(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;
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/handle_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class HandleWrap : public AsyncWrap {
// refer to `doc/guides/node-postmortem-support.md`
friend int GenDebugSymbols();
ListNode<HandleWrap> handle_wrap_queue_;
enum { kInitialized, kClosing, kClosingWithCallback, kClosed } state_;
enum { kInitialized, kClosing, kClosed } state_;
uv_handle_t* const handle_;
};

Expand Down

0 comments on commit e6f0680

Please sign in to comment.