Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src: introduce BaseObject base FunctionTemplate
PR-URL: #33772
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
addaleax committed Jun 19, 2020
commit 899aa5cdf9401ecd43339f3525bd9545ca0b125a
1 change: 1 addition & 0 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ Local<FunctionTemplate> 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);
Expand Down
1 change: 1 addition & 0 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
};

v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
return t;
Expand Down
3 changes: 3 additions & 0 deletions src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class BaseObject : public MemoryRetainer {
// a BaseObjectPtr to this object.
inline void Detach();

static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);

protected:
virtual inline void OnGCCollect();

Expand Down
11 changes: 11 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ void Environment::CreateProperties() {
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
templ->Inherit(BaseObject::GetConstructorTemplate(this));

set_binding_data_ctor_template(templ);
}
Expand Down Expand Up @@ -1112,4 +1113,14 @@ bool BaseObject::IsRootNode() const {
return !persistent_handle_.IsWeak();
}

Local<FunctionTemplate> BaseObject::GetConstructorTemplate(Environment* env) {
Local<FunctionTemplate> 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
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,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) \
Expand Down
1 change: 1 addition & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ void ModuleWrap::Initialize(Local<Object> 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);
Expand Down
9 changes: 9 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(
SecureContext::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
Local<String> secureContextString =
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
t->SetClassName(secureContextString);
Expand Down Expand Up @@ -3246,6 +3247,7 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(
KeyObject::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

env->SetProtoMethod(t, "init", Init);
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
Expand Down Expand Up @@ -3480,6 +3482,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "init", Init);
env->SetProtoMethod(t, "initiv", InitIv);
Expand Down Expand Up @@ -4095,6 +4098,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "init", HmacInit);
env->SetProtoMethod(t, "update", HmacUpdate);
Expand Down Expand Up @@ -4207,6 +4211,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "update", HashUpdate);
env->SetProtoMethod(t, "digest", HashDigest);
Expand Down Expand Up @@ -4463,6 +4468,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "init", SignInit);
env->SetProtoMethod(t, "update", SignUpdate);
Expand Down Expand Up @@ -4785,6 +4791,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "init", VerifyInit);
env->SetProtoMethod(t, "update", VerifyUpdate);
Expand Down Expand Up @@ -5095,6 +5102,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {

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

env->SetProtoMethod(t, "generateKeys", GenerateKeys);
env->SetProtoMethod(t, "computeSecret", ComputeSecret);
Expand Down Expand Up @@ -5454,6 +5462,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
HandleScope scope(env->isolate());

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

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

Expand Down
1 change: 1 addition & 0 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ void Initialize(Local<Object> target,
// ConverterObject
{
Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate());
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
ConverterObject::kInternalFieldCount);
Local<String> converter_string =
Expand Down
1 change: 1 addition & 0 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ void Initialize(Local<Object> 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);
Expand Down
2 changes: 2 additions & 0 deletions src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ void Initialize(Local<Object> target,

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

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

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

env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
Expand Down
1 change: 1 addition & 0 deletions src/node_trace_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void NodeCategorySet::Initialize(Local<Object> 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);

Expand Down
1 change: 1 addition & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ void Initialize(Local<Object> 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);
Expand Down
1 change: 1 addition & 0 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,7 @@ static void Initialize(Local<Object> 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);
Expand Down