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: 132 148 89.2 %
Date: 2021-05-03 04:13:35 Branches: 85 104 81.7 %

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::Object;
16
using v8::String;
17
using v8::Value;
18
19
647
CallbackScope::CallbackScope(Isolate* isolate,
20
                             Local<Object> object,
21
647
                             async_context asyncContext)
22
647
  : private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
23
                                       object,
24
647
                                       asyncContext)),
25
1294
    try_catch_(isolate) {
26
647
  try_catch_.SetVerbose(true);
27
647
}
28
29
1292
CallbackScope::~CallbackScope() {
30
646
  if (try_catch_.HasCaught())
31
1
    private_->MarkAsFailed();
32
646
  delete private_;
33
646
}
34
35
51195
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap, int flags)
36
    : InternalCallbackScope(async_wrap->env(),
37
                            async_wrap->object(),
38
51195
                            { async_wrap->get_async_id(),
39
51195
                              async_wrap->get_trigger_async_id() },
40
102390
                            flags) {}
41
42
688722
InternalCallbackScope::InternalCallbackScope(Environment* env,
43
                                             Local<Object> object,
44
                                             const async_context& asyncContext,
45
688722
                                             int flags)
46
  : env_(env),
47
    async_context_(asyncContext),
48
    object_(object),
49
688722
    skip_hooks_(flags & kSkipAsyncHooks),
50
1377444
    skip_task_queues_(flags & kSkipTaskQueues) {
51
688722
  CHECK_NOT_NULL(env);
52
688722
  env->PushAsyncCallbackScope();
53
54
688757
  if (!env->can_call_into_js()) {
55
10753
    failed_ = true;
56
10753
    return;
57
  }
58
59
1356073
  HandleScope handle_scope(env->isolate());
60
  // If you hit this assertion, you forgot to enter the v8::Context first.
61
677996
  CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
62
63
678064
  env->isolate()->SetIdle(false);
64
65
678034
  env->async_hooks()->push_async_context(
66
677987
    async_context_.async_id, async_context_.trigger_async_id, object);
67
68
678075
  pushed_ids_ = true;
69
70

678075
  if (asyncContext.async_id != 0 && !skip_hooks_) {
71
    // No need to check a return value because the application will exit if
72
    // an exception occurs.
73
51835
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
74
  }
75
}
76
77
1376826
InternalCallbackScope::~InternalCallbackScope() {
78
688456
  Close();
79
688374
  env_->PopAsyncCallbackScope();
80
688370
}
81
82
1095108
void InternalCallbackScope::Close() {
83
2022663
  if (closed_) return;
84
688339
  closed_ = true;
85
86
1544317
  auto idle = OnScopeLeave([&]() { env_->isolate()->SetIdle(true); });
87
88
688212
  if (!env_->can_call_into_js()) return;
89
1272568
  auto perform_stopping_check = [&]() {
90
1272618
    if (env_->is_stopping()) {
91
50
      MarkAsFailed();
92
50
      env_->async_hooks()->clear_async_id_stack();
93
    }
94
1949741
  };
95
677420
  perform_stopping_check();
96
97

677195
  if (!failed_ && async_context_.async_id != 0 && !skip_hooks_) {
98
51823
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
99
  }
100
101
677200
  if (pushed_ids_)
102
677198
    env_->async_hooks()->pop_async_context(async_context_.async_id);
103
104
676965
  if (failed_) return;
105
106

675980
  if (env_->async_callback_scope_depth() > 1 || skip_task_queues_) {
107
82059
    return;
108
  }
109
110
593923
  TickInfo* tick_info = env_->tick_info();
111
112
593917
  if (!env_->can_call_into_js()) return;
113
114
1356389
  auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); });
115
116
594200
  if (!tick_info->has_tick_scheduled()) {
117
854340
    env_->context()->GetMicrotaskQueue()->PerformCheckpoint(env_->isolate());
118
119
427700
    perform_stopping_check();
120
  }
121
122
  // Make sure the stack unwound properly. If there are nested MakeCallback's
123
  // then it should return early and not reach this code.
124
594706
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
125
66538
    CHECK_EQ(env_->execution_async_id(), 0);
126
66538
    CHECK_EQ(env_->trigger_async_id(), 0);
127
  }
128
129

