Skip to content

Commit 8059764

Browse files
joyeecheungRafaelGSS
authored andcommitted
src: use the internal field to determine if an object is a BaseObject
Instead of storing the function template of BaseObject for checking if an object is BaseObject by calling HasInstance, simply checks the first internal field of the object, which is always set in the BaseObject constructor. This is simpler and faster (there is now no need to iterate over the inheritance for the check). PR-URL: #47217 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 412d27b commit 8059764

28 files changed

+27
-79
lines changed

src/async_wrap.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
342342
tmpl = NewFunctionTemplate(isolate, nullptr);
343343
tmpl->SetClassName(
344344
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "AsyncWrap"));
345-
tmpl->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
346345
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
347346
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
348347
SetProtoMethod(

src/base_object-inl.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
3838
// while allowing to create a BaseObject in a vm context.
3939
}
4040

41-
// static
42-
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
43-
Environment* env) {
44-
return BaseObject::GetConstructorTemplate(env->isolate_data());
45-
}
46-
4741
void BaseObject::Detach() {
4842
CHECK_GT(pointer_data()->strong_ptr_count, 0);
4943
pointer_data()->is_detached = true;
@@ -76,14 +70,23 @@ Realm* BaseObject::realm() const {
7670
return realm_;
7771
}
7872

79-
void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
73+
bool BaseObject::IsBaseObject(v8::Local<v8::Object> obj) {
74+
if (obj->InternalFieldCount() < BaseObject::kInternalFieldCount) {
75+
return false;
76+
}
77+
void* ptr =
78+
obj->GetAlignedPointerFromInternalField(BaseObject::kEmbedderType);
79+
return ptr == &kNodeEmbedderId;
80+
}
81+
82+
void BaseObject::TagBaseObject(v8::Local<v8::Object> object) {
8083
DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
8184
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
8285
&kNodeEmbedderId);
8386
}
8487

8588
void BaseObject::SetInternalFields(v8::Local<v8::Object> object, void* slot) {
86-
TagNodeObject(object);
89+
TagBaseObject(object);
8790
object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot);
8891
}
8992

src/base_object.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
8989
IsolateData* isolate_data) {
9090
Local<FunctionTemplate> t = NewFunctionTemplate(
9191
isolate_data->isolate(), LazilyInitializedJSTemplateConstructor);
92-
t->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
9392
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
9493
return t;
9594
}
@@ -145,18 +144,6 @@ bool BaseObject::IsRootNode() const {
145144
return !persistent_handle_.IsWeak();
146145
}
147146

148-
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(
149-
IsolateData* isolate_data) {
150-
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template();
151-
if (tmpl.IsEmpty()) {
152-
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr);
153-
tmpl->SetClassName(
154-
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject"));
155-
isolate_data->set_base_object_ctor_template(tmpl);
156-
}
157-
return tmpl;
158-
}
159-
160147
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const {
161148
return IsWeakOrDetached();
162149
}

src/base_object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class BaseObject : public MemoryRetainer {
7676
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
7777
static inline void SetInternalFields(v8::Local<v8::Object> object,
7878
void* slot);
79-
static inline void TagNodeObject(v8::Local<v8::Object> object);
79+
static inline bool IsBaseObject(v8::Local<v8::Object> object);
80+
static inline void TagBaseObject(v8::Local<v8::Object> object);
8081
static void LazilyInitializedJSTemplateConstructor(
8182
const v8::FunctionCallbackInfo<v8::Value>& args);
8283
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);

src/crypto/crypto_cipher.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
277277

278278
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
279279

280-
t->InstanceTemplate()->SetInternalFieldCount(
281-
CipherBase::kInternalFieldCount);
282-
t->Inherit(BaseObject::GetConstructorTemplate(env));
280+
t->InstanceTemplate()->SetInternalFieldCount(CipherBase::kInternalFieldCount);
283281

284282
SetProtoMethod(isolate, t, "init", Init);
285283
SetProtoMethod(isolate, t, "initiv", InitIv);

src/crypto/crypto_context.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
268268
tmpl = NewFunctionTemplate(isolate, New);
269269
tmpl->InstanceTemplate()->SetInternalFieldCount(
270270
SecureContext::kInternalFieldCount);
271-
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
272271
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));
273272

274273
SetProtoMethod(isolate, tmpl, "init", Init);

src/crypto/crypto_dh.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
6969

7070
t->InstanceTemplate()->SetInternalFieldCount(
7171
DiffieHellman::kInternalFieldCount);
72-
t->Inherit(BaseObject::GetConstructorTemplate(env));
7372

7473
SetProtoMethod(isolate, t, "generateKeys", GenerateKeys);
7574
SetProtoMethod(isolate, t, "computeSecret", ComputeSecret);

src/crypto/crypto_ec.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
6666
Local<Context> context = env->context();
6767

6868
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
69-
t->Inherit(BaseObject::GetConstructorTemplate(env));
7069

7170
t->InstanceTemplate()->SetInternalFieldCount(ECDH::kInternalFieldCount);
7271

src/crypto/crypto_hash.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
5757
Local<Context> context = env->context();
5858
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
5959

60-
t->InstanceTemplate()->SetInternalFieldCount(
61-
Hash::kInternalFieldCount);
62-
t->Inherit(BaseObject::GetConstructorTemplate(env));
60+
t->InstanceTemplate()->SetInternalFieldCount(Hash::kInternalFieldCount);
6361

6462
SetProtoMethod(isolate, t, "update", HashUpdate);
6563
SetProtoMethod(isolate, t, "digest", HashDigest);

src/crypto/crypto_hmac.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
4141
Isolate* isolate = env->isolate();
4242
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
4343

44-
t->InstanceTemplate()->SetInternalFieldCount(
45-
Hmac::kInternalFieldCount);
46-
t->Inherit(BaseObject::GetConstructorTemplate(env));
44+
t->InstanceTemplate()->SetInternalFieldCount(Hmac::kInternalFieldCount);
4745

4846
SetProtoMethod(isolate, t, "init", HmacInit);
4947
SetProtoMethod(isolate, t, "update", HmacUpdate);

0 commit comments

Comments
 (0)