diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 2ffe38580bc281..d748219d55212a 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -498,6 +498,7 @@ Local AsyncWrap::GetConstructorTemplate(Environment* env) { if (tmpl.IsEmpty()) { tmpl = env->NewFunctionTemplate(nullptr); tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap")); + tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId); env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset); env->SetProtoMethod(tmpl, "getProviderType", AsyncWrap::GetProviderType); diff --git a/src/base_object-inl.h b/src/base_object-inl.h index cee7976c37e004..3f4aef4def9836 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -155,6 +155,7 @@ BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) { }; v8::Local t = env->NewFunctionTemplate(constructor); + t->Inherit(BaseObject::GetConstructorTemplate(env)); t->InstanceTemplate()->SetInternalFieldCount( BaseObject::kInternalFieldCount); return t; diff --git a/src/base_object.h b/src/base_object.h index 24965a9879825d..9e47a028ed456b 100644 --- a/src/base_object.h +++ b/src/base_object.h @@ -98,6 +98,9 @@ class BaseObject : public MemoryRetainer { // a BaseObjectPtr to this object. inline void Detach(); + static v8::Local GetConstructorTemplate( + Environment* env); + protected: virtual inline void OnGCCollect(); diff --git a/src/env.cc b/src/env.cc index 67d7365073c31b..4411a46b9ca726 100644 --- a/src/env.cc +++ b/src/env.cc @@ -264,6 +264,7 @@ void Environment::CreateProperties() { Local ctx = context(); Local templ = FunctionTemplate::New(isolate()); templ->InstanceTemplate()->SetInternalFieldCount(1); + templ->Inherit(BaseObject::GetConstructorTemplate(this)); Local obj = templ->GetFunction(ctx) .ToLocalChecked() ->NewInstance(ctx) @@ -1208,4 +1209,14 @@ bool BaseObject::IsRootNode() const { return !persistent_handle_.IsWeak(); } +Local BaseObject::GetConstructorTemplate(Environment* env) { + Local tmpl = env->base_object_ctor_template(); + if (tmpl.IsEmpty()) { + tmpl = env->NewFunctionTemplate(nullptr); + tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BaseObject")); + env->set_base_object_ctor_template(tmpl); + } + return tmpl; +} + } // namespace node diff --git a/src/env.h b/src/env.h index e7ec30f966bcf8..9b9af74a559c90 100644 --- a/src/env.h +++ b/src/env.h @@ -406,6 +406,7 @@ constexpr size_t kFsStatsBufferLength = V(as_callback_data_template, v8::FunctionTemplate) \ V(async_wrap_ctor_template, v8::FunctionTemplate) \ V(async_wrap_object_ctor_template, v8::FunctionTemplate) \ + V(base_object_ctor_template, v8::FunctionTemplate) \ V(compiled_fn_entry_template, v8::ObjectTemplate) \ V(dir_instance_template, v8::ObjectTemplate) \ V(fd_constructor_template, v8::ObjectTemplate) \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 97403eb54c9445..6f052f554518e7 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -691,6 +691,7 @@ void ModuleWrap::Initialize(Local target, tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap")); tpl->InstanceTemplate()->SetInternalFieldCount( ModuleWrap::kInternalFieldCount); + tpl->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(tpl, "link", Link); env->SetProtoMethod(tpl, "instantiate", Instantiate); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 2236056d016cc6..d570b9df333c8e 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -434,6 +434,7 @@ void SecureContext::Initialize(Environment* env, Local target) { Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( SecureContext::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); Local secureContextString = FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"); t->SetClassName(secureContextString); @@ -3208,6 +3209,7 @@ Local KeyObject::Initialize(Environment* env, Local target) { Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( KeyObject::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", Init); env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize", @@ -3442,6 +3444,7 @@ void CipherBase::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( CipherBase::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", Init); env->SetProtoMethod(t, "initiv", InitIv); @@ -4069,6 +4072,7 @@ void Hmac::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( Hmac::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", HmacInit); env->SetProtoMethod(t, "update", HmacUpdate); @@ -4195,6 +4199,7 @@ void Hash::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( Hash::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "update", HashUpdate); env->SetProtoMethod(t, "digest", HashDigest); @@ -4467,6 +4472,7 @@ void Sign::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( SignBase::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", SignInit); env->SetProtoMethod(t, "update", SignUpdate); @@ -4792,6 +4798,7 @@ void Verify::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( SignBase::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", VerifyInit); env->SetProtoMethod(t, "update", VerifyUpdate); @@ -5104,6 +5111,7 @@ void DiffieHellman::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( DiffieHellman::kInternalFieldCount); + t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "generateKeys", GenerateKeys); env->SetProtoMethod(t, "computeSecret", ComputeSecret); @@ -5462,6 +5470,7 @@ void ECDH::Initialize(Environment* env, Local target) { HandleScope scope(env->isolate()); Local t = env->NewFunctionTemplate(New); + t->Inherit(BaseObject::GetConstructorTemplate(env)); t->InstanceTemplate()->SetInternalFieldCount(ECDH::kInternalFieldCount); diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 169374aa5de441..5382e469a4087c 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -811,6 +811,7 @@ void Initialize(Local target, // ConverterObject { Local t = FunctionTemplate::New(env->isolate()); + t->Inherit(BaseObject::GetConstructorTemplate(env)); t->InstanceTemplate()->SetInternalFieldCount( ConverterObject::kInternalFieldCount); Local converter_string = diff --git a/src/node_perf.cc b/src/node_perf.cc index 4a1eaac2fcf10d..fdc3c732c92bc2 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -652,6 +652,7 @@ void Initialize(Local target, eldh->SetClassName(eldh_classname); eldh->InstanceTemplate()->SetInternalFieldCount( ELDHistogram::kInternalFieldCount); + eldh->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(eldh, "exceeds", ELDHistogramExceeds); env->SetProtoMethod(eldh, "min", ELDHistogramMin); env->SetProtoMethod(eldh, "max", ELDHistogramMax); diff --git a/src/node_serdes.cc b/src/node_serdes.cc index bcdcd19b261e88..0efb09066ae6da 100644 --- a/src/node_serdes.cc +++ b/src/node_serdes.cc @@ -453,6 +453,7 @@ void Initialize(Local target, ser->InstanceTemplate()->SetInternalFieldCount( SerializerContext::kInternalFieldCount); + ser->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader); env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue); @@ -480,6 +481,7 @@ void Initialize(Local target, des->InstanceTemplate()->SetInternalFieldCount( DeserializerContext::kInternalFieldCount); + des->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader); env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue); diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index 9adee9e458ccc0..58813a9083a560 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -131,6 +131,7 @@ void NodeCategorySet::Initialize(Local target, env->NewFunctionTemplate(NodeCategorySet::New); category_set->InstanceTemplate()->SetInternalFieldCount( NodeCategorySet::kInternalFieldCount); + category_set->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable); env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable); diff --git a/src/node_util.cc b/src/node_util.cc index eef53d071f7391..eac09f8d44fcbd 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -340,6 +340,7 @@ void Initialize(Local target, weak_ref->InstanceTemplate()->SetInternalFieldCount( WeakReference::kInternalFieldCount); weak_ref->SetClassName(weak_ref_string); + weak_ref->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(weak_ref, "get", WeakReference::Get); env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef); env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef); diff --git a/src/node_wasi.cc b/src/node_wasi.cc index c76562d7921ff1..b8539a9395e8b3 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1680,6 +1680,7 @@ static void Initialize(Local target, auto wasi_wrap_string = FIXED_ONE_BYTE_STRING(env->isolate(), "WASI"); tmpl->InstanceTemplate()->SetInternalFieldCount(WASI::kInternalFieldCount); tmpl->SetClassName(wasi_wrap_string); + tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(tmpl, "args_get", WASI::ArgsGet); env->SetProtoMethod(tmpl, "args_sizes_get", WASI::ArgsSizesGet);