@@ -80,6 +80,20 @@ struct AsyncWrapObject : public AsyncWrap {
8080 inline AsyncWrapObject (Environment* env, Local<Object> object,
8181 ProviderType type) : AsyncWrap(env, object, type) {}
8282
83+ static Local<FunctionTemplate> GetConstructorTemplate (Environment* env) {
84+ Local<FunctionTemplate> tmpl = env->async_wrap_object_ctor_template ();
85+ if (tmpl.IsEmpty ()) {
86+ tmpl = env->NewFunctionTemplate (AsyncWrapObject::New);
87+ tmpl->SetClassName (
88+ FIXED_ONE_BYTE_STRING (env->isolate (), " AsyncWrap" ));
89+ tmpl->Inherit (AsyncWrap::GetConstructorTemplate (env));
90+ tmpl->InstanceTemplate ()->SetInternalFieldCount (
91+ AsyncWrapObject::kInternalFieldCount );
92+ env->set_async_wrap_object_ctor_template (tmpl);
93+ }
94+ return tmpl;
95+ }
96+
8397 SET_NO_MEMORY_INFO ()
8498 SET_MEMORY_INFO_NAME (AsyncWrapObject)
8599 SET_SELF_SIZE (AsyncWrapObject)
@@ -559,21 +573,10 @@ void AsyncWrap::Initialize(Local<Object> target,
559573 env->set_async_hooks_promise_resolve_function (Local<Function>());
560574 env->set_async_hooks_binding (target);
561575
562- // TODO(addaleax): This block might better work as a
563- // AsyncWrapObject::Initialize() or AsyncWrapObject::GetConstructorTemplate()
564- // function.
565- {
566- auto class_name = FIXED_ONE_BYTE_STRING (env->isolate (), " AsyncWrap" );
567- auto function_template = env->NewFunctionTemplate (AsyncWrapObject::New);
568- function_template->SetClassName (class_name);
569- function_template->Inherit (AsyncWrap::GetConstructorTemplate (env));
570- auto instance_template = function_template->InstanceTemplate ();
571- instance_template->SetInternalFieldCount (AsyncWrap::kInternalFieldCount );
572- auto function =
573- function_template->GetFunction (env->context ()).ToLocalChecked ();
574- target->Set (env->context (), class_name, function).Check ();
575- env->set_async_wrap_object_ctor_template (function_template);
576- }
576+ target->Set (env->context (),
577+ FIXED_ONE_BYTE_STRING (env->isolate (), " AsyncWrap" ),
578+ AsyncWrapObject::GetConstructorTemplate (env)
579+ ->GetFunction (env->context ()).ToLocalChecked ()).Check ();
577580}
578581
579582
0 commit comments