From 8a7201b25fa2e5342b5d737742586bcd5ea1da5e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 8 Apr 2020 17:14:00 +0200 Subject: [PATCH] src: introduce BaseObject base FunctionTemplate PR-URL: https://github.com/nodejs/node/pull/33772 Reviewed-By: Benjamin Gruenbaum --- src/async_wrap.cc | 1 + src/base_object-inl.h | 1 + src/base_object.h | 3 +++ src/env.cc | 11 +++++++++++ src/env.h | 1 + src/module_wrap.cc | 1 + src/node_crypto.cc | 9 +++++++++ src/node_i18n.cc | 1 + src/node_perf.cc | 1 + src/node_process_methods.cc | 5 ++++- src/node_serdes.cc | 2 ++ src/node_trace_events.cc | 1 + src/node_util.cc | 1 + src/node_wasi.cc | 1 + 14 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 13c2c3ca5a5ec3..80342b78284892 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -557,6 +557,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 f6e980e8749f61..b3fb1562707b64 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -156,6 +156,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 674eae1f0f7766..e02fd8d209f30a 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 677faf093a8c1f..4030df9ec90f57 100644 --- a/src/env.cc +++ b/src/env.cc @@ -269,6 +269,7 @@ void Environment::CreateProperties() { Local templ = FunctionTemplate::New(isolate()); templ->InstanceTemplate()->SetInternalFieldCount( BaseObject::kInternalFieldCount); + templ->Inherit(BaseObject::GetConstructorTemplate(this)); set_binding_data_ctor_template(templ); } @@ -1112,4 +1113,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 4b8670de46a8c2..a8c797635cbb8d 100644 --- a/src/env.h +++ b/src/env.h @@ -395,6 +395,7 @@ constexpr size_t kFsStatsBufferLength = #define ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(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(compiled_fn_entry_template, v8::ObjectTemplate) \ V(dir_instance_template, v8::ObjectTemplate) \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index ab8dbc9cbf7fa5..05f8b8ad762135 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -699,6 +699,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 360732e37fd514..ca394f2e24e4bb 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -455,6 +455,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); @@ -3246,6 +3247,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", @@ -3480,6 +3482,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); @@ -4095,6 +4098,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); @@ -4207,6 +4211,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); @@ -4463,6 +4468,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); @@ -4785,6 +4791,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); @@ -5095,6 +5102,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); @@ -5454,6 +5462,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 4ed1c956c9e52e..916a9974d5d6f9 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -644,6 +644,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_process_methods.cc b/src/node_process_methods.cc index d580f74478cbf7..800ce0313647d4 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -426,7 +426,10 @@ static void ReallyExit(const FunctionCallbackInfo& args) { class FastHrtime : public BaseObject { public: static Local New(Environment* env) { - Local otmpl = v8::ObjectTemplate::New(env->isolate()); + Local ctor = + v8::FunctionTemplate::New(env->isolate()); + ctor->Inherit(BaseObject::GetConstructorTemplate(env)); + Local otmpl = ctor->InstanceTemplate(); otmpl->SetInternalFieldCount(FastHrtime::kInternalFieldCount); auto create_func = [env](auto fast_func, auto slow_func) { diff --git a/src/node_serdes.cc b/src/node_serdes.cc index c7877215911f8e..28844f1858ff3d 100644 --- a/src/node_serdes.cc +++ b/src/node_serdes.cc @@ -451,6 +451,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); @@ -478,6 +479,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 ec3f8e1fe7deaf..199b04c95b44d4 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -325,6 +325,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 4a78f99365bbe6..48ef82bd088d6a 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1679,6 +1679,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);