diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-03-06 15:32:40 +0100 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-03-06 15:32:40 +0100 |
commit | 958ab661cbede92470ffd47b23607c61f5e61e17 (patch) | |
tree | 170604f950aedb908c54e2aea1c2835b6f85c8b8 /src | |
parent | 3d20905b7055dec006041c1fda0094bef17c80ee (diff) | |
download | node-958ab661cbede92470ffd47b23607c61f5e61e17.tar.gz |
handle_wrap: add close callback support
Diffstat (limited to 'src')
-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 9c243ea2c..a667bb47c 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 c77ec77f1..6780cd72f 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; }; |