Skip to content

Commit 59b64ad

Browse files
QardBethGriggs
authored andcommitted
src: add AsyncWrapObject constructor template factory
PR-URL: #33051 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 401ab61 commit 59b64ad

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/async_wrap.cc

+18-15
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)