@@ -176,7 +176,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
176
176
class PromiseWrap : public AsyncWrap {
177
177
public:
178
178
PromiseWrap (Environment* env, Local<Object> object, bool silent)
179
- : AsyncWrap(env, object, PROVIDER_PROMISE, - 1 , silent) {
179
+ : AsyncWrap(env, object, PROVIDER_PROMISE, kInvalidAsyncId , silent) {
180
180
MakeWeak ();
181
181
}
182
182
@@ -382,7 +382,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
382
382
383
383
void AsyncWrap::GetAsyncId (const FunctionCallbackInfo<Value>& args) {
384
384
AsyncWrap* wrap;
385
- args.GetReturnValue ().Set (- 1 );
385
+ args.GetReturnValue ().Set (kInvalidAsyncId );
386
386
ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
387
387
args.GetReturnValue ().Set (wrap->get_async_id ());
388
388
}
@@ -409,10 +409,15 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
409
409
AsyncWrap* wrap;
410
410
ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
411
411
double execution_async_id =
412
- args[0 ]->IsNumber () ? args[0 ].As <Number>()->Value () : - 1 ;
412
+ args[0 ]->IsNumber () ? args[0 ].As <Number>()->Value () : kInvalidAsyncId ;
413
413
wrap->AsyncReset (execution_async_id);
414
414
}
415
415
416
+ void AsyncWrap::EmitDestroy () {
417
+ AsyncWrap::EmitDestroy (env (), async_id_);
418
+ // Ensure no double destroy is emitted via AsyncReset().
419
+ async_id_ = kInvalidAsyncId ;
420
+ }
416
421
417
422
void AsyncWrap::QueueDestroyAsyncId (const FunctionCallbackInfo<Value>& args) {
418
423
CHECK (args[0 ]->IsNumber ());
@@ -474,7 +479,7 @@ void AsyncWrap::Initialize(Local<Object> target,
474
479
// kDefaultTriggerAsyncId: Write the id of the resource responsible for a
475
480
// handle's creation just before calling the new handle's constructor.
476
481
// After the new handle is constructed kDefaultTriggerAsyncId is set back
477
- // to -1 .
482
+ // to kInvalidAsyncId .
478
483
FORCE_SET_TARGET_FIELD (target,
479
484
" async_id_fields" ,
480
485
env->async_hooks ()->async_id_fields ().GetJSArray ());
@@ -558,15 +563,15 @@ AsyncWrap::AsyncWrap(Environment* env,
558
563
CHECK_NE (provider, PROVIDER_NONE);
559
564
CHECK_GE (object->InternalFieldCount (), 1 );
560
565
561
- async_id_ = - 1 ;
566
+ async_id_ = kInvalidAsyncId ;
562
567
// Use AsyncReset() call to execute the init() callbacks.
563
568
AsyncReset (execution_async_id, silent);
564
569
}
565
570
566
571
567
572
AsyncWrap::~AsyncWrap () {
568
573
EmitTraceEventDestroy ();
569
- EmitDestroy (env (), get_async_id () );
574
+ EmitDestroy ();
570
575
}
571
576
572
577
void AsyncWrap::EmitTraceEventDestroy () {
@@ -602,16 +607,16 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
602
607
// and reused over their lifetime. This way a new uid can be assigned when
603
608
// the resource is pulled out of the pool and put back into use.
604
609
void AsyncWrap::AsyncReset (double execution_async_id, bool silent) {
605
- if (async_id_ != - 1 ) {
610
+ if (async_id_ != kInvalidAsyncId ) {
606
611
// This instance was in use before, we have already emitted an init with
607
612
// its previous async_id and need to emit a matching destroy for that
608
613
// before generating a new async_id.
609
- EmitDestroy (env (), async_id_ );
614
+ EmitDestroy ();
610
615
}
611
616
612
617
// Now we can assign a new async_id_ to this instance.
613
- async_id_ =
614
- execution_async_id == - 1 ? env ()-> new_async_id () : execution_async_id;
618
+ async_id_ = execution_async_id == kInvalidAsyncId ? env ()-> new_async_id ()
619
+ : execution_async_id;
615
620
trigger_async_id_ = env ()->get_default_trigger_async_id ();
616
621
617
622
switch (provider_type ()) {
@@ -693,7 +698,7 @@ async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
693
698
// Environment::GetCurrent() allocates a Local<> handle.
694
699
HandleScope handle_scope (isolate);
695
700
Environment* env = Environment::GetCurrent (isolate);
696
- if (env == nullptr ) return - 1 ;
701
+ if (env == nullptr ) return AsyncWrap:: kInvalidAsyncId ;
697
702
return env->execution_async_id ();
698
703
}
699
704
@@ -702,7 +707,7 @@ async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
702
707
// Environment::GetCurrent() allocates a Local<> handle.
703
708
HandleScope handle_scope (isolate);
704
709
Environment* env = Environment::GetCurrent (isolate);
705
- if (env == nullptr ) return - 1 ;
710
+ if (env == nullptr ) return AsyncWrap:: kInvalidAsyncId ;
706
711
return env->trigger_async_id ();
707
712
}
708
713
@@ -727,7 +732,7 @@ async_context EmitAsyncInit(Isolate* isolate,
727
732
CHECK_NOT_NULL (env);
728
733
729
734
// Initialize async context struct
730
- if (trigger_async_id == - 1 )
735
+ if (trigger_async_id == AsyncWrap:: kInvalidAsyncId )
731
736
trigger_async_id = env->get_default_trigger_async_id ();
732
737
733
738
async_context context = {
0 commit comments