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-07-28 22:34:34 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
4715
bool RunNextTicksNative(Environment* env) {
44
4715
  TickInfo* tick_info = env->tick_info();
45

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

4714
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn())
48
1551
    return true;
49
50
3163
  Local<Function> callback = env->tick_callback_function();
51
3163
  CHECK(!callback.IsEmpty());
52
12626
  return !callback->Call(env->context(), env->process_object(), 0, nullptr)
53
6300
              .IsEmpty();
54
}
55
56
550568
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
57
550568
  args.GetIsolate()->RunMicrotasks();
58
550558
}
59
60
4960
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {
61
4960
  Environment* env = Environment::GetCurrent(args);
62
9920
  CHECK(args[0]->IsFunction());
63
9920
  env->set_tick_callback_function(args[0].As<Function>());
64
4960
}
65
66
2171
void PromiseRejectCallback(PromiseRejectMessage message) {
67
  static std::atomic<uint64_t> unhandledRejections{0};
68
  static std::atomic<uint64_t> rejectionsHandledAfter{0};
69
70
2171
  Local<Promise> promise = message.GetPromise();
71
2171
  Isolate* isolate = promise->GetIsolate();
72
2171
  PromiseRejectEvent event = message.GetEvent();
73
74
2171
  Environment* env = Environment::GetCurrent(isolate);
75
76
2171
  if (env == nullptr) return;
77
78
2171
  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
2171
  CHECK(!callback.IsEmpty());
82
83
  Local<Value> value;
84
2171
  Local<Value> type = Number::New(env->isolate(), event);
85
86
2171
  if (event == kPromiseRejectWithNoHandler) {
87
1795
    value = message.GetValue();
88
1795
    unhandledRejections++;
89

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

626
    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
2171
  if (value.IsEmpty()) {
109
    value = Undefined(isolate);
110
  }
111
112
4342
  Local<Value> args[] = { type, promise, value };
113
  USE(callback->Call(
114
6513
      env->context(), Undefined(isolate), arraysize(args), args));
115
}
116
117
4960
static void SetPromiseRejectCallback(
118
    const FunctionCallbackInfo<Value>& args) {
119
4960
  Environment* env = Environment::GetCurrent(args);
120
121
9920
  CHECK(args[0]->IsFunction());
122
9920
  env->set_promise_reject_callback(args[0].As<Function>());
123
4960
}
124
125
4961
static void Initialize(Local<Object> target,
126
                       Local<Value> unused,
127
                       Local<Context> context,
128
                       void* priv) {
129
4961
  Environment* env = Environment::GetCurrent(context);
130
4961
  Isolate* isolate = env->isolate();
131
132
4961
  env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
133
4961
  env->SetMethod(target, "setTickCallback", SetTickCallback);
134
4961
  env->SetMethod(target, "runMicrotasks", RunMicrotasks);
135
  target->Set(env->context(),
136
              FIXED_ONE_BYTE_STRING(isolate, "tickInfo"),
137
24805
              env->tick_info()->fields().GetJSArray()).Check();
138
139
4961
  Local<Object> events = Object::New(isolate);
140
19844
  NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler);
141
19844
  NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject);
142
19844
  NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved);
143
19844
  NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved);
144
145
  target->Set(env->context(),
146
              FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"),
147
19844
              events).Check();
148
  env->SetMethod(target,
149
                 "setPromiseRejectCallback",
150
4961
                 SetPromiseRejectCallback);
151
4961
}
152
153
}  // namespace task_queue
154
}  // namespace node
155
156
4827
NODE_MODULE_CONTEXT_AWARE_INTERNAL(task_queue, node::task_queue::Initialize)