GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/callback.cc Lines: 94 107 87.9 %
Date: 2019-07-28 22:34:34 Branches: 72 90 80.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::Context;
9
using v8::EscapableHandleScope;
10
using v8::Function;
11
using v8::HandleScope;
12
using v8::Isolate;
13
using v8::Local;
14
using v8::MaybeLocal;
15
using v8::NewStringType;
16
using v8::Object;
17
using v8::String;
18
using v8::Value;
19
20
647
CallbackScope::CallbackScope(Isolate* isolate,
21
                             Local<Object> object,
22
                             async_context asyncContext)
23
  : private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
24
                                       object,
25
647
                                       asyncContext)),
26
1294
    try_catch_(isolate) {
27
647
  try_catch_.SetVerbose(true);
28
647
}
29
30
1292
CallbackScope::~CallbackScope() {
31
646
  if (try_catch_.HasCaught())
32
1
    private_->MarkAsFailed();
33
646
  delete private_;
34
646
}
35
36
2670
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap)
37
    : InternalCallbackScope(async_wrap->env(),
38
                            async_wrap->object(),
39
2670
                            { async_wrap->get_async_id(),
40
2670
                              async_wrap->get_trigger_async_id() }) {}
41
42
1010154
InternalCallbackScope::InternalCallbackScope(Environment* env,
43
                                             Local<Object> object,
44
                                             const async_context& asyncContext,
45
                                             ResourceExpectation expect)
46
  : env_(env),
47
    async_context_(asyncContext),
48
    object_(object),
49
1010154
    callback_scope_(env) {
50

2013145
  CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty());
51
1010161
  CHECK_NOT_NULL(env);
52
53
1010161
  if (!env->can_call_into_js()) {
54
747
    failed_ = true;
55
1010900
    return;
56
  }
57
58
1009414
  HandleScope handle_scope(env->isolate());
59
  // If you hit this assertion, you forgot to enter the v8::Context first.
60
1009408
  CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
61
62
1009411
  if (asyncContext.async_id != 0) {
63
    // No need to check a return value because the application will exit if
64
    // an exception occurs.
65
932539
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
66
  }
67
68
  env->async_hooks()->push_async_ids(async_context_.async_id,
69
1009413
                               async_context_.trigger_async_id);
70
1009403
  pushed_ids_ = true;
71
}
72
73
2020025
InternalCallbackScope::~InternalCallbackScope() {
74
1010014
  Close();
75
1010014
}
76
77
2003622
void InternalCallbackScope::Close() {
78
3490515
  if (closed_) return;
79
1010095
  closed_ = true;
80
81
1010095
  if (!env_->can_call_into_js()) return;
82


1009318
  if (failed_ && !env_->is_main_thread() && env_->is_stopping()) {
83
    env_->async_hooks()->clear_async_id_stack();
84
  }
85
86
1009317
  if (pushed_ids_)
87
1009317
    env_->async_hooks()->pop_async_id(async_context_.async_id);
88
89
1009320
  if (failed_) return;
90
91
1008754
  if (async_context_.async_id != 0) {
92
932458
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
93
  }
94
95
1008754
  if (env_->async_callback_scope_depth() > 1) {
96
203767
    return;
97
  }
98
99
804987
  TickInfo* tick_info = env_->tick_info();
100
101
804987
  if (!env_->can_call_into_js()) return;
102
804987
  if (!tick_info->has_tick_scheduled()) {
103
288615
    env_->isolate()->RunMicrotasks();
104
  }
105
106
  // Make sure the stack unwound properly. If there are nested MakeCallback's
107
  // then it should return early and not reach this code.
108
804978
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
109
37551
    CHECK_EQ(env_->execution_async_id(), 0);
110
37551
    CHECK_EQ(env_->trigger_async_id(), 0);
111
  }
112
113

804975
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
114
288257
    return;
115
  }
116
117
516718
  HandleScope handle_scope(env_->isolate());
118
516718
  Local<Object> process = env_->process_object();
119
120
516718
  if (!env_->can_call_into_js()) return;
121
122
516718
  Local<Function> tick_callback = env_->tick_callback_function();
123
124
  // The tick is triggered before JS land calls SetTickCallback
125
  // to initializes the tick callback during bootstrap.
126
516718
  CHECK(!tick_callback.IsEmpty());
127
128
1550079
  if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
129
545
    failed_ = true;
130
516644
  }
