GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: api/callback.cc Lines: 139 155 89.7 %
Date: 2022-08-30 04:20:47 Branches: 80 96 83.3 %

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
4
CallbackScope::CallbackScope(Isolate* isolate,
20
                             Local<Object> object,
21
4
                             async_context async_context)
22
4
  : CallbackScope(Environment::GetCurrent(isolate), object, async_context) {}
23
24
115543
CallbackScope::CallbackScope(Environment* env,
25
                             Local<Object> object,
26
115543
                             async_context asyncContext)
27
  : private_(new InternalCallbackScope(env,
28
                                       object,
29
115543
                                       asyncContext)),
30
231086
    try_catch_(env->isolate()) {
31
115543
  try_catch_.SetVerbose(true);
32
115543
}
33
34
115542
CallbackScope::~CallbackScope() {
35
115542
  if (try_catch_.HasCaught())
36
1
    private_->MarkAsFailed();
37
115542
  delete private_;
38
115542
}
39
40
42271
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap, int flags)
41
    : InternalCallbackScope(async_wrap->env(),
42
                            async_wrap->object(),
43
42271
                            { async_wrap->get_async_id(),
44
42271
                              async_wrap->get_trigger_async_id() },
45
42271
                            flags) {}
46
47
821678
InternalCallbackScope::InternalCallbackScope(Environment* env,
48
                                             Local<Object> object,
49
                                             const async_context& asyncContext,
50
821678
                                             int flags)
51
  : env_(env),
52
    async_context_(asyncContext),
53
    object_(object),
54
821678
    skip_hooks_(flags & kSkipAsyncHooks),
55
1643356
    skip_task_queues_(flags & kSkipTaskQueues) {
56
821678
  CHECK_NOT_NULL(env);
57
821678
  env->PushAsyncCallbackScope();
58
59
821678
  if (!env->can_call_into_js()) {
60
13680
    failed_ = true;
61
13680
    return;
62
  }
63
64
807998
  Isolate* isolate = env->isolate();
65
66
1615996
  HandleScope handle_scope(isolate);
67
807998
  Local<Context> current_context = isolate->GetCurrentContext();
68
  // If you hit this assertion, the caller forgot to enter the right Node.js
69
  // Environment's v8::Context first.
70
  // We first check `env->context() != current_context` because the contexts
71
  // likely *are* the same, in which case we can skip the slightly more
72
  // expensive Environment::GetCurrent() call.
73
1615996
  if (UNLIKELY(env->context() != current_context)) {
74
6
    CHECK_EQ(Environment::GetCurrent(isolate), env);
75
  }
76
77
807998
  isolate->SetIdle(false);
78
79
807998
  env->async_hooks()->push_async_context(
80
    async_context_.async_id, async_context_.trigger_async_id, object);
81
82
807998
  pushed_ids_ = true;
83
84

807998
  if (asyncContext.async_id != 0 && !skip_hooks_) {
85
    // No need to check a return value because the application will exit if
86
    // an exception occurs.
87
506399
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
88
  }
89
}
90
91
1642165
InternalCallbackScope::~InternalCallbackScope() {
92
821131
  Close();
93
821034
  env_->PopAsyncCallbackScope();
94
821034
}
95
96
1245362
void InternalCallbackScope::Close() {
97
1858663
  if (closed_) return;
98
821346
  closed_ = true;
99
100
821346
  Isolate* isolate = env_->isolate();
101
1642380
  auto idle = OnScopeLeave([&]() { isolate->SetIdle(true); });
102
103
821346
  if (!env_->can_call_into_js()) return;
104
1545775
  auto perform_stopping_check = [&]() {
105
1545775
    if (env_->is_stopping()) {
106
263
      MarkAsFailed();
107
263
      env_->async_hooks()->clear_async_id_stack();
108
    }
109
2353387
  };
110
807612
  perform_stopping_check();
111
112

807612
  if (!failed_ && async_context_.async_id != 0 && !skip_hooks_) {
113
506325
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
114
  }
115
116
807612
  if (pushed_ids_)
117
807612
    env_->async_hooks()->pop_async_context(async_context_.async_id);
118
119
807611
  if (failed_) return;
120
121

806612
  if (env_->async_callback_scope_depth() > 1 || skip_task_queues_) {
122
72064
    return;
123
  }
124
125
734548
  TickInfo* tick_info = env_->tick_info();
126
127
734548
  if (!env_->can_call_into_js()) return;
128
129
1468783
  auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); });
130
131
734547
  Local<Context> context = env_->context();
132
734547
  if (!tick_info->has_tick_scheduled()) {
133
530501
    context->GetMicrotaskQueue()->PerformCheckpoint(isolate);
134
135
530430
    perform_stopping_check();
136
  }
137
138
  // Make sure the stack unwound properly. If there are nested MakeCallback's
139
  // then it should return early and not reach this code.
140
734476
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
141
65862
    CHECK_EQ(env_->execution_async_id(), 0);
142
65862
    CHECK_EQ(env_->trigger_async_id(), 0);
143
  }
144
145

734476
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
146
526503
    return;
147
  }
148
149
207973
  HandleScope handle_scope(isolate);
150
207973
  Local<Object> process = env_->process_object();
151
152
207973
  if (!env_->can_call_into_js()) return;
153
154
207973
  Local<Function> tick_callback = env_->tick_callback_function();
155
156
  // The tick is triggered before JS land calls SetTickCallback
