GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/async-wrap.cc Lines: 124 133 93.2 %
Date: 2016-11-30 Branches: 63 80 78.8 %

Line Branch Exec Source
1
#include "async-wrap.h"
2
#include "async-wrap-inl.h"
3
#include "env.h"
4
#include "env-inl.h"
5
#include "util.h"
6
#include "util-inl.h"
7
8
#include "v8.h"
9
#include "v8-profiler.h"
10
11
using v8::Boolean;
12
using v8::Context;
13
using v8::Function;
14
using v8::FunctionCallbackInfo;
15
using v8::HandleScope;
16
using v8::HeapProfiler;
17
using v8::Integer;
18
using v8::Isolate;
19
using v8::Local;
20
using v8::MaybeLocal;
21
using v8::Number;
22
using v8::Object;
23
using v8::RetainedObjectInfo;
24
using v8::TryCatch;
25
using v8::Value;
26
27
namespace node {
28
29
static const char* const provider_names[] = {
30
#define V(PROVIDER)                                                           \
31
  #PROVIDER,
32
  NODE_ASYNC_PROVIDER_TYPES(V)
33
#undef V
34
};
35
36
37
2
class RetainedAsyncInfo: public RetainedObjectInfo {
38
 public:
39
  explicit RetainedAsyncInfo(uint16_t class_id, AsyncWrap* wrap);
40
41
  void Dispose() override;
42
  bool IsEquivalent(RetainedObjectInfo* other) override;
43
  intptr_t GetHash() override;
44
  const char* GetLabel() override;
45
  intptr_t GetSizeInBytes() override;
46
47
 private:
48
  const char* label_;
49
  const AsyncWrap* wrap_;
50
  const int length_;
51
};
52
53
54
RetainedAsyncInfo::RetainedAsyncInfo(uint16_t class_id, AsyncWrap* wrap)
55
1
    : label_(provider_names[class_id - NODE_ASYNC_ID_OFFSET]),
56
      wrap_(wrap),
57
3
      length_(wrap->self_size()) {
58
}
59
60
61
1
void RetainedAsyncInfo::Dispose() {
62
1
  delete this;
63
1
}
64
65
66
bool RetainedAsyncInfo::IsEquivalent(RetainedObjectInfo* other) {
67
  return label_ == other->GetLabel() &&
68
          wrap_ == static_cast<RetainedAsyncInfo*>(other)->wrap_;
69
}
70
71
72
2
intptr_t RetainedAsyncInfo::GetHash() {
73
2
  return reinterpret_cast<intptr_t>(wrap_);
74
}
75
76
77
3
const char* RetainedAsyncInfo::GetLabel() {
78
3
  return label_;
79
}
80
81
82
1
intptr_t RetainedAsyncInfo::GetSizeInBytes() {
83
1
  return length_;
84
}
85
86
87
1
RetainedObjectInfo* WrapperInfo(uint16_t class_id, Local<Value> wrapper) {
88
  // No class_id should be the provider type of NONE.
89
1
  CHECK_NE(NODE_ASYNC_ID_OFFSET, class_id);
90
1
  CHECK(wrapper->IsObject());
91
1
  CHECK(!wrapper.IsEmpty());
92
93
1
  Local<Object> object = wrapper.As<Object>();
94
1
  CHECK_GT(object->InternalFieldCount(), 0);
95
96
1
  AsyncWrap* wrap = Unwrap<AsyncWrap>(object);
97
1
  CHECK_NE(nullptr, wrap);
98
99
2
  return new RetainedAsyncInfo(class_id, wrap);
100
}
101
102
103
// end RetainedAsyncInfo
104
105
106
12
static void EnableHooksJS(const FunctionCallbackInfo<Value>& args) {
107
12
  Environment* env = Environment::GetCurrent(args);
108
12
  Local<Function> init_fn = env->async_hooks_init_function();
109

12
  if (init_fn.IsEmpty() || !init_fn->IsFunction())
110
1
    return env->ThrowTypeError("init callback is not assigned to a function");
111
22
  env->async_hooks()->set_enable_callbacks(1);
112
}
113
114
115
2
static void DisableHooksJS(const FunctionCallbackInfo<Value>& args) {
116
2
  Environment* env = Environment::GetCurrent(args);
117
4
  env->async_hooks()->set_enable_callbacks(0);
118
2
}
119
120
121
14
static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
122
14
  Environment* env = Environment::GetCurrent(args);
123
124
28
  if (env->async_hooks()->callbacks_enabled())
125
    return env->ThrowError("hooks should not be set while also enabled");
126
13
  if (!args[0]->IsObject())
127
    return env->ThrowTypeError("first argument must be an object");
128
129
24
  Local<Object> fn_obj = args[0].As<Object>();
130
131
12
  Local<Value> init_v = fn_obj->Get(
132
      env->context(),
133
48
      FIXED_ONE_BYTE_STRING(env->isolate(), "init")).ToLocalChecked();
134
12
  Local<Value> pre_v = fn_obj->Get(
135
      env->context(),
136
48
      FIXED_ONE_BYTE_STRING(env->isolate(), "pre")).ToLocalChecked();
137
12
  Local<Value> post_v = fn_obj->Get(
138
      env->context(),
139
48
      FIXED_ONE_BYTE_STRING(env->isolate(), "post")).ToLocalChecked();
140
12
  Local<Value> destroy_v = fn_obj->Get(
141
      env->context(),
142
48
      FIXED_ONE_BYTE_STRING(env->isolate(), "destroy")).ToLocalChecked();
143
144
12
  if (!init_v->IsFunction())
145
    return env->ThrowTypeError("init callback must be a function");
146
147
11
  env->set_async_hooks_init_function(init_v.As<Function>());
148
149
11
  if (pre_v->IsFunction())
150
5
    env->set_async_hooks_pre_function(pre_v.As<Function>());
151
11
  if (post_v->IsFunction())
152
6
    env->set_async_hooks_post_function(post_v.As<Function>());
153
11
  if (destroy_v->IsFunction())
154
5
    env->set_async_hooks_destroy_function(destroy_v.As<Function>());
155
}
156
157
158
12
static void Initialize(Local<Object> target,
159
                Local<Value> unused,
160
                Local<Context> context) {
161
12
  Environment* env = Environment::GetCurrent(context);
162
12
  Isolate* isolate = env->isolate();
163
24
  HandleScope scope(isolate);
164
165
12
  env->SetMethod(target, "setupHooks", SetupHooks);
166
12
  env->SetMethod(target, "disable", DisableHooksJS);
167
12
  env->SetMethod(target, "enable", EnableHooksJS);
168
169
12
  Local<Object> async_providers = Object::New(isolate);
170
#define V(PROVIDER)                                                           \
171
  async_providers->Set(FIXED_ONE_BYTE_STRING(isolate, #PROVIDER),             \
172
      Integer::New(isolate, AsyncWrap::PROVIDER_ ## PROVIDER));
173
588
  NODE_ASYNC_PROVIDER_TYPES(V)
174
#undef V
175
36
  target->Set(FIXED_ONE_BYTE_STRING(isolate, "Providers"), async_providers);
176
177
12
  env->set_async_hooks_init_function(Local<Function>());
178
12
  env->set_async_hooks_pre_function(Local<Function>());
179
12
  env->set_async_hooks_post_function(Local<Function>());
180
12
  env->set_async_hooks_destroy_function(Local<Function>());
181
12
}
182
183
184
1698
void LoadAsyncWrapperInfo(Environment* env) {
185
1698
  HeapProfiler* heap_profiler = env->isolate()->GetHeapProfiler();
186
#define V(PROVIDER)                                                           \
187
  heap_profiler->SetWrapperClassInfoProvider(                                 \
188
      (NODE_ASYNC_ID_OFFSET + AsyncWrap::PROVIDER_ ## PROVIDER), WrapperInfo);
189
1698
  NODE_ASYNC_PROVIDER_TYPES(V)
190
#undef V
191
1698
}
192
193
194
105993
Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
195
                                     int argc,
196
                                     Local<Value>* argv) {
197
317979
  CHECK(env()->context() == env()->isolate()->GetCurrentContext());
198
199
211986
  Local<Function> pre_fn = env()->async_hooks_pre_function();
200
211986
  Local<Function> post_fn = env()->async_hooks_post_function();
201
211986
  Local<Value> uid = Number::New(env()->isolate(), get_uid());
202
211986
  Local<Object> context = object();
203
105993
  Local<Object> domain;
204
105993
  bool has_domain = false;
205
206
317882
  Environment::AsyncCallbackScope callback_scope(env());
207
208
105993
  if (env()->using_domains()) {
209
3156
    Local<Value> domain_v = context->Get(env()->domain_string());
210
1052
    has_domain = domain_v->IsObject();
211
1052
    if (has_domain) {
212
44
      domain = domain_v.As<Object>();
213
176
      if (domain->Get(env()->disposed_string())->IsTrue())
214
        return Local<Value>();
215
    }
216
  }
217
218
105993
  if (has_domain) {
219
132
    Local<Value> enter_v = domain->Get(env()->enter_string());
220
44
    if (enter_v->IsFunction()) {
221
132
      if (enter_v.As<Function>()->Call(domain, 0, nullptr).IsEmpty()) {
222
        FatalError("node::AsyncWrap::MakeCallback",
223
                   "domain enter callback threw, please report this");
224
      }
225
    }
226
  }
227
228

211986
  if (ran_init_callback() && !pre_fn.IsEmpty()) {
229
9
    TryCatch try_catch(env()->isolate());
230
10
    MaybeLocal<Value> ar = pre_fn->Call(env()->context(), context, 1, &uid);
231
5
    if (ar.IsEmpty()) {
232
1
      ClearFatalExceptionHandlers(env());
233
1
      FatalException(env()->isolate(), try_catch);
234
      return Local<Value>();
235
    }
236
  }
237
238
211984
  Local<Value> ret = cb->Call(context, argc, argv);
239
240

211898
  if (ran_init_callback() && !post_fn.IsEmpty()) {
241
15
    Local<Value> did_throw = Boolean::New(env()->isolate(), ret.IsEmpty());
242
5
    Local<Value> vals[] = { uid, did_throw };
243
9
    TryCatch try_catch(env()->isolate());
244
    MaybeLocal<Value> ar =
245
15
        post_fn->Call(env()->context(), context, arraysize(vals), vals);
246
5
    if (ar.IsEmpty()) {
247
1
      ClearFatalExceptionHandlers(env());
248
1
      FatalException(env()->isolate(), try_catch);
249
      return Local<Value>();
250
    }
251
  }
252
253
105948
  if (ret.IsEmpty()) {
254
46
    return ret;
255
  }
256
257
105902
  if (has_domain) {
258
81
    Local<Value> exit_v = domain->Get(env()->exit_string());
259
27
    if (exit_v->IsFunction()) {
260
81
      if (exit_v.As<Function>()->Call(domain, 0, nullptr).IsEmpty()) {
261
        FatalError("node::AsyncWrap::MakeCallback",
262
                   "domain exit callback threw, please report this");
263
      }
264
    }
265
  }
266
267
105902
  if (callback_scope.in_makecallback()) {
268
33406
    return ret;
269
  }
270
271
144992
  Environment::TickInfo* tick_info = env()->tick_info();
272
273
72496
  if (tick_info->length() == 0) {
274
43567
    env()->isolate()->RunMicrotasks();
275
  }
276
277
144992
  Local<Object> process = env()->process_object();
278
279
72496
  if (tick_info->length() == 0) {
280
43560
    tick_info->set_index(0);
281
43560
    return ret;
282
  }
283
284
86756
  if (env()->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
285
20
    return Local<Value>();
286
  }
287
288
28864
  return ret;
289
}
290
291
}  // namespace node
292
293
1705
NODE_MODULE_CONTEXT_AWARE_BUILTIN(async_wrap, node::Initialize)