diff options
-rw-r--r-- | src/handle_wrap.cc | 41 | ||||
-rw-r--r-- | src/handle_wrap.h | 1 |
2 files changed, 29 insertions, 13 deletions
diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc index 9c243ea2c9..a667bb47c5 100644 --- a/src/handle_wrap.cc +++ b/src/handle_wrap.cc @@ -31,24 +31,26 @@ namespace node { +using v8::Arguments; using v8::Array; -using v8::Object; +using v8::Context; +using v8::Function; +using v8::FunctionTemplate; using v8::Handle; +using v8::HandleScope; +using v8::Integer; using v8::Local; +using v8::Object; using v8::Persistent; -using v8::Value; -using v8::HandleScope; -using v8::FunctionTemplate; using v8::String; -using v8::Function; using v8::TryCatch; -using v8::Context; -using v8::Arguments; -using v8::Integer; +using v8::Undefined; +using v8::Value; // defined in node.cc extern ngx_queue_t handle_wrap_queue; +static Persistent<String> close_sym; void HandleWrap::Initialize(Handle<Object> target) { @@ -91,13 +93,21 @@ Handle<Value> HandleWrap::Close(const Arguments& args) { args.Holder()->GetPointerFromInternalField(0)); // guard against uninitialized handle or double close - if (wrap && wrap->handle__) { - assert(!wrap->object_.IsEmpty()); - uv_close(wrap->handle__, OnClose); - wrap->handle__ = NULL; + if (wrap == NULL || wrap->handle__ == NULL) { + return Undefined(); } - return v8::Null(); + assert(!wrap->object_.IsEmpty()); + uv_close(wrap->handle__, OnClose); + wrap->handle__ = NULL; + + if (args[0]->IsFunction()) { + if (close_sym.IsEmpty() == true) close_sym = NODE_PSYMBOL("close"); + wrap->object_->Set(close_sym, args[0]); + wrap->flags_ |= kCloseCallback; + } + + return Undefined(); } @@ -138,6 +148,11 @@ void HandleWrap::OnClose(uv_handle_t* handle) { // But the handle pointer should be gone. assert(wrap->handle__ == NULL); + if (wrap->flags_ & kCloseCallback) { + assert(close_sym.IsEmpty() == false); + MakeCallback(wrap->object_, close_sym, 0, NULL); + } + wrap->object_->SetPointerInInternalField(0, NULL); wrap->object_.Dispose(); wrap->object_.Clear(); diff --git a/src/handle_wrap.h b/src/handle_wrap.h index c77ec77f1a..6780cd72fe 100644 --- a/src/handle_wrap.h +++ b/src/handle_wrap.h @@ -73,6 +73,7 @@ class HandleWrap { unsigned int flags_; static const unsigned int kUnref = 1; + static const unsigned int kCloseCallback = 2; }; |