Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
handle_wrap.cc
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "handle_wrap.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
25 #include "env.h"
26 #include "env-inl.h"
27 #include "util.h"
28 #include "util-inl.h"
29 #include "node.h"
30 
31 namespace node {
32 
33 using v8::Context;
34 using v8::FunctionCallbackInfo;
35 using v8::HandleScope;
36 using v8::Local;
37 using v8::Object;
38 using v8::Value;
39 
40 
41 void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
42  HandleWrap* wrap;
43  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
44 
45  if (IsAlive(wrap))
46  uv_ref(wrap->GetHandle());
47 }
48 
49 
50 void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
51  HandleWrap* wrap;
52  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
53 
54  if (IsAlive(wrap))
55  uv_unref(wrap->GetHandle());
56 }
57 
58 
59 void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
60  HandleWrap* wrap;
61  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
62  args.GetReturnValue().Set(HasRef(wrap));
63 }
64 
65 
66 void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
67  Environment* env = Environment::GetCurrent(args);
68 
69  HandleWrap* wrap;
70  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
71 
72  // Guard against uninitialized handle or double close.
73  if (!IsAlive(wrap))
74  return;
75 
76  if (wrap->state_ != kInitialized)
77  return;
78 
79  CHECK_EQ(false, wrap->persistent().IsEmpty());
80  uv_close(wrap->handle_, OnClose);
81  wrap->state_ = kClosing;
82 
83  if (args[0]->IsFunction()) {
84  wrap->object()->Set(env->onclose_string(), args[0]);
85  wrap->state_ = kClosingWithCallback;
86  }
87 }
88 
89 
90 HandleWrap::HandleWrap(Environment* env,
91  Local<Object> object,
92  uv_handle_t* handle,
93  AsyncWrap::ProviderType provider)
94  : AsyncWrap(env, object, provider),
95  state_(kInitialized),
96  handle_(handle) {
97  handle_->data = this;
98  HandleScope scope(env->isolate());
99  Wrap(object, this);
100  env->handle_wrap_queue()->PushBack(this);
101 }
102 
103 
104 HandleWrap::~HandleWrap() {
105  CHECK(persistent().IsEmpty());
106 }
107 
108 
109 void HandleWrap::OnClose(uv_handle_t* handle) {
110  HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
111  Environment* env = wrap->env();
112  HandleScope scope(env->isolate());
113  Context::Scope context_scope(env->context());
114 
115  // The wrap object should still be there.
116  CHECK_EQ(wrap->persistent().IsEmpty(), false);
117  CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback);
118 
119  const bool have_close_callback = (wrap->state_ == kClosingWithCallback);
120  wrap->state_ = kClosed;
121 
122  if (have_close_callback)
123  wrap->MakeCallback(env->onclose_string(), 0, nullptr);
124 
125  ClearWrap(wrap->object());
126  wrap->persistent().Reset();
127  delete wrap;
128 }
129 
130 
131 } // namespace node
QueryWrap * wrap
Definition: cares_wrap.cc:478