Skip to content

Commit fbfbc83

Browse files
addaleaxjuanarbol
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 9e6dfe6 commit fbfbc83

32 files changed

Lines changed: 129 additions & 55 deletions

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
@@ -115,6 +115,10 @@ class ExternalReferenceRegistry;
115115

116116
class AsyncWrap : public BaseObject {
117117
public:
118+
enum InternalFields {
119+
kInternalFieldCount = BaseObject::kInternalFieldCount,
120+
};
121+
118122
enum ProviderType {
119123
#define V(PROVIDER) \
120124
PROVIDER_ ## PROVIDER,
@@ -228,6 +232,9 @@ class AsyncWrap : public BaseObject {
228232

229233
bool IsDoneInitializing() const override;
230234

235+
static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
236+
Environment* env, int internal_field_count = kInternalFieldCount);
237+
231238
private:
232239
ProviderType provider_type_ = PROVIDER_NONE;
233240
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
@@ -106,9 +106,9 @@ class BaseObject : public MemoryRetainer {
106106
// the `BaseObject*` pointer) and a constructor that initializes that field
107107
// to `nullptr`.
108108
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
109-
IsolateData* isolate);
109+
IsolateData* isolate, int internal_field_count = kInternalFieldCount);
110110
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
111-
Environment* env);
111+
Environment* env, int internal_field_count = kInternalFieldCount);
112112

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

src/cares_wrap.cc

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

2222-
Local<FunctionTemplate> aiw =
2223-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2224-
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2222+
Local<FunctionTemplate> aiw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
22252223
SetConstructorFunction(context, target, "GetAddrInfoReqWrap", aiw);
22262224

2227-
Local<FunctionTemplate> niw =
2228-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2229-
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2225+
Local<FunctionTemplate> niw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
22302226
SetConstructorFunction(context, target, "GetNameInfoReqWrap", niw);
22312227

2232-
Local<FunctionTemplate> qrw =
2233-
BaseObject::MakeLazilyInitializedJSTemplate(env);
2234-
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2228+
Local<FunctionTemplate> qrw = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
22352229
SetConstructorFunction(context, target, "QueryReqWrap", qrw);
22362230

22372231
Local<FunctionTemplate> channel_wrap =

src/crypto/crypto_keys.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
10811081
Local<FunctionTemplate> t =
10821082
NewFunctionTemplate(isolate, NativeKeyObject::New);
10831083
t->InstanceTemplate()->SetInternalFieldCount(
1084-
KeyObjectHandle::kInternalFieldCount);
1084+
NativeKeyObject::kInternalFieldCount);
10851085

10861086
Local<Value> ctor;
10871087
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
@@ -323,7 +323,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
323323
Isolate* isolate = env->isolate();
324324
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
325325

326-
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);
326+
t->InstanceTemplate()->SetInternalFieldCount(Sign::kInternalFieldCount);
327327

328328
SetProtoMethod(isolate, t, "init", SignInit);
329329
SetProtoMethod(isolate, t, "update", SignUpdate);
@@ -458,7 +458,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
458458
Isolate* isolate = env->isolate();
459459
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
460460

461-
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);
461+
t->InstanceTemplate()->SetInternalFieldCount(Verify::kInternalFieldCount);
462462

463463
SetProtoMethod(isolate, t, "init", VerifyInit);
464464
SetProtoMethod(isolate, t, "update", VerifyUpdate);

src/crypto/crypto_tls.cc

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

21872187
NODE_DEFINE_CONSTANT(target, HAVE_SSL_TRACE);
21882188

2189-
Local<FunctionTemplate> t = BaseObject::MakeLazilyInitializedJSTemplate(env);
2189+
Local<FunctionTemplate> t = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
21902190
Local<String> tlsWrapString =
21912191
FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap");
21922192
t->SetClassName(tlsWrapString);
2193-
t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
2193+
t->InstanceTemplate()->SetInternalFieldCount(TLSWrap::kInternalFieldCount);
21942194

21952195
Local<FunctionTemplate> get_write_queue_size =
21962196
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
@@ -405,7 +405,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
405405
v8::Local<v8::FunctionTemplate> job = NewFunctionTemplate(isolate, new_fn);
406406
job->Inherit(AsyncWrap::GetConstructorTemplate(env));
407407
job->InstanceTemplate()->SetInternalFieldCount(
408-
AsyncWrap::kInternalFieldCount);
408+
CryptoJob::kInternalFieldCount);
409409
SetProtoMethod(isolate, job, "run", Run);
410410
SetConstructorFunction(context, target, CryptoJobTraits::JobName, job);
411411
}

0 commit comments

Comments
 (0)