58c2d78b2aa6c91bd4f1428f0babcc48acc72813
[platform/upstream/nodejs.git] / src / async-wrap-inl.h
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 #ifndef SRC_ASYNC_WRAP_INL_H_
23 #define SRC_ASYNC_WRAP_INL_H_
24
25 #include "async-wrap.h"
26 #include "base-object.h"
27 #include "base-object-inl.h"
28 #include "env.h"
29 #include "env-inl.h"
30 #include "util.h"
31 #include "util-inl.h"
32
33 #include "v8.h"
34 #include <assert.h>
35
36 namespace node {
37
38 inline AsyncWrap::AsyncWrap(Environment* env, v8::Handle<v8::Object> object)
39     : BaseObject(env, object),
40       async_flags_(NO_OPTIONS) {
41   if (!env->has_async_listeners())
42     return;
43
44   // TODO(trevnorris): Do we really need to TryCatch this call?
45   v8::TryCatch try_catch;
46   try_catch.SetVerbose(true);
47
48   v8::Local<v8::Value> val = object.As<v8::Value>();
49   env->async_listener_run_function()->Call(env->process_object(), 1, &val);
50
51   if (!try_catch.HasCaught())
52     async_flags_ |= ASYNC_LISTENERS;
53 }
54
55
56 inline AsyncWrap::~AsyncWrap() {
57 }
58
59
60 template <typename Type>
61 inline void AsyncWrap::AddMethods(v8::Handle<v8::FunctionTemplate> t) {
62   NODE_SET_PROTOTYPE_METHOD(t,
63                             "addAsyncListener",
64                             AddAsyncListener<Type>);
65   NODE_SET_PROTOTYPE_METHOD(t,
66                             "removeAsyncListener",
67                             RemoveAsyncListener<Type>);
68 }
69
70
71 inline uint32_t AsyncWrap::async_flags() const {
72   return async_flags_;
73 }
74
75
76 inline void AsyncWrap::set_flag(unsigned int flag) {
77   async_flags_ |= flag;
78 }
79
80
81 inline void AsyncWrap::remove_flag(unsigned int flag) {
82   async_flags_ &= ~flag;
83 }
84
85
86 inline bool AsyncWrap::has_async_queue() {
87   return async_flags() & ASYNC_LISTENERS;
88 }
89
90
91 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
92     const v8::Handle<v8::Function> cb,
93     int argc,
94     v8::Handle<v8::Value>* argv) {
95   assert(env()->context() == env()->isolate()->GetCurrentContext());
96
97   v8::Local<v8::Object> context = object();
98   v8::Local<v8::Object> process = env()->process_object();
99
100   v8::TryCatch try_catch;
101   try_catch.SetVerbose(true);
102
103   if (has_async_queue()) {
104     v8::Local<v8::Value> val = context.As<v8::Value>();
105     env()->async_listener_load_function()->Call(process, 1, &val);
106
107     if (try_catch.HasCaught())
108       return v8::Undefined(env()->isolate());
109   }
110
111   v8::Local<v8::Value> ret = cb->Call(context, argc, argv);
112
113   if (try_catch.HasCaught()) {
114     return Undefined(env()->isolate());
115   }
116
117   if (has_async_queue()) {
118     v8::Local<v8::Value> val = context.As<v8::Value>();
119     env()->async_listener_unload_function()->Call(process, 1, &val);
120
121     if (try_catch.HasCaught())
122       return v8::Undefined(env()->isolate());
123   }
124
125   Environment::TickInfo* tick_info = env()->tick_info();
126
127   if (tick_info->in_tick()) {
128     return ret;
129   }
130
131   if (tick_info->length() == 0) {
132     tick_info->set_index(0);
133     return ret;
134   }
135
136   tick_info->set_in_tick(true);
137
138   // TODO(trevnorris): Consider passing "context" to _tickCallback so it
139   // can then be passed as the first argument to the nextTick callback.
140   // That should greatly help needing to create closures.
141   env()->tick_callback_function()->Call(process, 0, NULL);
142
143   tick_info->set_in_tick(false);
144
145   if (try_catch.HasCaught()) {
146     tick_info->set_last_threw(true);
147     return Undefined(env()->isolate());
148   }
149
150   return ret;
151 }
152
153
154 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
155     const v8::Handle<v8::String> symbol,
156     int argc,
157     v8::Handle<v8::Value>* argv) {
158   v8::Local<v8::Value> cb_v = object()->Get(symbol);
159   v8::Local<v8::Function> cb = cb_v.As<v8::Function>();
160   assert(cb->IsFunction());
161
162   return MakeCallback(cb, argc, argv);
163 }
164
165
166 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
167     uint32_t index,
168     int argc,
169     v8::Handle<v8::Value>* argv) {
170   v8::Local<v8::Value> cb_v = object()->Get(index);
171   v8::Local<v8::Function> cb = cb_v.As<v8::Function>();
172   assert(cb->IsFunction());
173
174   return MakeCallback(cb, argc, argv);
175 }
176
177
178 template <typename Type>
179 inline void AsyncWrap::AddAsyncListener(
180     const v8::FunctionCallbackInfo<v8::Value>& args) {
181   v8::HandleScope handle_scope(args.GetIsolate());
182   Environment* env = Environment::GetCurrent(args.GetIsolate());
183
184   v8::Local<v8::Object> handle = args.This();
185   v8::Local<v8::Value> listener = args[0];
186   assert(listener->IsObject());
187   assert(handle->InternalFieldCount() > 0);
188
189   env->async_listener_push_function()->Call(handle, 1, &listener);
190
191   Type* wrap = static_cast<Type*>(
192       handle->GetAlignedPointerFromInternalField(0));
193   assert(wrap != NULL);
194   wrap->set_flag(ASYNC_LISTENERS);
195 }
196
197
198 template <typename Type>
199 inline void AsyncWrap::RemoveAsyncListener(
200     const v8::FunctionCallbackInfo<v8::Value>& args) {
201   v8::HandleScope handle_scope(args.GetIsolate());
202   Environment* env = Environment::GetCurrent(args.GetIsolate());
203
204   v8::Local<v8::Object> handle = args.This();
205   v8::Local<v8::Value> listener = args[0];
206   assert(listener->IsObject());
207   assert(handle->InternalFieldCount() > 0);
208
209   v8::Local<v8::Value> ret =
210       env->async_listener_strip_function()->Call(handle, 1, &listener);
211
212   if (ret->IsFalse()) {
213     Type* wrap = static_cast<Type*>(
214         handle->GetAlignedPointerFromInternalField(0));
215     assert(wrap != NULL);
216     wrap->remove_flag(ASYNC_LISTENERS);
217   }
218 }
219
220 }  // namespace node
221
222 #endif  // SRC_ASYNC_WRAP_INL_H_