@@ -80,6 +80,20 @@ struct AsyncWrapObject : public AsyncWrap {
80
80
inline AsyncWrapObject (Environment* env, Local<Object> object,
81
81
ProviderType type) : AsyncWrap(env, object, type) {}
82
82
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
+
83
97
SET_NO_MEMORY_INFO ()
84
98
SET_MEMORY_INFO_NAME (AsyncWrapObject)
85
99
SET_SELF_SIZE (AsyncWrapObject)
@@ -559,21 +573,10 @@ void AsyncWrap::Initialize(Local<Object> target,
559
573
env->set_async_hooks_promise_resolve_function (Local<Function>());
560
574
env->set_async_hooks_binding (target);
561
575
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 ();
577
580
}
578
581
579
582
0 commit comments