@@ -165,6 +165,7 @@ static void DestroyIdsCb(uv_timer_t* handle) {
165
165
if (ret.IsEmpty ()) {
166
166
ClearFatalExceptionHandlers (env);
167
167
FatalException (env->isolate (), try_catch);
168
+ UNREACHABLE ();
168
169
}
169
170
}
170
171
} while (!env->destroy_ids_list ()->empty ());
@@ -218,69 +219,43 @@ bool DomainExit(Environment* env, v8::Local<v8::Object> object) {
218
219
}
219
220
220
221
221
- static bool PreCallbackExecution (AsyncWrap* wrap, bool run_domain_cbs) {
222
- if (wrap->env ()->using_domains () && run_domain_cbs) {
223
- bool is_disposed = DomainEnter (wrap->env (), wrap->object ());
224
- if (is_disposed)
225
- return false ;
226
- }
227
-
228
- return AsyncWrap::EmitBefore (wrap->env (), wrap->get_id ());
229
- }
230
-
231
-
232
- bool AsyncWrap::EmitBefore (Environment* env, double async_id) {
222
+ void AsyncWrap::EmitBefore (Environment* env, double async_id) {
233
223
AsyncHooks* async_hooks = env->async_hooks ();
234
224
235
- if (async_hooks->fields ()[AsyncHooks::kBefore ] > 0 ) {
236
- Local<Value> uid = Number::New (env->isolate (), async_id);
237
- Local<Function> fn = env->async_hooks_before_function ();
238
- TryCatch try_catch (env->isolate ());
239
- MaybeLocal<Value> ar = fn->Call (
240
- env->context (), Undefined (env->isolate ()), 1 , &uid);
241
- if (ar.IsEmpty ()) {
242
- ClearFatalExceptionHandlers (env);
243
- FatalException (env->isolate (), try_catch);
244
- return false ;
245
- }
246
- }
247
-
248
- return true ;
249
- }
250
-
251
-
252
- static bool PostCallbackExecution (AsyncWrap* wrap, bool run_domain_cbs) {
253
- if (!AsyncWrap::EmitAfter (wrap->env (), wrap->get_id ()))
254
- return false ;
225
+ if (async_hooks->fields ()[AsyncHooks::kBefore ] == 0 )
226
+ return ;
255
227
256
- if (wrap->env ()->using_domains () && run_domain_cbs) {
257
- bool is_disposed = DomainExit (wrap->env (), wrap->object ());
258
- if (is_disposed)
259
- return false ;
228
+ Local<Value> uid = Number::New (env->isolate (), async_id);
229
+ Local<Function> fn = env->async_hooks_before_function ();
230
+ TryCatch try_catch (env->isolate ());
231
+ MaybeLocal<Value> ar = fn->Call (
232
+ env->context (), Undefined (env->isolate ()), 1 , &uid);
233
+ if (ar.IsEmpty ()) {
234
+ ClearFatalExceptionHandlers (env);
235
+ FatalException (env->isolate (), try_catch);
236
+ UNREACHABLE ();
260
237
}
261
-
262
- return true ;
263
238
}
264
239
265
- bool AsyncWrap::EmitAfter (Environment* env, double async_id) {
240
+
241
+ void AsyncWrap::EmitAfter (Environment* env, double async_id) {
266
242
AsyncHooks* async_hooks = env->async_hooks ();
267
243
268
- // If the callback failed then the after() hooks will be called at the end
269
- // of _fatalException().
270
- if (async_hooks->fields ()[AsyncHooks::kAfter ] > 0 ) {
271
- Local<Value> uid = Number::New (env->isolate (), async_id);
272
- Local<Function> fn = env->async_hooks_after_function ();
273
- TryCatch try_catch (env->isolate ());
274
- MaybeLocal<Value> ar = fn->Call (
275
- env->context (), Undefined (env->isolate ()), 1 , &uid);
276
- if (ar.IsEmpty ()) {
277
- ClearFatalExceptionHandlers (env);
278
- FatalException (env->isolate (), try_catch);
279
- return false ;
280
- }
281
- }
244
+ if (async_hooks->fields ()[AsyncHooks::kAfter ] == 0 )
245
+ return ;
282
246
283
- return true ;
247
+ // If the user's callback failed then the after() hooks will be called at the
248
+ // end of _fatalException().
249
+ Local<Value> uid = Number::New (env->isolate (), async_id);
250
+ Local<Function> fn = env->async_hooks_after_function ();
251
+ TryCatch try_catch (env->isolate ());
252
+ MaybeLocal<Value> ar = fn->Call (
253
+ env->context (), Undefined (env->isolate ()), 1 , &uid);
254
+ if (ar.IsEmpty ()) {
255
+ ClearFatalExceptionHandlers (env);
256
+ FatalException (env->isolate (), try_catch);
257
+ UNREACHABLE ();
258
+ }
284
259
}
285
260
286
261
class PromiseWrap : public AsyncWrap {
@@ -373,9 +348,9 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
373
348
CHECK_NE (wrap, nullptr );
374
349
if (type == PromiseHookType::kBefore ) {
375
350
env->async_hooks ()->push_ids (wrap->get_id (), wrap->get_trigger_id ());
376
- PreCallbackExecution (wrap, false );
351
+ AsyncWrap::EmitBefore (wrap-> env (), wrap-> get_id () );
377
352
} else if (type == PromiseHookType::kAfter ) {
378
- PostCallbackExecution (wrap, false );
353
+ AsyncWrap::EmitAfter (wrap-> env (), wrap-> get_id () );
379
354
if (env->current_async_id () == wrap->get_id ()) {
380
355
// This condition might not be true if async_hooks was enabled during
381
356
// the promise callback execution.
@@ -696,18 +671,27 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
696
671
get_id (),
697
672
get_trigger_id ());
698
673
699
- if (!PreCallbackExecution (this , true )) {
674
+ // Return v8::Undefined() because returning an empty handle will cause
675
+ // ToLocalChecked() to abort.
676
+ if (env ()->using_domains () && DomainEnter (env (), object ())) {
700
677
return Undefined (env ()->isolate ());
701
678
}
702
679
703
- // Finally... Get to running the user's callback.
680
+ // No need to check a return value because the application will exit if an
681
+ // exception occurs.
682
+ AsyncWrap::EmitBefore (env (), get_id ());
683
+
704
684
MaybeLocal<Value> ret = cb->Call (env ()->context (), object (), argc, argv);
705
685
706
686
if (ret.IsEmpty ()) {
707
687
return ret;
708
688
}
709
689
710
- if (!PostCallbackExecution (this , true )) {
690
+ AsyncWrap::EmitAfter (env (), get_id ());
691
+
692
+ // Return v8::Undefined() because returning an empty handle will cause
693
+ // ToLocalChecked() to abort.
694
+ if (env ()->using_domains () && DomainExit (env (), object ())) {
711
695
return Undefined (env ()->isolate ());
712
696
}
713
697
0 commit comments