594701
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
130
426960
    return;
131
  }
132
133
335328
  HandleScope handle_scope(env_->isolate());
134
167730
  Local<Object> process = env_->process_object();
135
136
167730
  if (!env_->can_call_into_js()) return;
137
138
167730
  Local<Function> tick_callback = env_->tick_callback_function();
139
140
  // The tick is triggered before JS land calls SetTickCallback
141
  // to initializes the tick callback during bootstrap.
142
167730
  CHECK(!tick_callback.IsEmpty());
143
144
503059
  if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
145
196
    failed_ = true;
146
  }
147
167599
  perform_stopping_check();
148
}
149
150
408829
MaybeLocal<Value> InternalMakeCallback(Environment* env,
151
                                       Local<Object> resource,
152
                                       Local<Object> recv,
153
                                       const Local<Function> callback,
154
                                       int argc,
155
                                       Local<Value> argv[],
156
                                       async_context asyncContext) {
157
408829
  CHECK(!recv.IsEmpty());
158
#ifdef DEBUG
159
  for (int i = 0; i < argc; i++)
160
    CHECK(!argv[i].IsEmpty());
161
#endif
162
163
408829
  Local<Function> hook_cb = env->async_hooks_callback_trampoline();
164
408912
  int flags = InternalCallbackScope::kNoFlags;
165
408912
  bool use_async_hooks_trampoline = false;
166
408912
  AsyncHooks* async_hooks = env->async_hooks();
167
408910
  if (!hook_cb.IsEmpty()) {
168
    // Use the callback trampoline if there are any before or after hooks, or
169
    // we can expect some kind of usage of async_hooks.executionAsyncResource().
170
408895
    flags = InternalCallbackScope::kSkipAsyncHooks;
171
408898
    use_async_hooks_trampoline =
172
817790
        async_hooks->fields()[AsyncHooks::kBefore] +
173
1226693
        async_hooks->fields()[AsyncHooks::kAfter] +
174
817793
        async_hooks->fields()[AsyncHooks::kUsesExecutionAsyncResource] > 0;
175
  }
176
177
817677
  InternalCallbackScope scope(env, resource, asyncContext, flags);
178
408928
  if (scope.Failed()) {
179
958
    return MaybeLocal<Value>();
180
  }
181
182
  MaybeLocal<Value> ret;
183
184
407905
  if (use_async_hooks_trampoline) {
185
59502
    MaybeStackBuffer<Local<Value>, 16> args(3 + argc);
186
59506
    args[0] = v8::Number::New(env->isolate(), asyncContext.async_id);
187
59506
    args[1] = resource;
188
59506
    args[2] = callback;
189
39743
    for (int i = 0; i < argc; i++) {
190
9990
      args[i + 3] = argv[i];
191
    }
192
89259
    ret = hook_cb->Call(env->context(), recv, args.length(), &args[0]);
193
  } else {
194
756371
    ret = callback->Call(env->context(), recv, argc, argv);
195
  }
196
197
407931
  if (ret.IsEmpty()) {
198
1002
    scope.MarkAsFailed();
199
1002
    return MaybeLocal<Value>();
200
  }
201
202
406929
  scope.Close();
203
406806
  if (scope.Failed()) {
204
110
    return MaybeLocal<Value>();
205
  }
206
207
406694
  return ret;
208
}
209
210
// Public MakeCallback()s
211
212
9298
MaybeLocal<Value> MakeCallback(Isolate* isolate,
213
                               Local<Object> recv,
214
                               const char* method,
215
                               int argc,
216
                               Local<Value> argv[],
217
                               async_context asyncContext) {
218
  Local<String> method_string =
219
18596
      String::NewFromUtf8(isolate, method).ToLocalChecked();
220
9298
  return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
221
}
222
223
9306
MaybeLocal<Value> MakeCallback(Isolate* isolate,
224
                               Local<Object> recv,
225
                               Local<String> symbol,
226
                               int argc,
227
                               Local<Value> argv[],
228
                               async_context asyncContext) {
229
  // Check can_call_into_js() first because calling Get() might do so.
230
  Environment* env =
231
18612
      Environment::GetCurrent(recv->GetCreationContext().ToLocalChecked());
232
9305
  CHECK_NOT_NULL(env);
233
9309
  if (!env->can_call_into_js()) return Local<Value>();
234
235
  Local<Value> callback_v;
236
27906
  if (!recv->Get(isolate->GetCurrentContext(), symbol).ToLocal(&callback_v))
237
    return Local<Value>();
238
9302
  if (!callback_v->IsFunction()) {
239
    // This used to return an empty value, but Undefined() makes more sense
240
    // since no exception is pending here.
241
    return Undefined(isolate);
242
  }
243
9302
  Local<Function> callback = callback_v.As<Function>();
244
9302
  return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
245
}
246
247
73495
MaybeLocal<Value> MakeCallback(Isolate* isolate,
248
                               Local<Object> recv,
249
                               Local<Function> callback,
250
                               int argc,
251
                               Local<Value> argv[],
252
                               async_context asyncContext) {
253
  // Observe the following two subtleties:
254
  //
255
  // 1. The environment is retrieved from the callback function's context.
256
  // 2. The context to enter is retrieved from the environment.
257
  //
258
  // Because of the AssignToContext() call in src/node_contextify.cc,
259
  // the two contexts need not be the same.
260
  Environment* env =
261
146991
      Environment::GetCurrent(callback->GetCreationContext().ToLocalChecked());
262
73496
  CHECK_NOT_NULL(env);
263
73496
  Context::Scope context_scope(env->context());
264
  MaybeLocal<Value> ret =
265
73495
      InternalMakeCallback(env, recv, recv, callback, argc, argv, asyncContext);
266

73477
  if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
267
    // This is only for legacy compatibility and we may want to look into
268
    // removing/adjusting it.
269
2002
    return Undefined(env->isolate());
270
  }
