@@ -20,28 +20,25 @@ using v8::Value;
20
20
void HandleWrap::Ref (const FunctionCallbackInfo<Value>& args) {
21
21
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder ());
22
22
23
- if (IsAlive (wrap)) {
24
- uv_ref (wrap->handle__ );
25
- wrap->flags_ &= ~kUnref ;
26
- }
23
+ if (IsAlive (wrap))
24
+ uv_ref (wrap->GetHandle ());
27
25
}
28
26
29
27
30
28
void HandleWrap::Unref (const FunctionCallbackInfo<Value>& args) {
31
29
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder ());
32
30
33
- if (IsAlive (wrap)) {
34
- uv_unref (wrap->handle__ );
35
- wrap->flags_ |= kUnref ;
36
- }
31
+ if (IsAlive (wrap))
32
+ uv_unref (wrap->GetHandle ());
37
33
}
38
34
39
35
40
36
void HandleWrap::Unrefed (const FunctionCallbackInfo<Value>& args) {
41
37
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder ());
42
-
43
- bool unrefed = wrap->flags_ & kUnref == 1 ;
44
- args.GetReturnValue ().Set (unrefed);
38
+ // XXX(bnoordhuis) It's debatable whether a nullptr wrap should count
39
+ // as having a reference count but it's compatible with the logic that
40
+ // it replaces.
41
+ args.GetReturnValue ().Set (wrap == nullptr || !HasRef (wrap));
45
42
}
46
43
47
44
@@ -50,17 +47,17 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
50
47
51
48
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder ());
52
49
53
- // guard against uninitialized handle or double close
50
+ // Guard against uninitialized handle or double close.
54
51
if (!IsAlive (wrap))
55
52
return ;
56
53
57
54
CHECK_EQ (false , wrap->persistent ().IsEmpty ());
58
55
uv_close (wrap->handle__ , OnClose);
59
- wrap->handle__ = nullptr ;
56
+ wrap->state_ = kClosing ;
60
57
61
58
if (args[0 ]->IsFunction ()) {
62
59
wrap->object ()->Set (env->onclose_string (), args[0 ]);
63
- wrap->flags_ |= kCloseCallback ;
60
+ wrap->state_ = kClosingWithCallback ;
64
61
}
65
62
}
66
63
@@ -71,7 +68,7 @@ HandleWrap::HandleWrap(Environment* env,
71
68
AsyncWrap::ProviderType provider,
72
69
AsyncWrap* parent)
73
70
: AsyncWrap(env, object, provider, parent),
74
- flags_ ( 0 ),
71
+ state_ ( kInitialized ),
75
72
handle__(handle) {
76
73
handle__->data = this ;
77
74
HandleScope scope (env->isolate ());
@@ -89,22 +86,19 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
89
86
HandleWrap* wrap = static_cast <HandleWrap*>(handle->data );
90
87
Environment* env = wrap->env ();
91
88
HandleScope scope (env->isolate ());
89
+ Context::Scope context_scope (env->context ());
92
90
93
91
// The wrap object should still be there.
94
92
CHECK_EQ (wrap->persistent ().IsEmpty (), false );
93
+ CHECK (wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback );
95
94
96
- // But the handle pointer should be gone.
97
- CHECK_EQ ( wrap->handle__ , nullptr ) ;
95
+ const bool have_close_callback = (wrap-> state_ == kClosingWithCallback );
96
+ wrap->state_ = kClosed ;
98
97
99
- HandleScope handle_scope (env->isolate ());
100
- Context::Scope context_scope (env->context ());
101
- Local<Object> object = wrap->object ();
102
-
103
- if (wrap->flags_ & kCloseCallback ) {
98
+ if (have_close_callback)
104
99
wrap->MakeCallback (env->onclose_string (), 0 , nullptr );
105
- }
106
100
107
- object->SetAlignedPointerInInternalField (0 , nullptr );
101
+ wrap-> object () ->SetAlignedPointerInInternalField (0 , nullptr );
108
102
wrap->persistent ().Reset ();
109
103
delete wrap;
110
104
}
0 commit comments