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
This enables us to tell whether a JS bindings object is associated
with a `BaseObject` or not.
  • Loading branch information
addaleax committed May 8, 2020
commit 3fce8a83d76fd0b4ec8770500de4291bf6a771ad
1 change: 1 addition & 0 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,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 @@ -270,6 +270,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 @@ -1195,4 +1196,14 @@ Local<Object> BaseObject::WrappedObject() const {
return object();
}

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 @@ -392,6 +392,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 @@ -691,6 +691,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 @@ -454,6 +454,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 @@ -3242,6 +3243,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 @@ -3476,6 +3478,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 @@ -4090,6 +4093,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 @@ -4202,6 +4206,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 @@ -4458,6 +4463,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 @@ -4780,6 +4786,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 @@ -5090,6 +5097,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 @@ -5448,6 +5456,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 @@ -647,6 +647,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 @@ -452,6 +452,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 @@ -479,6 +480,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 @@ -326,6 +326,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 @@ -1825,6 +1825,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