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: 81 83 97.6 %
Date: 2021-02-11 04:11:15 Branches: 32 42 76.2 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node.h"
3
#include "node_errors.h"
4
#include "node_external_reference.h"
5
#include "node_internals.h"
6
#include "node_process.h"
7
#include "util-inl.h"
8
#include "v8.h"
9
10
#include <atomic>
11
12
namespace node {
13
14
using errors::TryCatchScope;
15
using v8::Context;
16
using v8::Function;
17
using v8::FunctionCallbackInfo;
18
using v8::Isolate;
19
using v8::kPromiseHandlerAddedAfterReject;
20
using v8::kPromiseRejectAfterResolved;
21
using v8::kPromiseRejectWithNoHandler;
22
using v8::kPromiseResolveAfterResolved;
23
using v8::Local;
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
23826
void PromiseRejectCallback(PromiseRejectMessage message) {
32
  static std::atomic<uint64_t> unhandledRejections{0};
33
  static std::atomic<uint64_t> rejectionsHandledAfter{0};
34
35
23826
  Local<Promise> promise = message.GetPromise();
36
23826
  Isolate* isolate = promise->GetIsolate();
37
23826
  PromiseRejectEvent event = message.GetEvent();
38
39
23826
  Environment* env = Environment::GetCurrent(isolate);
40
41

23827
  if (env == nullptr || !env->can_call_into_js()) return;
42
43
23825
  Local<Function> callback = env->promise_reject_callback();
44
  // The promise is rejected before JS land calls SetPromiseRejectCallback
45
  // to initializes the promise reject callback during bootstrap.
46
23825
  CHECK(!callback.IsEmpty());
47
48
  Local<Value> value;
49
23825
  Local<Value> type = Number::New(env->isolate(), event);
50
51
23825
  if (event == kPromiseRejectWithNoHandler) {
52
8569
    value = message.GetValue();
53
8569
    unhandledRejections++;
54
8569
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
55
                  "rejections",
56
                  "unhandled", unhandledRejections,
57
142
                  "handledAfter", rejectionsHandledAfter);
58

32536
  } else if (event == kPromiseHandlerAddedAfterReject) {
59
15744
    value = Undefined(isolate);
60
7175
    rejectionsHandledAfter++;
61
4
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
62
7179
                  "rejections",
63
                  "unhandled", unhandledRejections,
64
123
                  "handledAfter", rejectionsHandledAfter);
65

22554
  } else if (event == kPromiseResolveAfterResolved) {
66
7246
    value = message.GetValue();
67
8010
  } else if (event == kPromiseRejectAfterResolved) {
68
8012
    value = message.GetValue();
69
2
  } else {
70
    return;
71
  }
72
73
23825
  if (value.IsEmpty()) {
74
    value = Undefined(isolate);
75
  }
76
77
47650
  Local<Value> args[] = { type, promise, value };
78
79
  // V8 does not expect this callback to have a scheduled exceptions once it
80
  // returns, so we print them out in a best effort to do something about it
81
  // without failing silently and without crashing the process.
82
47650
  TryCatchScope try_catch(env);
83
47650
  USE(callback->Call(
84
119125
      env->context(), Undefined(isolate), arraysize(args), args));
85

23825
  if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
86
394
    fprintf(stderr, "Exception in PromiseRejectCallback:\n");
87
394
    PrintCaughtException(isolate, env->context(), try_catch);
88
  }
89
}
90
namespace task_queue {
91
92
315
static void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
93
315
  Environment* env = Environment::GetCurrent(args);
94
315
  Isolate* isolate = env->isolate();
95
96
630
  CHECK(args[0]->IsFunction());
97
98
945
  isolate->GetCurrentContext()->GetMicrotaskQueue()
99
945
      ->EnqueueMicrotask(isolate, args[0].As<Function>());
100
315
}
101
102
445383
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
103
445383
  Environment* env = Environment::GetCurrent(args);
104
890766
  env->context()->GetMicrotaskQueue()->PerformCheckpoint(env->isolate());
105
445380
}
106
107
444
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {
108
444
  Environment* env = Environment::GetCurrent(args);
109
888
  CHECK(args[0]->IsFunction());
110
888
  env->set_tick_callback_function(args[0].As<Function>());
111
444
}
112
113
444
static void SetPromiseRejectCallback(
114
    const FunctionCallbackInfo<Value>& args) {
115
444
  Environment* env = Environment::GetCurrent(args);
116
117
888
  CHECK(args[0]->IsFunction());
118
888
  env->set_promise_reject_callback(args[0].As<Function>());
119
444
}
120
121
444
static void Initialize(Local<Object> target,
122
                       Local<Value> unused,
123
                       Local<Context> context,
124
                       void* priv) {
125
444
  Environment* env = Environment::GetCurrent(context);
126
444
  Isolate* isolate = env->isolate();
127
128
444
  env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
129
444
  env->SetMethod(target, "setTickCallback", SetTickCallback);
130
444
  env->SetMethod(target, "runMicrotasks", RunMicrotasks);
131
888
  target->Set(env->context(),
132
              FIXED_ONE_BYTE_STRING(isolate, "tickInfo"),
133
2220
              env->tick_info()->fields().GetJSArray()).Check();
134
135
444
  Local<Object> events = Object::New(isolate);
136
888
  NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler);
137
444
  NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject);
138
2220
  NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved);
139
2220
  NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved);
140
2664
141
3108
  target->Set(env->context(),
142
1332
              FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"),
143
2664
              events).Check();
144
  env->SetMethod(target,
145
                 "setPromiseRejectCallback",
146
444
                 SetPromiseRejectCallback);
147
444
}
148
149
4621
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
150
4621
  registry->Register(EnqueueMicrotask);
151
4621
  registry->Register(SetTickCallback);
152
4621
  registry->Register(RunMicrotasks);
153
4621
  registry->Register(SetPromiseRejectCallback);
154
4621
}
155
156
}  // namespace task_queue
157
}  // namespace node
158
159
4691
NODE_MODULE_CONTEXT_AWARE_INTERNAL(task_queue, node::task_queue::Initialize)
160

18718
NODE_MODULE_EXTERNAL_REFERENCE(task_queue,
161
                               node::task_queue::RegisterExternalReferences)