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