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-02-13 22:28:58 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::NewStringType;
16
using v8::Object;
17
using v8::String;
18
using v8::Value;
19
20
using AsyncHooks = Environment::AsyncHooks;
21
22
630
CallbackScope::CallbackScope(Isolate* isolate,
23
                             Local<Object> object,
24
                             async_context asyncContext)
25
  : private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
26
                                       object,
27
630
                                       asyncContext)),
28
1260
    try_catch_(isolate) {
29
630
  try_catch_.SetVerbose(true);
30
630
}
31
32
1258
CallbackScope::~CallbackScope() {
33
629
  if (try_catch_.HasCaught())
34
1
    private_->MarkAsFailed();
35
629
  delete private_;
36
629
}
37
38
1577
InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap)
39
    : InternalCallbackScope(async_wrap->env(),
40
                            async_wrap->object(),
41
1577
                            { async_wrap->get_async_id(),
42
1577
                              async_wrap->get_trigger_async_id() }) {}
43
44
1208457
InternalCallbackScope::InternalCallbackScope(Environment* env,
45
                                             Local<Object> object,
46
                                             const async_context& asyncContext,
47
                                             ResourceExpectation expect)
48
  : env_(env),
49
    async_context_(asyncContext),
50
    object_(object),
51
1208457
    callback_scope_(env) {
52

2405765
  CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty());
53
1208459
  CHECK_NOT_NULL(env);
54
55
1208459
  if (!env->can_call_into_js()) {
56
4411
    failed_ = true;
57
1212644
    return;
58
  }
59
60
1204050
  HandleScope handle_scope(env->isolate());
61
  // If you hit this assertion, you forgot to enter the v8::Context first.
62
1204050
  CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
63
64
1204050
  if (asyncContext.async_id != 0) {
65
    // No need to check a return value because the application will exit if
66
    // an exception occurs.
67
1129165
    AsyncWrap::EmitBefore(env, asyncContext.async_id);
68
  }
69
70
  env->async_hooks()->push_async_ids(async_context_.async_id,
71
1203822
                               async_context_.trigger_async_id);
72
1203821
  pushed_ids_ = true;
73
}
74
75
2416186
InternalCallbackScope::~InternalCallbackScope() {
76
1208094
  Close();
77
1208091
}
78
79
2396308
void InternalCallbackScope::Close() {
80
4238539
  if (closed_) return;
81
1208163
  closed_ = true;
82
1208163
  HandleScope handle_scope(env_->isolate());
83
84
1208165
  if (!env_->can_call_into_js()) return;
85


1203725
  if (failed_ && !env_->is_main_thread() && env_->is_stopping_worker()) {
86
    env_->async_hooks()->clear_async_id_stack();
87
  }
88
89
1203725
  if (pushed_ids_)
90
1203725
    env_->async_hooks()->pop_async_id(async_context_.async_id);
91
92
1203723
  if (failed_) return;
93
94
1203162
  if (async_context_.async_id != 0) {
95
1128847
    AsyncWrap::EmitAfter(env_, async_context_.async_id);
96
  }
97
98
1203163
  if (env_->makecallback_depth() > 1) {
99
227416
    return;
100
  }
101
102
975747
  Environment::TickInfo* tick_info = env_->tick_info();
103
104
975747
  if (!env_->can_call_into_js()) return;
105
975748
  if (!tick_info->has_tick_scheduled()) {
106
422052
    env_->isolate()->RunMicrotasks();
107
  }
108
109
  // Make sure the stack unwound properly. If there are nested MakeCallback's
110
  // then it should return early and not reach this code.
111
975742
  if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
112
37232
    CHECK_EQ(env_->execution_async_id(), 0);
113
37232
    CHECK_EQ(env_->trigger_async_id(), 0);
114
  }
115
116

975742
  if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) {
117
421671
    return;
118
  }
119
120
554071
  Local<Object> process = env_->process_object();
121
122
554071
  if (!env_->can_call_into_js()) return;
123
124
554071
  Local<Function> tick_callback = env_->tick_callback_function();
125
126
  // The tick is triggered before JS land calls SetTickCallback
127
  // to initializes the tick callback during bootstrap.
128
554071
  CHECK(!tick_callback.IsEmpty());
129
130
1662146
  if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
131
545
    failed_ = true;
132
554004
  }
133
}
134
135
1189382
MaybeLocal<Value> InternalMakeCallback(Environment* env,
136
                                       Local<Object> recv,
137
                                       const Local<Function> callback,
138
                                       int argc,
139
                                       Local<Value> argv[],
140
                                       async_context asyncContext) {
141
1189382
  CHECK(!recv.IsEmpty());
142
1189382
  InternalCallbackScope scope(env, recv, asyncContext);
143
1189158
  if (scope.Failed()) {
144
321
    return MaybeLocal<Value>();
145
  }
146
147
1188837
  Local<Function> domain_cb = env->domain_callback();
148
  MaybeLocal<Value> ret;
149


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

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