Skip to content

Commit 3fce8a8

Browse files
committed
src: introduce BaseObject base FunctionTemplate
This enables us to tell whether a JS bindings object is associated with a `BaseObject` or not.
1 parent bcdbd57 commit 3fce8a8

File tree

13 files changed

+34
-0
lines changed

13 files changed

+34
-0
lines changed

src/async_wrap.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
469469
if (tmpl.IsEmpty()) {
470470
tmpl = env->NewFunctionTemplate(nullptr);
471471
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
472+
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
472473
env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
473474
env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset);
474475
env->SetProtoMethod(tmpl, "getProviderType", AsyncWrap::GetProviderType);

src/base_object-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
156156
};
157157

158158
v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
159+
t->Inherit(BaseObject::GetConstructorTemplate(env));
159160
t->InstanceTemplate()->SetInternalFieldCount(
160161
BaseObject::kInternalFieldCount);
161162
return t;

src/base_object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ class BaseObject : public MemoryRetainer {
9898
// a BaseObjectPtr to this object.
9999
inline void Detach();
100100

101+
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
102+
Environment* env);
103+
101104
protected:
102105
virtual inline void OnGCCollect();
103106

src/env.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ void Environment::CreateProperties() {
270270
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
271271
templ->InstanceTemplate()->SetInternalFieldCount(
272272
BaseObject::kInternalFieldCount);
273+
templ->Inherit(BaseObject::GetConstructorTemplate(this));
273274

274275
set_binding_data_ctor_template(templ);
275276
}
@@ -1195,4 +1196,14 @@ Local<Object> BaseObject::WrappedObject() const {
11951196
return object();
11961197
}
11971198

1199+
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(Environment* env) {
1200+
Local<FunctionTemplate> tmpl = env->base_object_ctor_template();
1201+
if (tmpl.IsEmpty()) {
1202+
tmpl = env->NewFunctionTemplate(nullptr);
1203+
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BaseObject"));
1204+
env->set_base_object_ctor_template(tmpl);
1205+
}
1206+
return tmpl;
1207+
}
1208+
11981209
} // namespace node

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ constexpr size_t kFsStatsBufferLength =
392392
#define ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) \
393393
V(async_wrap_ctor_template, v8::FunctionTemplate) \
394394
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
395+
V(base_object_ctor_template, v8::FunctionTemplate) \
395396
V(binding_data_ctor_template, v8::FunctionTemplate) \
396397
V(compiled_fn_entry_template, v8::ObjectTemplate) \
397398
V(dir_instance_template, v8::ObjectTemplate) \

