3526512c6e008933d1fc9bba757e0e3a608bbd60
[platform/upstream/nodejs.git] / src / async-wrap.cc
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 "async-wrap.h"
23 #include "async-wrap-inl.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "util.h"
27 #include "util-inl.h"
28
29 #include "v8.h"
30
31 using v8::Context;
32 using v8::Function;
33 using v8::FunctionCallbackInfo;
34 using v8::Handle;
35 using v8::HandleScope;
36 using v8::Integer;
37 using v8::Isolate;
38 using v8::Local;
39 using v8::Object;
40 using v8::TryCatch;
41 using v8::Value;
42 using v8::kExternalUint32Array;
43
44 namespace node {
45
46 static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
47   Environment* env = Environment::GetCurrent(args.GetIsolate());
48
49   CHECK(args[0]->IsObject());
50   CHECK(args[1]->IsFunction());
51   CHECK(args[2]->IsFunction());
52   CHECK(args[3]->IsFunction());
53
54   // Attach Fields enum from Environment::AsyncHooks.
55   // Flags attached to this object are:
56   // - kCallInitHook (0): Tells the AsyncWrap constructor whether it should
57   //   make a call to the init JS callback. This is disabled by default, so
58   //   even after setting the callbacks the flag will have to be set to
59   //   non-zero to have those callbacks called. This only affects the init
60   //   callback. If the init callback was called, then the pre/post callbacks
61   //   will automatically be called.
62   Local<Object> async_hooks_obj = args[0].As<Object>();
63   Environment::AsyncHooks* async_hooks = env->async_hooks();
64   async_hooks_obj->SetIndexedPropertiesToExternalArrayData(
65       async_hooks->fields(),
66       kExternalUint32Array,
67       async_hooks->fields_count());
68
69   env->set_async_hooks_init_function(args[1].As<Function>());
70   env->set_async_hooks_pre_function(args[2].As<Function>());
71   env->set_async_hooks_post_function(args[3].As<Function>());
72 }
73
74
75 static void Initialize(Handle<Object> target,
76                 Handle<Value> unused,
77                 Handle<Context> context) {
78   Environment* env = Environment::GetCurrent(context);
79   Isolate* isolate = env->isolate();
80   HandleScope scope(isolate);
81
82   NODE_SET_METHOD(target, "setupHooks", SetupHooks);
83
84   Local<Object> async_providers = Object::New(isolate);
85 #define V(PROVIDER)                                                           \
86   async_providers->Set(FIXED_ONE_BYTE_STRING(isolate, #PROVIDER),             \
87       Integer::New(isolate, AsyncWrap::PROVIDER_ ## PROVIDER));
88   NODE_ASYNC_PROVIDER_TYPES(V)
89 #undef V
90   target->Set(FIXED_ONE_BYTE_STRING(isolate, "Providers"), async_providers);
91 }
92
93
94 Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb,
95                                       int argc,
96                                       Handle<Value>* argv) {
97   CHECK(env()->context() == env()->isolate()->GetCurrentContext());
98
99   Local<Object> context = object();
100   Local<Object> process = env()->process_object();
101   Local<Object> domain;
102   bool has_domain = false;
103
104   if (env()->using_domains()) {
105     Local<Value> domain_v = context->Get(env()->domain_string());
106     has_domain = domain_v->IsObject();
107     if (has_domain) {
108       domain = domain_v.As<Object>();
109       if (domain->Get(env()->disposed_string())->IsTrue())
110         return Undefined(env()->isolate());
111     }
112   }
113
114   TryCatch try_catch;
115   try_catch.SetVerbose(true);
116
117   if (has_domain) {
118     Local<Value> enter_v = domain->Get(env()->enter_string());
119     if (enter_v->IsFunction()) {
120       enter_v.As<Function>()->Call(domain, 0, nullptr);
121       if (try_catch.HasCaught())
122         return Undefined(env()->isolate());
123     }
124   }
125
126   if (has_async_queue_) {
127     try_catch.SetVerbose(false);
128     env()->async_hooks_pre_function()->Call(context, 0, nullptr);
129     if (try_catch.HasCaught())
130       FatalError("node::AsyncWrap::MakeCallback", "pre hook threw");
131     try_catch.SetVerbose(true);
132   }
133
134   Local<Value> ret = cb->Call(context, argc, argv);
135
136   if (try_catch.HasCaught()) {
137     return Undefined(env()->isolate());
138   }
139
140   if (has_async_queue_) {
141     try_catch.SetVerbose(false);
142     env()->async_hooks_post_function()->Call(context, 0, nullptr);
143     if (try_catch.HasCaught())
144       FatalError("node::AsyncWrap::MakeCallback", "post hook threw");
145     try_catch.SetVerbose(true);
146   }
147
148   if (has_domain) {
149     Local<Value> exit_v = domain->Get(env()->exit_string());
150     if (exit_v->IsFunction()) {
151       exit_v.As<Function>()->Call(domain, 0, nullptr);
152       if (try_catch.HasCaught())
153         return Undefined(env()->isolate());
154     }
155   }
156
157   Environment::TickInfo* tick_info = env()->tick_info();
158
159   if (tick_info->in_tick()) {
160     return ret;
161   }
162
163   if (tick_info->length() == 0) {
164     env()->isolate()->RunMicrotasks();
165   }
166
167   if (tick_info->length() == 0) {
168     tick_info->set_index(0);
169     return ret;
170   }
171
172   tick_info->set_in_tick(true);
173
174   env()->tick_callback_function()->Call(process, 0, nullptr);
175
176   tick_info->set_in_tick(false);
177
178   if (try_catch.HasCaught()) {
179     tick_info->set_last_threw(true);
180     return Undefined(env()->isolate());
181   }
182
183   return ret;
184 }
185
186 }  // namespace node
187
188 NODE_MODULE_CONTEXT_AWARE_BUILTIN(async_wrap, node::Initialize)