@@ -64,7 +64,7 @@ struct AsyncWrapObject : public AsyncWrap {
64
64
static inline void New (const FunctionCallbackInfo<Value>& args) {
65
65
Environment* env = Environment::GetCurrent (args);
66
66
CHECK (args.IsConstructCall ());
67
- CHECK (env->async_wrap_constructor_template ()->HasInstance (args.This ()));
67
+ CHECK (env->async_wrap_object_ctor_template ()->HasInstance (args.This ()));
68
68
CHECK (args[0 ]->IsUint32 ());
69
69
auto type = static_cast <ProviderType>(args[0 ].As <Uint32>()->Value ());
70
70
new AsyncWrapObject (env, args.This (), type);
@@ -424,12 +424,16 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo<Value>& args) {
424
424
args[0 ].As <Number>()->Value ());
425
425
}
426
426
427
- void AsyncWrap::AddWrapMethods (Environment* env,
428
- Local<FunctionTemplate> constructor,
429
- int flag) {
430
- env->SetProtoMethod (constructor, " getAsyncId" , AsyncWrap::GetAsyncId);
431
- if (flag & kFlagHasReset )
432
- env->SetProtoMethod (constructor, " asyncReset" , AsyncWrap::AsyncReset);
427
+ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate (Environment* env) {
428
+ Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template ();
429
+ if (tmpl.IsEmpty ()) {
430
+ tmpl = env->NewFunctionTemplate (nullptr );
431
+ tmpl->SetClassName (FIXED_ONE_BYTE_STRING (env->isolate (), " AsyncWrap" ));
432
+ env->SetProtoMethod (tmpl, " getAsyncId" , AsyncWrap::GetAsyncId);
433
+ env->SetProtoMethod (tmpl, " asyncReset" , AsyncWrap::AsyncReset);
434
+ env->set_async_wrap_ctor_template (tmpl);
435
+ }
436
+ return tmpl;
433
437
}
434
438
435
439
void AsyncWrap::Initialize (Local<Object> target,
@@ -525,17 +529,20 @@ void AsyncWrap::Initialize(Local<Object> target,
525
529
env->set_async_hooks_promise_resolve_function (Local<Function>());
526
530
env->set_async_hooks_binding (target);
527
531
532
+ // TODO(addaleax): This block might better work as a
533
+ // AsyncWrapObject::Initialize() or AsyncWrapObject::GetConstructorTemplate()
534
+ // function.
528
535
{
529
536
auto class_name = FIXED_ONE_BYTE_STRING (env->isolate (), " AsyncWrap" );
530
537
auto function_template = env->NewFunctionTemplate (AsyncWrapObject::New);
531
538
function_template->SetClassName (class_name);
532
- AsyncWrap::AddWrapMethods (env, function_template );
539
+ function_template-> Inherit ( AsyncWrap::GetConstructorTemplate (env) );
533
540
auto instance_template = function_template->InstanceTemplate ();
534
541
instance_template->SetInternalFieldCount (1 );
535
542
auto function =
536
543
function_template->GetFunction (env->context ()).ToLocalChecked ();
537
544
target->Set (env->context (), class_name, function).FromJust ();
538
- env->set_async_wrap_constructor_template (function_template);
545
+ env->set_async_wrap_object_ctor_template (function_template);
539
546
}
540
547
}
541
548
0 commit comments