src/module_wrap.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ void ModuleWrap::Initialize(Local<Object> target,
691691
tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"));
692692
tpl->InstanceTemplate()->SetInternalFieldCount(
693693
ModuleWrap::kInternalFieldCount);
694+
tpl->Inherit(BaseObject::GetConstructorTemplate(env));
694695

695696
env->SetProtoMethod(tpl, "link", Link);
696697
env->SetProtoMethod(tpl, "instantiate", Instantiate);

src/node_crypto.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
454454
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
455455
t->InstanceTemplate()->SetInternalFieldCount(
456456
SecureContext::kInternalFieldCount);
457+
t->Inherit(BaseObject::GetConstructorTemplate(env));
457458
Local<String> secureContextString =
458459
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
459460
t->SetClassName(secureContextString);
@@ -3242,6 +3243,7 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
32423243
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
32433244
t->InstanceTemplate()->SetInternalFieldCount(
32443245
KeyObject::kInternalFieldCount);
3246+
t->Inherit(BaseObject::GetConstructorTemplate(env));
32453247

32463248
env->SetProtoMethod(t, "init", Init);
32473249
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
@@ -3476,6 +3478,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
34763478

34773479
t->InstanceTemplate()->SetInternalFieldCount(
34783480
CipherBase::kInternalFieldCount);
3481+
t->Inherit(BaseObject::GetConstructorTemplate(env));
34793482

34803483
env->SetProtoMethod(t, "init", Init);
34813484
env->SetProtoMethod(t, "initiv", InitIv);
@@ -4090,6 +4093,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
40904093

40914094
t->InstanceTemplate()->SetInternalFieldCount(
40924095
Hmac::kInternalFieldCount);
4096+
t->Inherit(BaseObject::GetConstructorTemplate(env));
40934097

40944098
env->SetProtoMethod(t, "init", HmacInit);
40954099
env->SetProtoMethod(t, "update", HmacUpdate);
@@ -4202,6 +4206,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
42024206

42034207
t->InstanceTemplate()->SetInternalFieldCount(
42044208
Hash::kInternalFieldCount);
4209+
t->Inherit(BaseObject::GetConstructorTemplate(env));
42054210

42064211
env->SetProtoMethod(t, "update", HashUpdate);
42074212
env->SetProtoMethod(t, "digest", HashDigest);
@@ -4458,6 +4463,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
44584463

44594464
t->InstanceTemplate()->SetInternalFieldCount(
44604465
SignBase::kInternalFieldCount);
4466+
t->Inherit(BaseObject::GetConstructorTemplate(env));
44614467

44624468
env->SetProtoMethod(t, "init", SignInit);
44634469
env->SetProtoMethod(t, "update", SignUpdate);
@@ -4780,6 +4786,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
47804786

47814787
t->InstanceTemplate()->SetInternalFieldCount(
47824788
SignBase::kInternalFieldCount);
4789+
t->Inherit(BaseObject::GetConstructorTemplate(env));
47834790

47844791
env->SetProtoMethod(t, "init", VerifyInit);
47854792
env->SetProtoMethod(t, "update", VerifyUpdate);
@@ -5090,6 +5097,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
50905097

50915098
t->InstanceTemplate()->SetInternalFieldCount(
50925099
DiffieHellman::kInternalFieldCount);
5100+
t->Inherit(BaseObject::GetConstructorTemplate(env));
50935101

50945102
env->SetProtoMethod(t, "generateKeys", GenerateKeys);
50955103
env->SetProtoMethod(t, "computeSecret", ComputeSecret);
@@ -5448,6 +5456,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
54485456
HandleScope scope(env->isolate());
54495457

54505458
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
5459+
t->Inherit(BaseObject::GetConstructorTemplate(env));
54515460

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

src/node_i18n.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ void Initialize(Local<Object> target,
811811
// ConverterObject
812812
{
813813
Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate());
814+
t->Inherit(BaseObject::GetConstructorTemplate(env));
814815
t->InstanceTemplate()->SetInternalFieldCount(
815816
ConverterObject::kInternalFieldCount);
816817
Local<String> converter_string =

src/node_perf.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ void Initialize(Local<Object> target,
647647
eldh->SetClassName(eldh_classname);
648648
eldh->InstanceTemplate()->SetInternalFieldCount(
649649
ELDHistogram::kInternalFieldCount);
650+
eldh->Inherit(BaseObject::GetConstructorTemplate(env));
650651
env->SetProtoMethod(eldh, "exceeds", ELDHistogramExceeds);
651652
env->SetProtoMethod(eldh, "min", ELDHistogramMin);
652653
env->SetProtoMethod(eldh, "max", ELDHistogramMax);

src/node_serdes.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ void Initialize(Local<Object> target,
452452

453453
ser->InstanceTemplate()->SetInternalFieldCount(
454454
SerializerContext::kInternalFieldCount);
455+
ser->Inherit(BaseObject::GetConstructorTemplate(env));
455456

456457
env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
457458
env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
@@ -479,6 +480,7 @@ void Initialize(Local<Object> target,
479480

480481
des->InstanceTemplate()->SetInternalFieldCount(
481482
DeserializerContext::kInternalFieldCount);
483+
des->Inherit(BaseObject::GetConstructorTemplate(env));
482484

483485
env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
484486
env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);

0 commit comments

Comments
 (0)