Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_wrap: AsyncWrapObject constructor template factory #33051

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ struct AsyncWrapObject : public AsyncWrap {
inline AsyncWrapObject(Environment* env, Local<Object> object,
ProviderType type) : AsyncWrap(env, object, type) {}

static Local<FunctionTemplate> GetConstructorTemplate(Environment* env) {
Local<FunctionTemplate> 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)
Expand Down Expand Up @@ -559,21 +573,10 @@ void AsyncWrap::Initialize(Local<Object> target,
env->set_async_hooks_promise_resolve_function(Local<Function>());
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();
}


Expand Down