From 62f29534de2382f80e83865bac9da57b96476c73 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Fri, 24 Apr 2020 17:02:07 -0700 Subject: [PATCH] src: add AsyncWrapObject constructor template factory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33051 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Gerhard Stöbich Reviewed-By: David Carlier Reviewed-By: Juan José Arboleda --- src/async_wrap.cc | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index b24c160156c280..42837e09818ec2 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -80,6 +80,20 @@ struct AsyncWrapObject : public AsyncWrap { inline AsyncWrapObject(Environment* env, Local object, ProviderType type) : AsyncWrap(env, object, type) {} + static Local GetConstructorTemplate(Environment* env) { + Local tmpl = env->async_wrap_object_ctor_template(); + if (tmpl.IsEmpty()) { + tmpl = env->NewFunctionTemplate(AsyncWrapObject::New); + tmpl->SetClassName( + FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap")); + tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); + tmpl->InstanceTemplate()->SetInternalFieldCount( + AsyncWrapObject::kInternalFieldCount); + env->set_async_wrap_object_ctor_template(tmpl); + } + return tmpl; + } + SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(AsyncWrapObject) SET_SELF_SIZE(AsyncWrapObject) @@ -559,21 +573,10 @@ void AsyncWrap::Initialize(Local target, env->set_async_hooks_promise_resolve_function(Local()); env->set_async_hooks_binding(target); - // TODO(addaleax): This block might better work as a - // AsyncWrapObject::Initialize() or AsyncWrapObject::GetConstructorTemplate() - // function. - { - auto class_name = FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"); - auto function_template = env->NewFunctionTemplate(AsyncWrapObject::New); - function_template->SetClassName(class_name); - function_template->Inherit(AsyncWrap::GetConstructorTemplate(env)); - auto instance_template = function_template->InstanceTemplate(); - instance_template->SetInternalFieldCount(AsyncWrap::kInternalFieldCount); - auto function = - function_template->GetFunction(env->context()).ToLocalChecked(); - target->Set(env->context(), class_name, function).Check(); - env->set_async_wrap_object_ctor_template(function_template); - } + target->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"), + AsyncWrapObject::GetConstructorTemplate(env) + ->GetFunction(env->context()).ToLocalChecked()).Check(); }