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: 48 54 88.9 %
Date: 2019-02-01 22:03:38 Branches: 12 26 46.2 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node.h"
3
#include "node_internals.h"
4
#include "v8.h"
5
6
#include <atomic>
7
8
namespace node {
9
10
using v8::Array;
11
using v8::Context;
12
using v8::Function;
13
using v8::FunctionCallbackInfo;
14
using v8::Isolate;
15
using v8::kPromiseHandlerAddedAfterReject;
16
using v8::kPromiseRejectAfterResolved;
17
using v8::kPromiseRejectWithNoHandler;
18
using v8::kPromiseResolveAfterResolved;
19
using v8::Local;
20
using v8::Number;
21
using v8::Object;
22
using v8::Promise;
23
using v8::PromiseRejectEvent;
24
using v8::PromiseRejectMessage;
25
using v8::Value;
26
27
namespace task_queue {
28
29
2444
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
30
2444
  args.GetIsolate()->RunMicrotasks();
31
2444
}
32
33
164
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {
34
164
  Environment* env = Environment::GetCurrent(args);
35
328
  CHECK(args[0]->IsFunction());
36
328
  env->set_tick_callback_function(args[0].As<Function>());
37
164
}
38
39
2
void PromiseRejectCallback(PromiseRejectMessage message) {
40
  static std::atomic<uint64_t> unhandledRejections{0};
41
  static std::atomic<uint64_t> rejectionsHandledAfter{0};
42
43
2
  Local<Promise> promise = message.GetPromise();
44
2
  Isolate* isolate = promise->GetIsolate();
45
2
  PromiseRejectEvent event = message.GetEvent();
46
47
2
  Environment* env = Environment::GetCurrent(isolate);
48
49
2
  if (env == nullptr) return;
50
51
2
  Local<Function> callback = env->promise_reject_callback();
52
  // The promise is rejected before JS land calls SetPromiseRejectCallback
53
  // to initializes the promise reject callback during bootstrap.
54
2
  CHECK(!callback.IsEmpty());
55
56
  Local<Value> value;
57
2
  Local<Value> type = Number::New(env->isolate(), event);
58
59
2
  if (event == kPromiseRejectWithNoHandler) {
60
1
    value = message.GetValue();
61
1
    unhandledRejections++;
62

1
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
63
                  "rejections",
64
                  "unhandled", unhandledRejections,
65
                  "handledAfter", rejectionsHandledAfter);
66
1
  } else if (event == kPromiseHandlerAddedAfterReject) {
67
1
    value = Undefined(isolate);
68
1
    rejectionsHandledAfter++;
69

1
    TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections),
70
                  "rejections",
71
                  "unhandled", unhandledRejections,
72
                  "handledAfter", rejectionsHandledAfter);
73
  } else if (event == kPromiseResolveAfterResolved) {
74
    value = message.GetValue();
75
  } else if (event == kPromiseRejectAfterResolved) {
76
    value = message.GetValue();
77
  } else {
78
    return;
79
  }
80
81
2
  if (value.IsEmpty()) {
82
    value = Undefined(isolate);
83
  }
84
85
4
  Local<Value> args[] = { type, promise, value };
86
  USE(callback->Call(
87
6
      env->context(), Undefined(isolate), arraysize(args), args));
88
}
89
90
164
static void SetPromiseRejectCallback(
91
    const FunctionCallbackInfo<Value>& args) {
92
164
  Environment* env = Environment::GetCurrent(args);
93
94
328
  CHECK(args[0]->IsFunction());
95
328
  env->set_promise_reject_callback(args[0].As<Function>());
96
164
}
97
98
164
static void Initialize(Local<Object> target,
99
                       Local<Value> unused,
100
                       Local<Context> context,
101
                       void* priv) {
102
164
  Environment* env = Environment::GetCurrent(context);
103
164
  Isolate* isolate = env->isolate();
104
105
164
  env->SetMethod(target, "setTickCallback", SetTickCallback);
106
164
  env->SetMethod(target, "runMicrotasks", RunMicrotasks);
107
  target->Set(env->context(),
108
              FIXED_ONE_BYTE_STRING(isolate, "tickInfo"),
109
820
              env->tick_info()->fields().GetJSArray()).FromJust();
110
111
164
  Local<Object> events = Object::New(isolate);
112
656
  NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler);
113
656
  NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject);
114
656
  NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved);
115
656
  NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved);
116
117
  target->Set(env->context(),
118
              FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"),
119
656
              events).FromJust();
120
  env->SetMethod(target,
121
                 "setPromiseRejectCallback",
122
164
                 SetPromiseRejectCallback);
123
164
}
124
125
}  // namespace task_queue
126
}  // namespace node
127
128
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(task_queue, node::task_queue::Initialize)