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 #include "async-wrap.h"
23 #include "async-wrap-inl.h"
33 using v8::FunctionCallbackInfo;
35 using v8::HandleScope;
42 using v8::kExternalUint32Array;
46 static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
47 Environment* env = Environment::GetCurrent(args.GetIsolate());
49 CHECK(args[0]->IsObject());
50 CHECK(args[1]->IsFunction());
51 CHECK(args[2]->IsFunction());
52 CHECK(args[3]->IsFunction());
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(),
67 async_hooks->fields_count());
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>());
75 static void Initialize(Handle<Object> target,
77 Handle<Context> context) {
78 Environment* env = Environment::GetCurrent(context);
79 Isolate* isolate = env->isolate();
80 HandleScope scope(isolate);
82 NODE_SET_METHOD(target, "setupHooks", SetupHooks);
84 Local<Object> async_providers = Object::New(isolate);
86 async_providers->Set(FIXED_ONE_BYTE_STRING(isolate, #PROVIDER), \
87 Integer::New(isolate, AsyncWrap::PROVIDER_ ## PROVIDER));
88 NODE_ASYNC_PROVIDER_TYPES(V)
90 target->Set(FIXED_ONE_BYTE_STRING(isolate, "Providers"), async_providers);
94 Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb,
96 Handle<Value>* argv) {
97 CHECK(env()->context() == env()->isolate()->GetCurrentContext());
99 Local<Object> context = object();
100 Local<Object> process = env()->process_object();
101 Local<Object> domain;
102 bool has_domain = false;
104 if (env()->using_domains()) {
105 Local<Value> domain_v = context->Get(env()->domain_string());
106 has_domain = domain_v->IsObject();
108 domain = domain_v.As<Object>();
109 if (domain->Get(env()->disposed_string())->IsTrue())
110 return Undefined(env()->isolate());
115 try_catch.SetVerbose(true);
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());
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);
134 Local<Value> ret = cb->Call(context, argc, argv);
136 if (try_catch.HasCaught()) {
137 return Undefined(env()->isolate());
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);
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());
157 Environment::TickInfo* tick_info = env()->tick_info();
159 if (tick_info->in_tick()) {
163 if (tick_info->length() == 0) {
164 env()->isolate()->RunMicrotasks();
167 if (tick_info->length() == 0) {
168 tick_info->set_index(0);
172 tick_info->set_in_tick(true);
174 env()->tick_callback_function()->Call(process, 0, nullptr);
176 tick_info->set_in_tick(false);
178 if (try_catch.HasCaught()) {
179 tick_info->set_last_threw(true);
180 return Undefined(env()->isolate());
188 NODE_MODULE_CONTEXT_AWARE_BUILTIN(async_wrap, node::Initialize)