GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/callback_scope.cc Lines: 58 59 98.3 %
Date: 2019-01-07 12:15:22 Branches: 49 62 79.0 %

Line Branch Exec Source
1
#include "node.h"
2
#include "async_wrap-inl.h"
3
#include "env-inl.h"
4
#include "v8.h"
5
6
namespace node {
7
8
using v8::Function;
9
using v8::HandleScope;
10
using v8::Isolate;
11
using v8::Local;
12
using v8::Object;
13
14
using AsyncHooks = Environment::AsyncHooks;
15
16
630
CallbackScope::CallbackScope(Isolate* isolate,
17
                             Local<Object> object,
18
                             async_context asyncContext)
19
  : private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
20
                                       object,
21
630
                                       asyncContext)),
22
1260
    try_catch_(isolate) {
23
630
  try_catch_.SetVerbose(true);
24
630
}
25
26
1258
CallbackScope::~CallbackScope() {
27
629
  if (try_catch_.HasCaught())
28
1
    private_->MarkAsFailed();
29
629
  delete private_;
30
629
}
31
32
1537
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap)
33
    : InternalCallbackScope(async_wrap->env(),
34
                            async_wrap->object(),
35
1537
                            { async_wrap->get_async_id(),
36
1537
                              async_wrap->get_trigger_async_id() }) {}
37
38
737466
InternalCallbackScope::InternalCallbackScope(Environment* env,
39
                                             Local<Object> object,
40
                                             const async_context& asyncContext,
41
                                             ResourceExpectation expect)
42
  : env_(env),
43
    async_context_(asyncContext),
44
    object_(object),
45
737466
    callback_scope_(env) {
46

1473202
  CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty());
47
737549
  CHECK_NOT_NULL(env);
48
49
737549
  if (!env->can_call_into_js()) {
50
402
    failed_ = true;
51
738005
    return;
52
  }
53
54
737201
  HandleScope handle_scope(env->isolate());
55
  // If you hit this assertion, you forgot to enter the v8::Context first.
56
737202
  CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
57
58
737201
  if (asyncContext.async_id != 0) {
59
    // No need to check a return value because the application will exit if
60
    // an exception occurs.
61
673379
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
62
  }
63
64
  env->async_hooks()->push_async_ids(async_context_.async_id,
65
737202
                               async_context_.trigger_async_id);
66
737197
  pushed_ids_ = true;
67
}
68
69
1474954
InternalCallbackScope::~InternalCallbackScope() {
70
737477
  Close();
71
737477
}
72
73
1468206
void InternalCallbackScope::Close() {
74
2553534
  if (closed_) return;
75
737557
  closed_ = true;
76
737557
  HandleScope handle_scope(env_->isolate());
77
78
737556
  if (!env_->can_call_into_js()) return;
79


737141
  if (failed_ && !env_->is_main_thread() && env_->is_stopping_worker()) {
80
    env_->async_hooks()->clear_async_id_stack();
81
  }
82
83
737141
  if (pushed_ids_)
84
737141
    env_->async_hooks()->pop_async_id(async_context_.async_id);
85
86
737140
  if (failed_) return;
87
88
737084
  if (async_context_.async_id != 0) {
89
673309
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
90
  }
91
92
737085
  if (env_->makecallback_depth() > 1) {
93
77049
    return;
94
  }
95
96
660036
  Environment::TickInfo* tick_info = env_->tick_info();
97
98
660036
  if (!env_->can_call_into_js()) return;
99
660035
  if (!tick_info->has_tick_scheduled()) {
100
277318
    env_->isolate()->RunMicrotasks();
101
  }
102
103
  // Make sure the stack unwound properly. If there are nested MakeCallback's
104
  // then it should return early and not reach this code.
105
660029
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
106
37073
    CHECK_EQ(env_->execution_async_id(), 0);
107
37073
    CHECK_EQ(env_->trigger_async_id(), 0);
108
  }
109
110

660029
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
111
277158
    return;
112
  }
113
114
382871
  Local<Object> process = env_->process_object();
115
116
382871
  if (!env_->can_call_into_js()) return;
117
118
382872
  Local<Function> tick_callback = env_->tick_callback_function();
119
120
  // The tick is triggered before JS land calls SetTickCallback
121
  // to initializes the tick callback during bootstrap.
122
382872
  CHECK(!tick_callback.IsEmpty());
123
124
1148542
  if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
125
1049
    failed_ = true;
126
382798
  }
127
}
128
129
}  // namespace node