Skip to content

Commit d8ea1aa

Browse files
addaleaxaduh95
authored andcommitted
src: make AsyncWrap subclass internal field counts explicit
This helps ensure that we already set the correct number of internal fields when creating objects, even if the number of internal fields of e.g. AsyncWrap changes over time. PR-URL: #62103 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 0b15244 commit d8ea1aa

32 files changed

+129
-55
lines changed

src/async_wrap-inl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ inline v8::Local<v8::FunctionTemplate> AsyncWrap::GetConstructorTemplate(
8989
return GetConstructorTemplate(env->isolate_data());
9090
}
9191

92+
// static
93+
v8::Local<v8::FunctionTemplate> AsyncWrap::MakeLazilyInitializedJSTemplate(
94+
Environment* env, int internal_field_count) {
95+
v8::Local<v8::FunctionTemplate> t =
96+
BaseObject::MakeLazilyInitializedJSTemplate(env, internal_field_count);
97+
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
98+
return t;
99+
}
100+
92101
} // namespace node
93102

94103
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/async_wrap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class ExternalReferenceRegistry;
118118

119119
class AsyncWrap : public BaseObject {
120120
public:
121+
enum InternalFields {
122+
kInternalFieldCount = BaseObject::kInternalFieldCount,
123+
};
124+
121125
enum ProviderType {
122126
#define V(PROVIDER) \
123127
PROVIDER_ ## PROVIDER,
@@ -231,6 +235,9 @@ class AsyncWrap : public BaseObject {
231235

232236
bool IsDoneInitializing() const override;
233237

238+
static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
239+
Environment* env, int internal_field_count = kInternalFieldCount);
240+
234241
private:
235242
ProviderType provider_type_ = PROVIDER_NONE;
236243
bool init_hook_ran_ = false;

src/base_object.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ void BaseObject::LazilyInitializedJSTemplateConstructor(
8181
}
8282

8383
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
84-
Environment* env) {
85-
return MakeLazilyInitializedJSTemplate(env->isolate_data());
84+
Environment* env, int internal_field_count) {
85+
return MakeLazilyInitializedJSTemplate(env->isolate_data(),
86+
internal_field_count);
8687
}
8788

8889
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
89-
IsolateData* isolate_data) {
90+
IsolateData* isolate_data, int internal_field_count) {
9091
Local<FunctionTemplate> t = NewFunctionTemplate(
9192
isolate_data->isolate(), LazilyInitializedJSTemplateConstructor);
92-
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
93+
t->InstanceTemplate()->SetInternalFieldCount(internal_field_count);
9394
return t;
9495
}
9596

src/base_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ class BaseObject : public MemoryRetainer {
109109
// the `BaseObject*` pointer) and a constructor that initializes that field
110110
// to `nullptr`.
111111
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
112-
IsolateData* isolate);
112+
IsolateData* isolate, int internal_field_count = kInternalFieldCount);
113113
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
114-
Environment* env);
114+
Environment* env, int internal_field_count = kInternalFieldCount);
115115

116116
// Setter/Getter pair for internal fields that can be passed to SetAccessor.
117117
template <int Field>

src/cares_wrap.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,19 +2296,13 @@ void Initialize(Local<Object> target,
22962296
NODE_DEFINE_CONSTANT(target, DNS_ORDER_IPV4_FIRST);
22972297
NODE_DEFINE_CONSTANT(target, DNS_ORDER_IPV6_FIRST);
22982298

2299-
Local<FunctionTemplate> aiw =
2300-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2301-
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2299+
Local<FunctionTemplate> aiw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
23022300
SetConstructorFunction(context, target, "GetAddrInfoReqWrap", aiw);
23032301

2304-
Local<FunctionTemplate> niw =
2305-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2306-
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2302+
Local<FunctionTemplate> niw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
23072303
SetConstructorFunction(context, target, "GetNameInfoReqWrap", niw);
23082304

2309-
Local<FunctionTemplate> qrw =
2310-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2311-
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2305+
Local<FunctionTemplate> qrw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
23122306
SetConstructorFunction(context, target, "QueryReqWrap", qrw);
23132307

23142308
Local<FunctionTemplate> channel_wrap =

src/crypto/crypto_keys.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
16141614
Local<FunctionTemplate> t =
16151615
NewFunctionTemplate(isolate, NativeKeyObject::New);
16161616
t->InstanceTemplate()->SetInternalFieldCount(
1617-
KeyObjectHandle::kInternalFieldCount);
1617+
NativeKeyObject::kInternalFieldCount);
16181618

16191619
Local<Value> ctor;
16201620
if (!t->GetFunction(env->context()).ToLocal(&ctor))

src/crypto/crypto_sig.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
310310
Isolate* isolate = env->isolate();
311311
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
312312

313-
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);
313+
t->InstanceTemplate()->SetInternalFieldCount(Sign::kInternalFieldCount);
314314

315315
SetProtoMethod(isolate, t, "init", SignInit);
316316
SetProtoMethod(isolate, t, "update", SignUpdate);
@@ -437,7 +437,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
437437
Isolate* isolate = env->isolate();
438438
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
439439

440-
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);
440+
t->InstanceTemplate()->SetInternalFieldCount(Verify::kInternalFieldCount);
441441

442442
SetProtoMethod(isolate, t, "init", VerifyInit);
443443
SetProtoMethod(isolate, t, "update", VerifyUpdate);

src/crypto/crypto_tls.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,11 +2172,11 @@ void TLSWrap::Initialize(
21722172

21732173
NODE_DEFINE_CONSTANT(target, HAVE_SSL_TRACE);
21742174

2175-
Local<FunctionTemplate> t = BaseObject::MakeLazilyInitializedJSTemplate(env);
2175+
Local<FunctionTemplate> t = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
21762176
Local<String> tlsWrapString =
21772177
FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap");
21782178
t->SetClassName(tlsWrapString);
2179-
t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
2179+
t->InstanceTemplate()->SetInternalFieldCount(TLSWrap::kInternalFieldCount);
21802180

21812181
Local<FunctionTemplate> get_write_queue_size =
21822182
FunctionTemplate::New(env->isolate(),

src/crypto/crypto_tls.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class TLSWrap : public AsyncWrap,
4343
public StreamBase,
4444
public StreamListener {
4545
public:
46+
enum InternalFields {
47+
kInternalFieldCount = std::max<uint32_t>(AsyncWrap::kInternalFieldCount,
48+
StreamBase::kInternalFieldCount),
49+
};
50+
4651
enum class Kind {
4752
kClient,
4853
kServer

src/crypto/crypto_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
348348
v8::Local<v8::FunctionTemplate> job = NewFunctionTemplate(isolate, new_fn);
349349
job->Inherit(AsyncWrap::GetConstructorTemplate(env));
350350
job->InstanceTemplate()->SetInternalFieldCount(
351-
AsyncWrap::kInternalFieldCount);
351+
CryptoJob::kInternalFieldCount);
352352
SetProtoMethod(isolate, job, "run", Run);
353353
SetConstructorFunction(context, target, CryptoJobTraits::JobName, job);
354354
}

0 commit comments

Comments
 (0)