1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #ifndef SRC_ASYNC_WRAP_INL_H_
23 #define SRC_ASYNC_WRAP_INL_H_
25 #include "async-wrap.h"
26 #include "base-object.h"
27 #include "base-object-inl.h"
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())
44 // TODO(trevnorris): Do we really need to TryCatch this call?
45 v8::TryCatch try_catch;
46 try_catch.SetVerbose(true);
48 v8::Local<v8::Value> val = object.As<v8::Value>();
49 env->async_listener_run_function()->Call(env->process_object(), 1, &val);
51 if (!try_catch.HasCaught())
52 async_flags_ |= ASYNC_LISTENERS;
56 inline AsyncWrap::~AsyncWrap() {
60 template <typename Type>
61 inline void AsyncWrap::AddMethods(v8::Handle<v8::FunctionTemplate> t) {
62 NODE_SET_PROTOTYPE_METHOD(t,
64 AddAsyncListener<Type>);
65 NODE_SET_PROTOTYPE_METHOD(t,
66 "removeAsyncListener",
67 RemoveAsyncListener<Type>);
71 inline uint32_t AsyncWrap::async_flags() const {
76 inline void AsyncWrap::set_flag(unsigned int flag) {
81 inline void AsyncWrap::remove_flag(unsigned int flag) {
82 async_flags_ &= ~flag;
86 inline bool AsyncWrap::has_async_queue() {
87 return async_flags() & ASYNC_LISTENERS;
91 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
92 const v8::Handle<v8::Function> cb,
94 v8::Handle<v8::Value>* argv) {
95 assert(env()->context() == env()->isolate()->GetCurrentContext());
97 v8::Local<v8::Object> context = object();
98 v8::Local<v8::Object> process = env()->process_object();
100 v8::TryCatch try_catch;
101 try_catch.SetVerbose(true);
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);
107 if (try_catch.HasCaught())
108 return v8::Undefined(env()->isolate());
111 v8::Local<v8::Value> ret = cb->Call(context, argc, argv);
113 if (try_catch.HasCaught()) {
114 return Undefined(env()->isolate());
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);
121 if (try_catch.HasCaught())
122 return v8::Undefined(env()->isolate());
125 Environment::TickInfo* tick_info = env()->tick_info();
127 if (tick_info->in_tick()) {
131 if (tick_info->length() == 0) {
132 tick_info->set_index(0);
136 tick_info->set_in_tick(true);
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);
143 tick_info->set_in_tick(false);
145 if (try_catch.HasCaught()) {
146 tick_info->set_last_threw(true);
147 return Undefined(env()->isolate());
154 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
155 const v8::Handle<v8::String> symbol,
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());
162 return MakeCallback(cb, argc, argv);
166 inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
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());
174 return MakeCallback(cb, argc, argv);
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());
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);
189 env->async_listener_push_function()->Call(handle, 1, &listener);
191 Type* wrap = static_cast<Type*>(
192 handle->GetAlignedPointerFromInternalField(0));
193 assert(wrap != NULL);
194 wrap->set_flag(ASYNC_LISTENERS);
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());
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);
209 v8::Local<v8::Value> ret =
210 env->async_listener_strip_function()->Call(handle, 1, &listener);
212 if (ret->IsFalse()) {
213 Type* wrap = static_cast<Type*>(
214 handle->GetAlignedPointerFromInternalField(0));
215 assert(wrap != NULL);
216 wrap->remove_flag(ASYNC_LISTENERS);
222 #endif // SRC_ASYNC_WRAP_INL_H_