Skip to content

Commit

Permalink
src: use the internal field to determine if an object is a BaseObject
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
joyeecheung committed Mar 22, 2023
1 parent 1a18b44 commit 7e1ecdf
Show file tree
Hide file tree
Showing 27 changed files with 23 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
tmpl = NewFunctionTemplate(isolate, nullptr);
tmpl->SetClassName(
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "AsyncWrap"));
tmpl->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
SetProtoMethod(
Expand Down
15 changes: 9 additions & 6 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
// while allowing to create a BaseObject in a vm context.
}

// static
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
Environment* env) {
return BaseObject::GetConstructorTemplate(env->isolate_data());
}

void BaseObject::Detach() {
CHECK_GT(pointer_data()->strong_ptr_count, 0);
pointer_data()->is_detached = true;
Expand Down Expand Up @@ -76,6 +70,15 @@ Realm* BaseObject::realm() const {
return realm_;
}

bool BaseObject::IsBaseObject(v8::Local<v8::Object> obj) {
if (obj->InternalFieldCount() < BaseObject::kInternalFieldCount) {
return false;
}
void* ptr =
obj->GetAlignedPointerFromInternalField(BaseObject::kEmbedderType);
return ptr == &kNodeEmbedderId;
}

void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
Expand Down
13 changes: 0 additions & 13 deletions src/base_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
IsolateData* isolate_data) {
Local<FunctionTemplate> t = NewFunctionTemplate(
isolate_data->isolate(), LazilyInitializedJSTemplateConstructor);
t->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
return t;
}
Expand Down Expand Up @@ -145,18 +144,6 @@ bool BaseObject::IsRootNode() const {
return !persistent_handle_.IsWeak();
}

Local<FunctionTemplate> BaseObject::GetConstructorTemplate(
IsolateData* isolate_data) {
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template();
if (tmpl.IsEmpty()) {
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr);
tmpl->SetClassName(
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject"));
isolate_data->set_base_object_ctor_template(tmpl);
}
return tmpl;
}

bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const {
return IsWeakOrDetached();
}
Expand Down
1 change: 1 addition & 0 deletions src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class BaseObject : public MemoryRetainer {
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
static inline void SetInternalFields(v8::Local<v8::Object> object,
void* slot);
static inline bool IsBaseObject(v8::Local<v8::Object> object);
static inline void TagNodeObject(v8::Local<v8::Object> object);
static void LazilyInitializedJSTemplateConstructor(
const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {

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

t->InstanceTemplate()->SetInternalFieldCount(
CipherBase::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(CipherBase::kInternalFieldCount);

SetProtoMethod(isolate, t, "init", Init);
SetProtoMethod(isolate, t, "initiv", InitIv);
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
tmpl = NewFunctionTemplate(isolate, New);
tmpl->InstanceTemplate()->SetInternalFieldCount(
SecureContext::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));

SetProtoMethod(isolate, tmpl, "init", Init);
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {

t->InstanceTemplate()->SetInternalFieldCount(
DiffieHellman::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

SetProtoMethod(isolate, t, "generateKeys", GenerateKeys);
SetProtoMethod(isolate, t, "computeSecret", ComputeSecret);
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
Local<Context> context = env->context();

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

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

Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
Local<Context> context = env->context();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);

t->InstanceTemplate()->SetInternalFieldCount(
Hash::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(Hash::kInternalFieldCount);

SetProtoMethod(isolate, t, "update", HashUpdate);
SetProtoMethod(isolate, t, "digest", HashDigest);
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);

t->InstanceTemplate()->SetInternalFieldCount(
Hmac::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(Hmac::kInternalFieldCount);

SetProtoMethod(isolate, t, "init", HmacInit);
SetProtoMethod(isolate, t, "update", HmacUpdate);
Expand Down
2 changes: 0 additions & 2 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,6 @@ v8::Local<v8::Function> KeyObjectHandle::Initialize(Environment* env) {
templ = NewFunctionTemplate(isolate, New);
templ->InstanceTemplate()->SetInternalFieldCount(
KeyObjectHandle::kInternalFieldCount);
templ->Inherit(BaseObject::GetConstructorTemplate(env));

SetProtoMethod(isolate, templ, "init", Init);
SetProtoMethodNoSideEffect(
Expand Down Expand Up @@ -1342,7 +1341,6 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
NewFunctionTemplate(isolate, NativeKeyObject::New);
t->InstanceTemplate()->SetInternalFieldCount(
KeyObjectHandle::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

Local<Value> ctor;
if (!t->GetFunction(env->context()).ToLocal(&ctor))
Expand Down
8 changes: 2 additions & 6 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);

t->InstanceTemplate()->SetInternalFieldCount(
SignBase::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);

SetProtoMethod(isolate, t, "init", SignInit);
SetProtoMethod(isolate, t, "update", SignUpdate);
Expand Down Expand Up @@ -459,9 +457,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);

t->InstanceTemplate()->SetInternalFieldCount(
SignBase::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(SignBase::kInternalFieldCount);

SetProtoMethod(isolate, t, "init", VerifyInit);
SetProtoMethod(isolate, t, "update", VerifyUpdate);
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_x509.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
tmpl = NewFunctionTemplate(isolate, nullptr);
tmpl->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "X509Certificate"));
SetProtoMethod(isolate, tmpl, "subject", Subject);
Expand Down
1 change: 0 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ void IsolateData::CreateProperties() {
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
templ->Inherit(BaseObject::GetConstructorTemplate(this));
set_binding_data_ctor_template(templ);
binding::CreateInternalBindingTemplates(this);

Expand Down
1 change: 0 additions & 1 deletion src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@
#define PER_ISOLATE_TEMPLATE_PROPERTIES(V) \
V(async_wrap_ctor_template, v8::FunctionTemplate) \
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
V(base_object_ctor_template, v8::FunctionTemplate) \
V(binding_data_ctor_template, v8::FunctionTemplate) \
V(blob_constructor_template, v8::FunctionTemplate) \
V(blob_reader_constructor_template, v8::FunctionTemplate) \
Expand Down
1 change: 0 additions & 1 deletion src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ Local<FunctionTemplate> HistogramBase::GetConstructorTemplate(
tmpl = NewFunctionTemplate(isolate, New);
Local<String> classname = FIXED_ONE_BYTE_STRING(isolate, "Histogram");
tmpl->SetClassName(classname);
tmpl->Inherit(BaseObject::GetConstructorTemplate(isolate_data));

tmpl->InstanceTemplate()->SetInternalFieldCount(
HistogramBase::kInternalFieldCount);
Expand Down
1 change: 0 additions & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ void ModuleWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> tpl = NewFunctionTemplate(isolate, New);
tpl->InstanceTemplate()->SetInternalFieldCount(
ModuleWrap::kInternalFieldCount);
tpl->Inherit(BaseObject::GetConstructorTemplate(env));

SetProtoMethod(isolate, tpl, "link", Link);
SetProtoMethod(isolate, tpl, "instantiate", Instantiate);
Expand Down
1 change: 0 additions & 1 deletion src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ void CreateInternalBindingTemplates(IsolateData* isolate_data) {
FunctionTemplate::New(isolate_data->isolate()); \
templ->InstanceTemplate()->SetInternalFieldCount( \
BaseObject::kInternalFieldCount); \
templ->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); \
_register_isolate_##modname(isolate_data, templ); \
isolate_data->set_##modname##_binding(templ); \
} while (0);
Expand Down
2 changes: 0 additions & 2 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
tmpl = NewFunctionTemplate(isolate, nullptr);
tmpl->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "Blob"));
SetProtoMethod(isolate, tmpl, "getReader", GetReader);
Expand Down Expand Up @@ -281,7 +280,6 @@ Local<FunctionTemplate> Blob::Reader::GetConstructorTemplate(Environment* env) {
tmpl = NewFunctionTemplate(isolate, nullptr);
tmpl->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BlobReader"));
SetProtoMethod(env->isolate(), tmpl, "pull", Pull);
env->set_blob_reader_constructor_template(tmpl);
Expand Down
1 change: 0 additions & 1 deletion src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
// ConverterObject
{
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, nullptr);
t->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
t->InstanceTemplate()->SetInternalFieldCount(
ConverterObject::kInternalFieldCount);
Local<String> converter_string =
Expand Down
15 changes: 8 additions & 7 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
}

Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object) override {
if (env_->base_object_ctor_template()->HasInstance(object)) {
if (BaseObject::IsBaseObject(object)) {
return WriteHostObject(
BaseObjectPtr<BaseObject> { Unwrap<BaseObject>(object) });
}
Expand Down Expand Up @@ -490,7 +490,8 @@ Maybe<bool> Message::Serialize(Environment* env,
array_buffers.push_back(ab);
serializer.TransferArrayBuffer(id, ab);
continue;
} else if (env->base_object_ctor_template()->HasInstance(entry)) {
} else if (entry->IsObject() &&
BaseObject::IsBaseObject(entry.As<Object>())) {
// Check if the source MessagePort is being transferred.
if (!source_port.IsEmpty() && entry == source_port) {
ThrowDataCloneException(
Expand Down Expand Up @@ -1257,7 +1258,7 @@ JSTransferable::NestedTransferables() const {
Local<Value> value;
if (!list->Get(context, i).ToLocal(&value))
return Nothing<BaseObjectList>();
if (env()->base_object_ctor_template()->HasInstance(value))
if (value->IsObject() && BaseObject::IsBaseObject(value.As<Object>()))
ret.emplace_back(Unwrap<BaseObject>(value));
}
return Just(ret);
Expand Down Expand Up @@ -1309,9 +1310,10 @@ BaseObjectPtr<BaseObject> JSTransferable::Data::Deserialize(

Local<Value> ret;
CHECK(!env->messaging_deserialize_create_object().IsEmpty());
if (!env->messaging_deserialize_create_object()->Call(
context, Null(env->isolate()), 1, &info).ToLocal(&ret) ||
!env->base_object_ctor_template()->HasInstance(ret)) {
if (!env->messaging_deserialize_create_object()
->Call(context, Null(env->isolate()), 1, &info)
.ToLocal(&ret) ||
!ret->IsObject() || !BaseObject::IsBaseObject(ret.As<Object>())) {
return {};
}

Expand Down Expand Up @@ -1492,7 +1494,6 @@ static void InitMessaging(Local<Object> target,
{
Local<FunctionTemplate> t =
NewFunctionTemplate(isolate, JSTransferable::New);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
JSTransferable::kInternalFieldCount);
SetConstructorFunction(context, target, "JSTransferable", t);
Expand Down
2 changes: 0 additions & 2 deletions src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ void Initialize(Local<Object> target,

ser->InstanceTemplate()->SetInternalFieldCount(
SerializerContext::kInternalFieldCount);
ser->Inherit(BaseObject::GetConstructorTemplate(env));

SetProtoMethod(isolate, ser, "writeHeader", SerializerContext::WriteHeader);
SetProtoMethod(isolate, ser, "writeValue", SerializerContext::WriteValue);
Expand Down Expand Up @@ -490,7 +489,6 @@ void Initialize(Local<Object> target,

des->InstanceTemplate()->SetInternalFieldCount(
DeserializerContext::kInternalFieldCount);
des->Inherit(BaseObject::GetConstructorTemplate(env));

SetProtoMethod(isolate, des, "readHeader", DeserializerContext::ReadHeader);
SetProtoMethod(isolate, des, "readValue", DeserializerContext::ReadValue);
Expand Down
2 changes: 0 additions & 2 deletions src/node_sockaddr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ Local<FunctionTemplate> SocketAddressBlockListWrap::GetConstructorTemplate(
Isolate* isolate = env->isolate();
tmpl = NewFunctionTemplate(isolate, SocketAddressBlockListWrap::New);
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BlockList"));
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->InstanceTemplate()->SetInternalFieldCount(kInternalFieldCount);
SetProtoMethod(isolate, tmpl, "addAddress", AddAddress);
SetProtoMethod(isolate, tmpl, "addRange", AddRange);
Expand Down Expand Up @@ -757,7 +756,6 @@ Local<FunctionTemplate> SocketAddressBase::GetConstructorTemplate(
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SocketAddress"));
tmpl->InstanceTemplate()->SetInternalFieldCount(
SocketAddressBase::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
SetProtoMethod(isolate, tmpl, "detail", Detail);
SetProtoMethod(isolate, tmpl, "legacyDetail", LegacyDetail);
SetProtoMethodNoSideEffect(isolate, tmpl, "flowlabel", GetFlowLabel);
Expand Down
1 change: 0 additions & 1 deletion src/node_trace_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void NodeCategorySet::Initialize(Local<Object> target,
NewFunctionTemplate(isolate, NodeCategorySet::New);
category_set->InstanceTemplate()->SetInternalFieldCount(
NodeCategorySet::kInternalFieldCount);
category_set->Inherit(BaseObject::GetConstructorTemplate(env));
SetProtoMethod(isolate, category_set, "enable", NodeCategorySet::Enable);
SetProtoMethod(isolate, category_set, "disable", NodeCategorySet::Disable);

Expand Down
1 change: 0 additions & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ void Initialize(Local<Object> target,
NewFunctionTemplate(isolate, WeakReference::New);
weak_ref->InstanceTemplate()->SetInternalFieldCount(
WeakReference::kInternalFieldCount);
weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
SetProtoMethod(isolate, weak_ref, "get", WeakReference::Get);
SetProtoMethod(isolate, weak_ref, "getRef", WeakReference::GetRef);
SetProtoMethod(isolate, weak_ref, "incRef", WeakReference::IncRef);
Expand Down
1 change: 0 additions & 1 deletion src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,6 @@ static void InitializePreview1(Local<Object> target,

Local<FunctionTemplate> tmpl = NewFunctionTemplate(isolate, WASI::New);
tmpl->InstanceTemplate()->SetInternalFieldCount(WASI::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));

#define V(F, name) \
SetFunction<decltype(&WASI::F), WASI::F>(WASI::F, env, name, tmpl);
Expand Down
1 change: 0 additions & 1 deletion src/node_wasm_web_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Local<Function> WasmStreamingObject::Initialize(Environment* env) {

Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
WasmStreamingObject::kInternalFieldCount);

Expand Down

0 comments on commit 7e1ecdf

Please sign in to comment.