GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_task_queue.cc Lines: 69 71 97.2 %
Date: 2019-08-17 22:35:23 Branches: 34 42 81.0 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node.h"
3
#include "node_errors.h"
4
#include "node_internals.h"
5
#include "node_process.h"
6
#include "util-inl.h"
7
#include "v8.h"
8
9
#include <atomic>
10
11
namespace node {
12
13
using v8::Array;
14
using v8::Context;
15
using v8::Function;
16
using v8::FunctionCallbackInfo;
17
using v8::Isolate;
18
using v8::kPromiseHandlerAddedAfterReject;
19
using v8::kPromiseRejectAfterResolved;
20
using v8::kPromiseRejectWithNoHandler;
21
using v8::kPromiseResolveAfterResolved;
22
using v8::Local;
23
using v8::Message;
24
using v8::Number;
25
using v8::Object;
26
using v8::Promise;
27
using v8::PromiseRejectEvent;
28
using v8::PromiseRejectMessage;
29
using v8::Value;
30
31
namespace task_queue {
32
33
80
static void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
34
80
  Environment* env = Environment::GetCurrent(args);
35
80
  Isolate* isolate = env->isolate();
36
37
160
  CHECK(args[0]->IsFunction());
38
39
160
  isolate->EnqueueMicrotask(args[0].As<Function>());
40
80
}
41
42
// Should be in sync with runNextTicks in internal/process/task_queues.js
43
4902
bool RunNextTicksNative(Environment* env) {
44
4902
  TickInfo* tick_info = env->tick_info();
45

4902
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn())
46
1574
    env->isolate()->RunMicrotasks();
47

4901
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn())
48
1559
    return true;
49
50
3342
  Local<Function> callback = env->tick_callback_function();
51
3342
  CHECK(!callback.IsEmpty());
52
13337
  return !callback->Call(env->context(), env->process_object(), 0, nullptr)
53
6653
              .IsEmpty();
54
}
55
56
646875
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
57
646875
  args.GetIsolate()->RunMicrotasks();
58
646861
}
59
60
5152
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {
61
5152
  Environment* env = Environment::GetCurrent(args);
62
10304
  CHECK(args[0]->IsFunction());
63
10304
  env->set_tick_callback_function(args[0].As<Function>());
64
5152
}
65
66
2241
void PromiseRejectCallback(PromiseRejectMessage message) {
67
  static std::atomic<uint64_t> unhandledRejections{0};
68
  static std::atomic<uint64_t> rejectionsHandledAfter{0};
69
70
2241
  Local<Promise> promise = message.GetPromise();
71
2241
  Isolate* isolate = promise->GetIsolate();
72
2241
  PromiseRejectEvent event = message.GetEvent();
73
74
2241
  Environment* env = Environment::GetCurrent(isolate);
75
76
2241
  if (env == nullptr) return;
77
78
2241
  Local<Function> callback = env->promise_reject_callback();
79
  // The promise is rejected before JS land calls SetPromiseRejectCallback
80
  // to initializes the promise reject callback during bootstrap.
81
2241
  CHECK(!callback.IsEmpty());
82
83
  Local<Value> value;
84
2241
  Local<Value> type = Number::New(env->isolate(), event);
85
86
2241
  if (event == kPromiseRejectWithNoHandler) {
87
1831
    value = message.GetValue();
88
1831
    unhandledRejections++;
89

3662
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
90
                  "rejections",
91
                  "unhandled", unhandledRejections,
92
                  "handledAfter", rejectionsHandledAfter);
93
410
  } else if (event == kPromiseHandlerAddedAfterReject) {
94
347
    value = Undefined(isolate);
95
347
    rejectionsHandledAfter++;
96

694
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
97
                  "rejections",
98
                  "unhandled", unhandledRejections,
99
                  "handledAfter", rejectionsHandledAfter);
100
63
  } else if (event == kPromiseResolveAfterResolved) {
101
60
    value = message.GetValue();
102
3
  } else if (event == kPromiseRejectAfterResolved) {
103
3
    value = message.GetValue();
104
  } else {
105
    return;
106
  }
107
108
2241
  if (value.IsEmpty()) {
109
    value = Undefined(isolate);
110
  }
111
112
4482
  Local<Value> args[] = { type, promise, value };
113
  USE(callback->Call(
114
6723
      env->context(), Undefined(isolate), arraysize(args), args));
115
}
116
117
5152
static void SetPromiseRejectCallback(
118
    const FunctionCallbackInfo<Value>& args) {
119
5152
  Environment* env = Environment::GetCurrent(args);
120
121
10304
  CHECK(args[0]->IsFunction());
122
10304
  env->set_promise_reject_callback(args[0].As<Function>());
123
5152
}
124
125
5154
static void Initialize(Local<Object> target,
126
                       Local<Value> unused,
127
                       Local<Context> context,
128
                       void* priv) {
129
5154
  Environment* env = Environment::GetCurrent(context);
130
5154
  Isolate* isolate = env->isolate();
131
132
5154
  env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
133
5154
  env->SetMethod(target, "setTickCallback", SetTickCallback);
134
5154
  env->SetMethod(target, "runMicrotasks", RunMicrotasks);
135
  target->Set(env->context(),
136
              FIXED_ONE_BYTE_STRING(isolate, "tickInfo"),
137
25770
              env->tick_info()->fields().GetJSArray()).Check();
138
139
5154
  Local<Object> events = Object::New(isolate);
140
20616
  NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler);
141
20616
  NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject);
142
20616
  NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved);
143
20616
  NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved);
144
145
  target->Set(env->context(),
146
              FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"),
147
20616
              events).Check();
148
  env->SetMethod(target,
149
                 "setPromiseRejectCallback",
150
5154
                 SetPromiseRejectCallback);
151
5154
}
152
153
}  // namespace task_queue
154
}  // namespace node
155
156
5015
NODE_MODULE_CONTEXT_AWARE_INTERNAL(task_queue, node::task_queue::Initialize)