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: 109 121 90.1 %
Date: 2020-05-27 22:15:15 Branches: 76 88 86.4 %

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

661540
  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
607010
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
74
  }
75
}
76
77
1323778
InternalCallbackScope::~InternalCallbackScope() {
78
661901
  Close();
79
661876
  env_->PopAsyncCallbackScope();
80
661877
}
81
82
1271374
void InternalCallbackScope::Close() {
83
2125506
  if (closed_) return;
84
662000
  closed_ = true;
85
86
662000
  if (!env_->can_call_into_js()) return;
87
1261987
  auto perform_stopping_check = [&]() {
88
1262000
    if (env_->is_stopping()) {
89
13
      MarkAsFailed();
90
13
      env_->async_hooks()->clear_async_id_stack();
91
    }
92
1923208
  };
93
661223
  perform_stopping_check();
94
95

661217
  if (!failed_ && async_context_.async_id != 0 && !skip_hooks_) {
96
606906
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
97
  }
98
99
661221
  if (pushed_ids_)
100
661220
    env_->async_hooks()->pop_async_context(async_context_.async_id);
101
102
661223
  if (failed_) return;
103
104

660632
  if (env_->async_callback_scope_depth() > 1 || skip_task_queues_) {
105
60142
    return;
106
  }
107
108
600491
  TickInfo* tick_info = env_->tick_info();
109
110
600488
  if (!env_->can_call_into_js()) return;
111
112
1617970
  auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); });
113
114
600486
  if (!tick_info->has_tick_scheduled()) {
115
183683
    MicrotasksScope::PerformCheckpoint(env_->isolate());
116
117
183660
    perform_stopping_check();
118
  }
119
120
  // Make sure the stack unwound properly. If there are nested MakeCallback's
121
  // then it should return early and not reach this code.
122
600469
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
123
38133
    CHECK_EQ(env_->execution_async_id(), 0);
124
38133
    CHECK_EQ(env_->trigger_async_id(), 0);
125
  }
126
127

600471
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
128
183250
    return;
129
  }
130
131
834336
  HandleScope handle_scope(env_->isolate());
132
417221
  Local<Object> process = env_->process_object();
133
134
417221
  if (!env_->can_call_into_js()) return;
135
136
417221
  Local<Function> tick_callback = env_->tick_callback_function();
137
138
  // The tick is triggered before JS land calls SetTickCallback
139
  // to initializes the tick callback during bootstrap.
140
417221
  CHECK(!tick_callback.IsEmpty());
141
142
1251558
  if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
143
548
    failed_ = true;
144
  }
145
417116
  perform_stopping_check();
146
}
147
148
610432
MaybeLocal<Value> InternalMakeCallback(Environment* env,
149
                                       Local<Object> resource,
150
                                       Local<Object> recv,
151
                                       const Local<Function> callback,
152
                                       int argc,
153
                                       Local<Value> argv[],
154
                                       async_context asyncContext) {
155
610432
  CHECK(!recv.IsEmpty());
156
#ifdef DEBUG
157
  for (int i = 0; i < argc; i++)
158
    CHECK(!argv[i].IsEmpty());
159
#endif
160
161
1221110
  InternalCallbackScope scope(env, resource, asyncContext);
162
610745
  if (scope.Failed()) {
163
680
    return MaybeLocal<Value>();
164
  }
165
166
610122
  Local<Function> domain_cb = env->domain_callback();
167
  MaybeLocal<Value> ret;
168

648904
  if (asyncContext.async_id != 0 || domain_cb.IsEmpty()) {
169
1216895
    ret = callback->Call(env->context(), recv, argc, argv);
170
  } else {
171
3192
    std::vector<Local<Value>> args(1 + argc);
172
3194
    args[0] = callback;
173
1597
    std::copy(&argv[0], &argv[argc], args.begin() + 1);
174
4791
    ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]);
175
  }
176
177
610093
  if (ret.IsEmpty()) {
178
622
    scope.MarkAsFailed();
179
622
    return MaybeLocal<Value>();
180
  }
181
182
609471
  scope.Close();
183
609372
  if (scope.Failed()) {
184
515
    return MaybeLocal<Value>();
185
  }
186
187
608861
  return ret;
188
}
189
190
// Public MakeCallback()s
191
192
8218
MaybeLocal<Value> MakeCallback(Isolate* isolate,
193
                               Local<Object> recv,
194
                               const char* method,
195
                               int argc,
196
                               Local<Value> argv[],
197
                               async_context asyncContext) {
198
  Local<String> method_string =
199
16434
      String::NewFromUtf8(isolate, method, NewStringType::kNormal)
200
8216
          .ToLocalChecked();
201
8216
  return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
202
}
203
204
8227
MaybeLocal<Value> MakeCallback(Isolate* isolate,
205
                               Local<Object> recv,
206
                               Local<String> symbol,
207
                               int argc,
208
                               Local<Value> argv[],
209
                               async_context asyncContext) {
210
  Local<Value> callback_v =
211
24680
      recv->Get(isolate->GetCurrentContext(), symbol).ToLocalChecked();
212
8227
  if (callback_v.IsEmpty()) return Local<Value>();
213
8303
  if (!callback_v->IsFunction()) return Local<Value>();
214
8150
  Local<Function> callback = callback_v.As<Function>();
215
8150
  return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
216
}
217
218
38944
MaybeLocal<Value> MakeCallback(Isolate* isolate,
219
                               Local<Object> recv,
220
                               Local<Function> callback,
221
                               int argc,
222
                               Local<Value> argv[],
223
                               async_context asyncContext) {
224
  // Observe the following two subtleties:
225
  //
226
  // 1. The environment is retrieved from the callback function's context.
227
  // 2. The context to enter is retrieved from the environment.
228
  //
229
  // Because of the AssignToContext() call in src/node_contextify.cc,
230
  // the two contexts need not be the same.
231
38944
  Environment* env = Environment::GetCurrent(callback->CreationContext());
232
38945
  CHECK_NOT_NULL(env);
233
38945
  Context::Scope context_scope(env->context());
234
  MaybeLocal<Value> ret =
235
38945
      InternalMakeCallback(env, recv, recv, callback, argc, argv, asyncContext);
236

38923
  if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
237
    // This is only for legacy compatibility and we may want to look into
238
    // removing/adjusting it.
239
2092
    return Undefined(env->isolate());
240
  }
241
37877
  return ret;
242
}
243
244
// Legacy MakeCallback()s
245
246
Local<Value> MakeCallback(Isolate* isolate,
247
                          Local<Object> recv,
248
                          const char* method,
249
                          int argc,
250
                          Local<Value>* argv) {
251
  EscapableHandleScope handle_scope(isolate);
252
  return handle_scope.Escape(
253
      MakeCallback(isolate, recv, method, argc, argv, {0, 0})
254
          .FromMaybe(Local<Value>()));
255
}
256
257
Local<Value> MakeCallback(Isolate* isolate,
258
                          Local<Object> recv,
259
                          Local<String> symbol,
260
                          int argc,
261
                          Local<Value>* argv) {
262
  EscapableHandleScope handle_scope(isolate);
263
  return handle_scope.Escape(
264
      MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
265
          .FromMaybe(Local<Value>()));
266
}
267
268
Local<Value> MakeCallback(Isolate* isolate,
269
                          Local<Object> recv,
270
                          Local<Function> callback,
271
                          int argc,
272
                          Local<Value>* argv) {
273
  EscapableHandleScope handle_scope(isolate);
274
  return handle_scope.Escape(
275
      MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
276
          .FromMaybe(Local<Value>()));
277
}
278
279
}  // namespace node