Skip to content

Commit

Permalink
quic: set up FunctionTemplates more cleanly
Browse files Browse the repository at this point in the history
- Use `kInternalFieldCount`, similar to 0fac393.
- Always inherit from the correct parent template, similar to 8a7201b.
- Always provide a template class name.

PR-URL: #33968
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and jasnell committed Jun 22, 2020
1 parent df144d2 commit 1b7434d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/quic/node_quic_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3723,7 +3723,7 @@ void QuicSession::Initialize(
session->SetClassName(class_name);
session->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> sessiont = session->InstanceTemplate();
sessiont->SetInternalFieldCount(1);
sessiont->SetInternalFieldCount(QuicSession::kInternalFieldCount);
sessiont->Set(env->owner_symbol(), Null(env->isolate()));
AddMethods(env, session);
env->set_quicserversession_instance_template(sessiont);
Expand All @@ -3736,7 +3736,7 @@ void QuicSession::Initialize(
session->SetClassName(class_name);
session->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> sessiont = session->InstanceTemplate();
sessiont->SetInternalFieldCount(1);
sessiont->SetInternalFieldCount(QuicSession::kInternalFieldCount);
sessiont->Set(env->owner_symbol(), Null(env->isolate()));
AddMethods(env, session);
env->SetProtoMethod(session,
Expand Down
16 changes: 11 additions & 5 deletions src/quic/node_quic_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,10 @@ void QuicEndpoint::Initialize(
Isolate* isolate = env->isolate();
Local<String> class_name = FIXED_ONE_BYTE_STRING(isolate, "QuicEndpoint");
Local<FunctionTemplate> endpoint = env->NewFunctionTemplate(NewQuicEndpoint);
endpoint->Inherit(BaseObject::GetConstructorTemplate(env));
endpoint->SetClassName(class_name);
endpoint->InstanceTemplate()->SetInternalFieldCount(1);
endpoint->InstanceTemplate()->SetInternalFieldCount(
QuicEndpoint::kInternalFieldCount);
env->SetProtoMethod(endpoint,
"waitForPendingCallbacks",
QuicEndpointWaitForPendingCallbacks);
Expand All @@ -1142,8 +1144,10 @@ void QuicSocket::Initialize(
Isolate* isolate = env->isolate();
Local<String> class_name = FIXED_ONE_BYTE_STRING(isolate, "QuicSocket");
Local<FunctionTemplate> socket = env->NewFunctionTemplate(NewQuicSocket);
socket->Inherit(AsyncWrap::GetConstructorTemplate(env));
socket->SetClassName(class_name);
socket->InstanceTemplate()->SetInternalFieldCount(1);
socket->InstanceTemplate()->SetInternalFieldCount(
QuicSocket::kInternalFieldCount);
socket->InstanceTemplate()->Set(env->owner_symbol(), Null(isolate));
env->SetProtoMethod(socket,
"addEndpoint",
Expand All @@ -1170,9 +1174,11 @@ void QuicSocket::Initialize(
target->Set(context, class_name,
socket->GetFunction(env->context()).ToLocalChecked()).FromJust();

// TODO(addaleax): None of these templates actually are constructor templates.
Local<ObjectTemplate> sendwrap_template = ObjectTemplate::New(isolate);
sendwrap_template->SetInternalFieldCount(1);
Local<FunctionTemplate> sendwrap_ctor = FunctionTemplate::New(isolate);
sendwrap_ctor->Inherit(AsyncWrap::GetConstructorTemplate(env));
sendwrap_ctor->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "SendWrap"));
Local<ObjectTemplate> sendwrap_template = sendwrap_ctor->InstanceTemplate();
sendwrap_template->SetInternalFieldCount(SendWrap::kInternalFieldCount);
env->set_quicsocketsendwrap_instance_template(sendwrap_template);
}

Expand Down

0 comments on commit 1b7434d

Please sign in to comment.