157
  // to initializes the tick callback during bootstrap.
158
207973
  CHECK(!tick_callback.IsEmpty());
159
160
415706
  if (tick_callback->Call(context, process, 0, nullptr).IsEmpty()) {
161
408
    failed_ = true;
162
  }
163
207733
  perform_stopping_check();
164
}
165
166
427001
MaybeLocal<Value> InternalMakeCallback(Environment* env,
167
                                       Local<Object> resource,
168
                                       Local<Object> recv,
169
                                       const Local<Function> callback,
170
                                       int argc,
171
                                       Local<Value> argv[],
172
                                       async_context asyncContext) {
173
427001
  CHECK(!recv.IsEmpty());
174
#ifdef DEBUG
175
  for (int i = 0; i < argc; i++)
176
    CHECK(!argv[i].IsEmpty());
177
#endif
178
179
427001
  Local<Function> hook_cb = env->async_hooks_callback_trampoline();
180
427001
  int flags = InternalCallbackScope::kNoFlags;
181
427001
  bool use_async_hooks_trampoline = false;
182
427001
  AsyncHooks* async_hooks = env->async_hooks();
183
427001
  if (!hook_cb.IsEmpty()) {
184
    // Use the callback trampoline if there are any before or after hooks, or
185
    // we can expect some kind of usage of async_hooks.executionAsyncResource().
186
48469
    flags = InternalCallbackScope::kSkipAsyncHooks;
187
48469
    use_async_hooks_trampoline =
188
48469
        async_hooks->fields()[AsyncHooks::kBefore] +
189
48469
        async_hooks->fields()[AsyncHooks::kAfter] +
190
48469
        async_hooks->fields()[AsyncHooks::kUsesExecutionAsyncResource] > 0;
191
  }
192
193
853737
  InternalCallbackScope scope(env, resource, asyncContext, flags);
194
427001
  if (scope.Failed()) {
195
1702
    return MaybeLocal<Value>();
196
  }
197
198
  MaybeLocal<Value> ret;
199
200
425299
  Local<Context> context = env->context();
201
425299
  if (use_async_hooks_trampoline) {
202
30844
    MaybeStackBuffer<Local<Value>, 16> args(3 + argc);
203
61688
    args[0] = v8::Number::New(env->isolate(), asyncContext.async_id);
204
30844
    args[1] = resource;
205
30844
    args[2] = callback;
206
41648
    for (int i = 0; i < argc; i++) {
207
10804
      args[i + 3] = argv[i];
208
    }
209
30844
    ret = hook_cb->Call(context, recv, args.length(), &args[0]);
210
  } else {
211
394455
    ret = callback->Call(context, recv, argc, argv);
212
  }
213
214
425249
  if (ret.IsEmpty()) {
215
1018
    scope.MarkAsFailed();
216
1018
    return MaybeLocal<Value>();
217
  }
218
219
424231
  scope.Close();
220
424016
  if (scope.Failed()) {
221
321
    return MaybeLocal<Value>();
222
  }
223
224
423695
  return ret;
225
}
226
227
// Public MakeCallback()s
228
229
10444
MaybeLocal<Value> MakeCallback(Isolate* isolate,
230
                               Local<Object> recv,
231
                               const char* method,
232
                               int argc,
233
                               Local<Value> argv[],
234
                               async_context asyncContext) {
235
  Local<String> method_string =
236
10444
      String::NewFromUtf8(isolate, method).ToLocalChecked();
237
10444
  return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
238
}
239
240
10453
MaybeLocal<Value> MakeCallback(Isolate* isolate,
241
                               Local<Object> recv,
242
                               Local<String> symbol,
243
                               int argc,
244
                               Local<Value> argv[],
245
                               async_context asyncContext) {
246
  // Check can_call_into_js() first because calling Get() might do so.
247
  Environment* env =
248
20906
      Environment::GetCurrent(recv->GetCreationContext().ToLocalChecked());
249
10453
  CHECK_NOT_NULL(env);
250
10469
  if (!env->can_call_into_js()) return Local<Value>();
251
252
  Local<Value> callback_v;
253
20874
  if (!recv->Get(isolate->GetCurrentContext(), symbol).ToLocal(&callback_v))
254
    return Local<Value>();
255
10437
  if (!callback_v->IsFunction()) {
256
    // This used to return an empty value, but Undefined() makes more sense
257
    // since no exception is pending here.
258
    return Undefined(isolate);
259
  }
260
10437
  Local<Function> callback = callback_v.As<Function>();
261
10437
  return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
262
}
263
264
51375
MaybeLocal<Value> MakeCallback(Isolate* isolate,
265
                               Local<Object> recv,
266
                               Local<Function> callback,
267
                               int argc,
268
                               Local<Value> argv[],
269
                               async_context asyncContext) {
270
  // Observe the following two subtleties:
271
  //
272
  // 1. The environment is retrieved from the callback function's context.
273
  // 2. The context to enter is retrieved from the environment.
274
  //
275
  // Because of the AssignToContext() call in src/node_contextify.cc,
276
  // the two contexts need not be the same.
277
  Environment* env =
278
102750
      Environment::GetCurrent(callback->GetCreationContext().ToLocalChecked());
279
51375
  CHECK_NOT_NULL(env);
280
51375
  Context::Scope context_scope(env->context());
281
  MaybeLocal<Value> ret =
282
51375
      InternalMakeCallback(env, recv, recv, callback, argc, argv, asyncContext);
283

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