271
72476
  return ret;
272
}
273
274
// Use this if you just want to safely invoke some JS callback and
275
// would like to retain the currently active async_context, if any.
276
// In case none is available, a fixed default context will be
277
// installed otherwise.
278
16
MaybeLocal<Value> MakeSyncCallback(Isolate* isolate,
279
                                   Local<Object> recv,
280
                                   Local<Function> callback,
281
                                   int argc,
282
                                   Local<Value> argv[]) {
283
  Environment* env =
284
32
      Environment::GetCurrent(callback->GetCreationContext().ToLocalChecked());
285
16
  CHECK_NOT_NULL(env);
286
16
  if (!env->can_call_into_js()) return Local<Value>();
287
288
16
  Context::Scope context_scope(env->context());
289
16
  if (env->async_callback_scope_depth()) {
290
    // There's another MakeCallback() on the stack, piggy back on it.
291
    // In particular, retain the current async_context.
292
32
    return callback->Call(env->context(), recv, argc, argv);
293
  }
294
295
  // This is a toplevel invocation and the caller (intentionally)
296
  // didn't provide any async_context to run in. Install a default context.
297
  MaybeLocal<Value> ret =
298
    InternalMakeCallback(env, env->process_object(), recv, callback, argc, argv,
299
                         async_context{0, 0});
300
  return ret;
301
}
302
303
// Legacy MakeCallback()s
304
305
Local<Value> MakeCallback(Isolate* isolate,
306
                          Local<Object> recv,
307
                          const char* method,
308
                          int argc,
309
                          Local<Value>* argv) {
310
  EscapableHandleScope handle_scope(isolate);
311
  return handle_scope.Escape(
312
      MakeCallback(isolate, recv, method, argc, argv, {0, 0})
313
          .FromMaybe(Local<Value>()));
314
}
315
316
Local<Value> MakeCallback(Isolate* isolate,
317
                          Local<Object> recv,
318
                          Local<String> symbol,
319
                          int argc,
320
                          Local<Value>* argv) {
321
  EscapableHandleScope handle_scope(isolate);
322
  return handle_scope.Escape(
323
      MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
324
          .FromMaybe(Local<Value>()));
325
}
326
327
Local<Value> MakeCallback(Isolate* isolate,
328
                          Local<Object> recv,
329
                          Local<Function> callback,
330
                          int argc,
331
                          Local<Value>* argv) {
332
  EscapableHandleScope handle_scope(isolate);
333
  return handle_scope.Escape(
334
      MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
335
          .FromMaybe(Local<Value>()));
336
}
337
338

14487
}  // namespace node