131
}
132
133
994666
MaybeLocal<Value> InternalMakeCallback(Environment* env,
134
                                       Local<Object> recv,
135
                                       const Local<Function> callback,
136
                                       int argc,
137
                                       Local<Value> argv[],
138
                                       async_context asyncContext) {
139
994666
  CHECK(!recv.IsEmpty());
140
#ifdef DEBUG
141
  for (int i = 0; i < argc; i++)
142
    CHECK(!argv[i].IsEmpty());
143
#endif
144
145
994666
  InternalCallbackScope scope(env, recv, asyncContext);
146
994659
  if (scope.Failed()) {
147
434
    return MaybeLocal<Value>();
148
  }
149
150
994225
  Local<Function> domain_cb = env->domain_callback();
151
  MaybeLocal<Value> ret;
152


1061214
  if (asyncContext.async_id != 0 || domain_cb.IsEmpty() || recv.IsEmpty()) {
153
1984469
    ret = callback->Call(env->context(), recv, argc, argv);
154
  } else {
155
1991
    std::vector<Local<Value>> args(1 + argc);
156
3982
    args[0] = callback;
157
1991
    std::copy(&argv[0], &argv[argc], args.begin() + 1);
158
5973
    ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]);
159
  }
160
161
994193
  if (ret.IsEmpty()) {
162
584
    scope.MarkAsFailed();
163
584
    return MaybeLocal<Value>();
164
  }
165
166
993609
  scope.Close();
167
993527
  if (scope.Failed()) {
168
528
    return MaybeLocal<Value>();
169
  }
170
171
992999
  return ret;
172
}
173
174
// Public MakeCallback()s
175
176
9130
MaybeLocal<Value> MakeCallback(Isolate* isolate,
177
                               Local<Object> recv,
178
                               const char* method,
179
                               int argc,
180
                               Local<Value> argv[],
181
                               async_context asyncContext) {
182
  Local<String> method_string =
183
      String::NewFromUtf8(isolate, method, NewStringType::kNormal)
184
18260
          .ToLocalChecked();
185
9130
  return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
186
}
187
188
9139
MaybeLocal<Value> MakeCallback(Isolate* isolate,
189
                               Local<Object> recv,
190
                               Local<String> symbol,
191
                               int argc,
192
                               Local<Value> argv[],
193
                               async_context asyncContext) {
194
  Local<Value> callback_v =
195
27417
      recv->Get(isolate->GetCurrentContext(), symbol).ToLocalChecked();
196
9139
  if (callback_v.IsEmpty()) return Local<Value>();
197
9213
  if (!callback_v->IsFunction()) return Local<Value>();
198
9065
  Local<Function> callback = callback_v.As<Function>();
199
9065
  return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
200
}
201
202
65013
MaybeLocal<Value> MakeCallback(Isolate* isolate,
203
                               Local<Object> recv,
204
                               Local<Function> callback,
205
                               int argc,
206
                               Local<Value> argv[],
207
                               async_context asyncContext) {
208
  // Observe the following two subtleties:
209
  //
210
  // 1. The environment is retrieved from the callback function's context.
211
  // 2. The context to enter is retrieved from the environment.
212
  //
213
  // Because of the AssignToContext() call in src/node_contextify.cc,
214
  // the two contexts need not be the same.
215
65013
  Environment* env = Environment::GetCurrent(callback->CreationContext());
216
65013
  CHECK_NOT_NULL(env);
217
65013
  Context::Scope context_scope(env->context());
218
  MaybeLocal<Value> ret =
219
65013
      InternalMakeCallback(env, recv, callback, argc, argv, asyncContext);
220

65000
  if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
221
    // This is only for legacy compatibility and we may want to look into
222
    // removing/adjusting it.
223
2076
    return Undefined(env->isolate());
224
  }
225
63962
  return ret;
226
}
227
228
// Legacy MakeCallback()s
229
230
Local<Value> MakeCallback(Isolate* isolate,
231
                          Local<Object> recv,
232
                          const char* method,
233
                          int argc,
234
                          Local<Value>* argv) {
235
  EscapableHandleScope handle_scope(isolate);
236
  return handle_scope.Escape(
237
      MakeCallback(isolate, recv, method, argc, argv, {0, 0})
238
          .FromMaybe(Local<Value>()));
239
}
240
241
Local<Value> MakeCallback(Isolate* isolate,
242
                          Local<Object> recv,
243
                          Local<String> symbol,
244
                          int argc,
245
                          Local<Value>* argv) {
246
  EscapableHandleScope handle_scope(isolate);
247
  return handle_scope.Escape(
248
      MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
249
          .FromMaybe(Local<Value>()));
250
}
251
252
Local<Value> MakeCallback(Isolate* isolate,
253
                          Local<Object> recv,
254
                          Local<Function> callback,
255
                          int argc,
256
                          Local<Value>* argv) {
257
  EscapableHandleScope handle_scope(isolate);
258
  return handle_scope.Escape(
259
      MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
260
          .FromMaybe(Local<Value>()));
261
}
262
263
}  // namespace node