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-10-07 22:40:39 Branches: 73 90 81.1 %

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
                             async_context asyncContext)
24
  : 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
3667
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap)
38
    : InternalCallbackScope(async_wrap->env(),
39
                            async_wrap->object(),
40
3667
                            { async_wrap->get_async_id(),
41
3667
                              async_wrap->get_trigger_async_id() }) {}
42
43
1187991
InternalCallbackScope::InternalCallbackScope(Environment* env,
44
                                             Local<Object> object,
45
                                             const async_context& asyncContext,
46
                                             ResourceExpectation expect)
47
  : env_(env),
48
    async_context_(asyncContext),
49
    object_(object),
50
1187991
    callback_scope_(env) {
51

2368089
  CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty());
52
1188046
  CHECK_NOT_NULL(env);
53
54
1188046
  if (!env->can_call_into_js()) {
55
1259
    failed_ = true;
56
1189349
    return;
57
  }
58
59
1186858
  HandleScope handle_scope(env->isolate());
60
  // If you hit this assertion, you forgot to enter the v8::Context first.
61
1186812
  CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
62
63
1186842
  if (asyncContext.async_id != 0) {
64
    // No need to check a return value because the application will exit if
65
    // an exception occurs.
66
1103630
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
67
  }
68
69
  env->async_hooks()->push_async_ids(async_context_.async_id,
70
1186832
                               async_context_.trigger_async_id);
71
1186798
  pushed_ids_ = true;
72
}
73
74
2375925
InternalCallbackScope::~InternalCallbackScope() {
75
1187963
  Close();
76
1187962
}
77
78
2358642
void InternalCallbackScope::Close() {
79
4122061
  if (closed_) return;
80
1187994
  closed_ = true;
81
82
1187994
  if (!env_->can_call_into_js()) return;
83


1186735
  if (failed_ && !env_->is_main_thread() && env_->is_stopping()) {
84
    env_->async_hooks()->clear_async_id_stack();
85
  }
86
87
1186710
  if (pushed_ids_)
88
1186709
    env_->async_hooks()->pop_async_id(async_context_.async_id);
89
90
1186751
  if (failed_) return;
91
92
1186189
  if (async_context_.async_id != 0) {
93
1103533
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
94
  }
95
96
1186142
  if (env_->async_callback_scope_depth() > 1) {
97
294081
    return;
98
  }
99
100
892108
  TickInfo* tick_info = env_->tick_info();
101
102
892092
  if (!env_->can_call_into_js()) return;
103
892107
  if (!tick_info->has_tick_scheduled()) {
104
297253
    MicrotasksScope::PerformCheckpoint(env_->isolate());
105
  }
106
107
  // Make sure the stack unwound properly. If there are nested MakeCallback's
108
  // then it should return early and not reach this code.
109
892106
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
110
37431
    CHECK_EQ(env_->execution_async_id(), 0);
111
37431
    CHECK_EQ(env_->trigger_async_id(), 0);
112
  }
113
114

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


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

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