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